Date post: | 12-Jan-2016 |
Category: |
Documents |
Upload: | ilene-nichols |
View: | 223 times |
Download: | 2 times |
1
TK1924 Program Design & Problem Solving
Session 2011/2012
L8: Binary Trees
2
Objectives
• Learn about binary trees
• Explore various binary tree traversal algorithms
• Learn how to organize data in a binary search tree
• Discover how to insert and delete items in a binary search tree
3
What is a tree?
Root
Leaf
4
What is a tree?
Root
Leaf
5
What is a tree?
Root
Leaf
6
What is a tree?
Root
Leaf
7
Root
Node
What is a tree?
8
Family Tree Terms
Root
1
2 3
6
4
7
109
8
1211
13
5
9
1
2 3
6
4
7
109
8
1211
13
5
1: Root
10
1
2 3
6
4
7
109
8
1211
13
5
1: Root5, 9, 10, 12, 13: Leaf (no children)
11
1
2 3
6
4
7
109
8
1211
13
5
1: Root5, 9, 10, 12, 13: Leaf (no children)7 : child of 4 (accessed directly)
12
1
2 3
6
4
7
109
8
1211
13
5
1: Root5, 9, 10, 12, 13: Leaf (no children)7 : child of 4 (accessed directly)4 : parent of 7, parent of 8 (accessing node)
13
1
2 3
6
4
7
109
8
1211
13
5
1: Root5, 9, 10, 12, 13: Leaf (no children)7 : child of 4 (accessed directly)4 : parent of 7, parent of 8 (accessing node)7 and 8: siblings (same parent)
14
1
2 3
6
4
7
109
8
1211
13
5
1: Root5, 9, 10, 12, 13: Leaf (no children)7 : child of 4 (accessed directly)4 : parent of 7, parent of 8 (accessing node)7 and 8: siblings (same parent) : stem/branch
15
Binary Trees
1
2 3
6
4
7
109
8
1211
13
5
Condition: each node must not have More than 2 children
16
Binary Trees
Condition: Each node must not have more than 2 children
1
2
5
3
6
8
7
109
11
4
1
2
3
4
5
17
Binary Trees
• Definition: A binary tree, T, is either empty or such that:– T has a special node called the root node;– T has two sets of nodes, LT and RT, called the
left subtree and right subtree of T, respectively; – LT and RT are binary trees
18
Binary Tree
1
2
5
3
6
8
7
109
11
4
1
2
3
4
5
19
Binary Tree With One Node
The root node of the binary tree = A
LA = empty
RA = empty
A
20
Binary Trees With Two Nodes
A
B
Binary tree with two nodes; the right sub-tree of the rood node is empty.
A
C
Binary tree with two nodes; the left sub-tree of the rood node is empty.
21
Various Binary Trees With Three Nodes
A
B
D
A
B
E
A
C
G
A
C
F
(i) (ii) (iii) (iv)
22
Binary Trees
Following struct defines the node of a binary tree:
template<class elemType>
struct nodeType
{
elemType info;
nodeType<elemType> *llink;
nodeType<elemType> *rlink;
};
23
Nodes
• For each node:– Data is stored in info– The pointer to the left child is stored in llink– The pointer to the right child is stored in rlink
24
General Binary Tree
25
Binary Tree Definitions
• Leaf: node that has no left and right children• Parent: node with at least one child node• Level of a node: number of branches on the path
from root to node• Height of a binary tree: number of nodes on the
longest path from root to node• Width of a binary tree: the maximum number of
elements on one level of the tree
26
Binary Trees
1
2
5
3
6
8
7
109
11
4
Height of tree = 5Level
1
2
3
4
5
Width of tree = 4
27
Height of a Binary Tree
Recursive algorithm to find height of binary tree:
if(p is NULL)
height(p) = 0
else
height(p) = 1 + max(height(p->llink), height(p->rlink))
(height(p) denotes height of binary tree with root p):
28
Height of a Binary Tree
Function to implement above algorithm:
template<class elemType>int height(nodeType<elemType> *p){ if(p == NULL) return 0; else return 1 + max(height(p->llink),
height(p->rlink));}
29
Binary Tree Traversal
• Must start with the root, then– Visit the node first or– Visit the subtrees first
• Three different traversals– Inorder– Preorder– Postorder
30
Traversals
• Inorder – Traverse the left subtree– Visit the node– Traverse the right subtree
• Preorder– Visit the node– Traverse the left subtree– Traverse the right subtree
• Postorder– Traverse the left subtree
– Traverse the right subtree
– Visit the node
31
The order of traversal being discussed is as follows:• N : visit node• L : Traverse left subtree• R : Traverse right subtree
Traverse BST
32
Traverse BST
64
10
33
88
7 99
If NLR (Preorder): ??? If LNR (Inorder): ??? If LRN (Postorder): ???
64 10 7 33 88 997 10 33 64 88 99
7 33 10 99 88 64
33
Binary Tree: Inorder Traversal
template<class elemType>
void inorder(nodeType<elemType> *p)
{
if(p != NULL)
{
inorder(p->llink);
cout<<p->info<<” “;
inorder(p->rlink);
}
}
34
Binary Tree: preorder Traversal
template<class elemType>
void preorder(nodeType<elemType> *p)
{
if(p != NULL)
{
cout<<p->info<<” “;
preorder(p->llink);
preorder(p->rlink);
}
}
35
Binary Tree: postorder Traversals
template<class elemType>
void postorder(nodeType<elemType> *p)
{
if(p != NULL)
{
postorder(p->llink);
postorder(p->rlink);
cout<<p->info<<” “;
}
}1
36
Implementing Binary Trees: class binaryTreeType Functions
• Public– isEmpty
– inorderTraversal
– preorderTraversal
– postorderTraversal
– treeHeight
– treeNodeCount
– treeLeavesCount
– destroyTree
• Private• copyTree
• Destroy
• Inorder, preorder, postorder
• Height
• Max
• nodeCount
• leavesCount
37
Binary Search Trees
• Data in each node– Larger than the data in its left child– Smaller than the data in its right child
• A binary search tree,t, is either empty or:– T has a special node called the root node– T has two sets of nodes, LT and RT, called the left
subtree and right subtree of T, respectively– Key in root node larger than every key in left subtree
and smaller than every key in right subtree– LT and RT are binary search trees
38
Binary Search Trees
64
10
7 33
88
99 64
10
33
88
997
39
Operations Performed on Binary Search Trees
• Determine whether the binary search tree is empty
• Search the binary search tree for a particular item
• Insert an item in the binary search tree
• Delete an item from the binary search tree
40
Search BST
64
10
33
88
7 99
Find 33
41
Search BST
64
10
33
88
7 99
Find 33
42
Search BST
64
10
33
88
7 99
Find 3333 = 64?
43
Search BST
64
10
33
88
7 99
Find 3333 < 64?
44
Search BST
64
10
33
88
7 99
Find 33
45
Search BST
64
10
33
88
7 99
Find 33
33 = 10?
46
Search BST
64
10
33
88
7 99
Find 33
33 < 10?
47
Search BST
64
10
33
88
7 99
Find 33
33 = 33?
48
Search BST
64
10
33
88
7 99
Find 33
49
Search BST
64
10
33
88
7 99
Find 6
50
Search BST
64
10
33
88
7 99
Find 66 = 64?
51
Search BST
64
10
33
88
7 99
Find 66 < 64?
52
Search BST
64
10
33
88
7 99
Find 6
6 = 10?
53
Search BST
64
10
33
88
7 99
Find 6
6 < 10?
54
Search BST
64
10
33
88
7 99
Find 6
6 = 7?
55
Search BST
64
10
33
88
7 99
Find 6
6 < 7?
56
Search BST
64
10
33
88
7 99
Find 6
NULL
57
Insert Node
5 8 9 13 21 44 45 46
5 8 9 13 14 21 44 45 46
WHAT ARE THE FEATURES OF A SORTED LIST????
FEATURES OF A SORTED LIST IS PRESERVED
Given a sorted list:
If 14 is inserted, the list become:
58
Insert Node
64
10
33
88
7 99
How does the new BST look like ? How do you do it?
Given a BST:
If 14 to be inserted :
59
Insert Node
64
10
33
88
7 99
14
Are the features of a BST preserved???
Given a BST:
60
Insert Node
64
10
33
88
7 99
14 < 64?
Algorithm to insert 14:
61
Insert Node
64
10
33
88
7 99
14 < 10?
Algorithm to insert 14:
62
Insert Node
64
10
33
88
7 99
14 < 33?
Algorithm to insert 14:
63
Insert Node
64
10
33
88
7 99
NULL
Algorithm to insert 14:
64
Insert Node
64
10
33
88
7 99
Insert here
14
Algorithm to insert 14:
65
Insert Node
64
10
33
88
7 99
14
Algorithm to insert 14:
66
Insert Node
Try insert 99:
64
10
33
88
7 99
14
67
Insert Node
64
10
33
88
7 99
14
99 < 64?
Try insert 99:
68
Insert Node
64
10
33
88
7 99
14
99 < 88?
Try insert 99:
69
Insert Node
64
10
33
88
7 99
1499 < 99?
Try insert 99:
70
Insert Node
This insert algorithm is not very effective
Try building a BST using the same algorithm for the following sequence:
K N G I C U
K U C I N G
C G I K N U
It is difficult to produce a balanced Tree one way is using the AVL Tree algorithm
71
4 cases node deletion :
• The node to be deleted is a leafCase 1: The node to be deleted has no left and right subtrees
• The node to be deleted has 1 childCase 2: The node to be deleted has no left subtree
Case 3: The node to be deleted has no right subtree
• The node to be deleted has 2 childrenCase 4: The node to be deleted has nonempty left and right
subtrees
Delete Node
72
M
E
D
P
B VN
T ZA
To delete D:
Delete Node: First Case
Case 1: The node to be deleted has no left and right subtrees
parent
73
M
E
D
P
B VN
T ZA
To delete D
parent
Set the right child of parent as null
x
Delete Node: First Case
74
M
E
D
P
B VN
T ZA
To delete D
parent
x
Delete Node: First Case
Set the right child of parent as null
Delete Node: First Case
75
To delete D
Delete x
M
E
D
P
B VN
T ZA
parent
x
Set the right child of parent as null
76
M
E P
B VN
T ZA
To delete D
parent
Delete Node: First Case
77
To delete E
Delete Node: Second Case
Case 3: The node to be deleted has no right subtree
M
E P
B VN
T ZA D
78
Delete Node: Second Case
M
E P
B VN
T ZA
To delete E
D
x
parent
79
Delete Node: Second Case
M
E P
B VN
T ZA
To delete E
D
x
parent
Set the Lchild (@ Rchild) of x as the Lchild (@ Rchild) of parent
80
Delete Node: Second Case
M
E P
B VN
T ZA
To delete E
D
x
parent
Set the Lchild (@ Rchild) of x as the Lchild (@ Rchild) of parent
81
Delete Node: Second Case
M
E P
B VN
T ZA
To delete E
D
x
parent
Set the Lchild (@ Rchild) of x as the Lchild (@ Rchild) of parentFree x
82
Delete Node: Second Case
M
P
B VN
T ZA
To delete E
D
parent
Set the Lchild (@ Rchild) of x as the Lchild (@ Rchild) of parentFree x
83
Delete Node: Second Case
M
PB
VN
T Z
A
To delete E
D
parent
Set the Lchild (@ Rchild) of x as the Lchild (@ Rchild) of parentFree x
84
Delete Node: Third Case
M
E P
B VN
T ZA D
To delete P
Case 4: The node to be deleted has nonempty left and right subtrees
85
Delete Node: Third Case
To delete P
Case 4: The node to be deleted has nonempty left and right subtrees
M
E P
B VN
T ZA D
parent
x
86
Delete Node: Third Case
M
E P
B VN
T ZA D
To delete P
Determine the next node based on Inorder(LNR)
parent
x
*Usually, the Inorder node has only one child or no children at all
87
Delete Node: Third Case
M
E P
B VN
T ZA D
To delete P
Determine the next node based on Inorder(LNR)
parent
x
y
parent of y
88
Delete Node: Third Case
M
E P
B VN
T ZA D
To delete P
Copy
parent
x
y
parent of y
89
Delete Node: Third Case
M
E P
B VN
T ZA D
To delete P
Copy x->data = y->data
parent
x
y
parent of y
90
Delete Node: Third Case
M
E T
B VN
T ZA D
To delete P parent
x
y
Copy x->data = y->data
parent of y
91
Delete Node: Third Case
M
E T
B VN
T ZA D
To delete P parent
xy
Copy x->data = y->data x = y
parent of y
92
Delete Node: Third Case
M
E T
B VN
T ZA D
To delete P
Delete T
parent
y x
parent of y
93
Delete Node: Third Case
M
E T
B VN
T ZA D
To delete P
Delete T (as in case 1)parent_y->Lchild = NULL
parent
y
parent of y
94
Delete Node: Third Case
M
E T
B VN
ZA D
To delete P
Delete T
parent
parent of y
95
Operations Performed on Binary Search Trees
• Find the height of the binary search tree
• Find the number of nodes in the binary search tree
• Find the number of leaves in the binary search tree
• Traverse the binary search tree
• Copy the binary search tree
96
Summary
• Binary trees
• Binary search trees
• Recursive traversal algorithms