Transcript Chapter 11

TK1924
Program Design & Problem Solving
Session 2011/2012
L8: Binary Trees
1
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
2
What is a tree?
Leaf
Root
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
Node
7
Family Tree Terms
Root
1
2
3
4
5
6
7
9
10
8
11
12
13
8
1: Root
1
2
3
4
5
6
7
9
10
8
11
12
13
9
1: Root
5, 9, 10, 12, 13: Leaf (no children)
1
2
3
4
5
6
7
9
10
8
11
12
13
10
1: Root
5, 9, 10, 12, 13: Leaf (no children)
7 : child of 4 (accessed directly)
1
2
3
4
5
6
7
9
10
8
11
12
13
11
1: Root
5, 9, 10, 12, 13: Leaf (no children)
7 : child of 4 (accessed directly)
4 : parent of 7, parent of 8 (accessing node)
1
2
3
4
5
6
7
9
10
8
11
12
13
12
1: Root
5, 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)
1
2
3
4
5
6
7
9
10
8
11
12
13
13
1: Root
5, 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
1
2
3
4
5
6
7
9
10
8
11
12
13
14
Binary Trees
Condition:
each node must not have
More than 2 children
1
2
3
4
5
6
7
9
10
8
11
12
13
15
Binary Trees
Condition:
Each node must not have
more than 2 children
1
1
2
4
2
3
5
6
8
7
9
11
3
4
10
5
16
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
17
Binary Tree
1
1
2
4
2
3
5
6
8
7
9
11
3
4
10
5
18
Binary Tree With One Node
A
The root node of the binary tree = A
LA = empty
RA = empty
19
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.
20
Various Binary Trees With Three
Nodes
A
B
D
A
B
A
C
E
(i)
A
(ii)
C
F
G
(iii)
(iv)
21
Binary Trees
Following struct defines the node of a binary tree:
template<class elemType>
struct nodeType
{
elemType info;
nodeType<elemType> *llink;
nodeType<elemType> *rlink;
};
22
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
23
General Binary Tree
24
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
25
Binary Trees
Level
Height of tree = 5
1
1
Width of tree = 4
2
2
3
3
4
5
6
8
7
9
11
4
10
5
26
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):
27
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));
}
28
Binary Tree Traversal
• Must start with the root, then
– Visit the node first or
– Visit the subtrees first
• Three different traversals
– Inorder
– Preorder
– Postorder
29
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
30
Traverse BST
The order of traversal being discussed is as follows:
• N : visit node
• L : Traverse left subtree
• R : Traverse right subtree
31
Traverse BST
64
10
7
88
33
If NLR (Preorder): 64
???10 7 33 88 99
If LNR (Inorder): 7???
10 33 64 88 99
If LRN (Postorder): 7???
33 10 99 88 64
99
32
Binary Tree: Inorder Traversal
template<class elemType>
void inorder(nodeType<elemType> *p)
{
if(p != NULL)
{
inorder(p->llink);
cout<<p->info<<” “;
inorder(p->rlink);
}
}
33
Binary Tree: preorder Traversal
template<class elemType>
void preorder(nodeType<elemType> *p)
{
if(p != NULL)
{
cout<<p->info<<” “;
preorder(p->llink);
preorder(p->rlink);
}
}
34
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
35
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
36
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
37
Binary Search Trees
64
10
7
88
33
64
99
10
7
88
33
99
38
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
39
Search BST
Find 33
64
10
7
88
33
99
40
Search BST
Find 33
64
10
7
88
33
99
41
Search BST
33 = 64?
Find 33
64
10
7
88
33
99
42
Search BST
33 < 64?
Find 33
64
10
7
88
33
99
43
Search BST
Find 33
64
10
7
88
33
99
44
Search BST
Find 33
64
33 = 10?
10
7
88
33
99
45
Search BST
Find 33
64
33 < 10?
10
7
88
33
99
46
Search BST
Find 33
64
10
7
88
33
33 = 33?
99
47
Search BST
Find 33
64
10
7
88
33
99
48
Search BST
Find 6
64
10
7
88
33
99
49
Search BST
6 = 64?
Find 6
64
10
7
88
33
99
50
Search BST
6 < 64?
Find 6
64
10
7
88
33
99
51
Search BST
Find 6
64
6 = 10?
10
7
88
33
99
52
Search BST
Find 6
64
6 < 10?
10
7
88
33
99
53
Search BST
Find 6
64
10
6 = 7?
7
88
33
99
54
Search BST
Find 6
64
10
6 < 7?
7
88
33
99
55
Search BST
Find 6
64
10
7
88
33
99
NULL
56
Insert Node
Given a sorted list:
5 8 9 13 21 44 45 46
If 14 is inserted, the list become:
5 8 9 13 14 21 44 45 46
FEATURES OF A SORTED LIST IS PRESERVED
WHAT ARE THE FEATURES OF A SORTED LIST????
57
Insert Node
Given a BST:
64
10
7
88
33
99
If 14 to be inserted :
How does the new BST look like ?
How do you do it?
58
Insert Node
Given a BST:
64
10
7
88
33
99
14
Are the features of a BST preserved???
59
Insert Node
Algorithm to insert 14:
14 < 64?
64
10
7
88
33
99
60
Insert Node
Algorithm to insert 14:
64
14 < 10?
10
7
88
33
99
61
Insert Node
Algorithm to insert 14:
64
10
7
88
33
99
14 < 33?
62
Insert Node
Algorithm to insert 14:
64
10
7
88
33
99
NULL
63
Insert Node
Algorithm to insert 14:
64
10
7
88
33
99
14
Insert here
64
Insert Node
Algorithm to insert 14:
64
10
7
88
33
99
14
65
Insert Node
Try insert 99:
64
10
7
88
33
99
14
66
Insert Node
Try insert 99:
99 < 64?
64
10
7
88
33
99
14
67
Insert Node
Try insert 99:
64
10
7
88
33
99 < 88?
99
14
68
Insert Node
Try insert 99:
64
10
7
88
33
99
99 < 99?
14
69
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
It is difficult to produce a
balanced Tree
 one way is using the
AVL Tree algorithm
C G I K N U
70
Delete Node
4 cases node deletion :
• The node to be deleted is a leaf
Case 1: The node to be deleted has no left and right subtrees
• The node to be deleted has 1 child
Case 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 children
Case 4: The node to be deleted has nonempty left and right
subtrees
71
Delete Node: First Case
Case 1: The node to be deleted has no left and right subtrees
To delete D:
M
parent
E
B
A
P
N
D
V
T
Z
72
Delete Node: First Case
To delete D
M
E
parent
P
B
N
V
x
A
D
T
Z
Set the right child of parent as null
73
Delete Node: First Case
To delete D
M
E
parent
P
B
N
V
x
A
D
T
Z
Set the right child of parent as null
74
Delete Node: First Case
To delete D
M
E
parent
P
B
N
V
x
A
D
Set the right child of parent as null
Delete x
T
Z
75
Delete Node: First Case
To delete D
M
E
parent
B
A
P
N
V
T
Z
76
Delete Node: Second Case
Case 3: The node to be deleted has no right subtree
To delete E
M
E
B
A
P
N
D
V
T
Z
77
Delete Node: Second Case
parent
To delete E
M
x
E
B
A
P
N
D
V
T
Z
78
Delete Node: Second Case
parent
To delete E
M
x
E
B
A
P
N
D
V
T
Z
Set the Lchild (@ Rchild) of x as the Lchild (@ Rchild) of parent
79
Delete Node: Second Case
parent
To delete E
M
x
E
B
A
P
N
D
V
T
Z
Set the Lchild (@ Rchild) of x as the Lchild (@ Rchild) of parent
80
Delete Node: Second Case
parent
To delete E
M
x
E
B
A
P
N
D
V
T
Z
Set the Lchild (@ Rchild) of x as the Lchild (@ Rchild) of parent
Free x
81
Delete Node: Second Case
parent
To delete E
M
P
B
A
N
D
V
T
Z
Set the Lchild (@ Rchild) of x as the Lchild (@ Rchild) of parent
Free x
82
Delete Node: Second Case
parent
To delete E
M
B
A
P
D
N
V
T
Z
Set the Lchild (@ Rchild) of x as the Lchild (@ Rchild) of parent
Free x
83
Delete Node: Third Case
Case 4: The node to be deleted has nonempty left and
right subtrees
M
To delete P
E
B
A
P
N
D
V
T
Z
84
Delete Node: Third Case
Case 4: The node to be deleted has nonempty left and
right subtrees
parent
To delete P
M
x
E
B
A
P
N
D
V
T
Z
85
Delete Node: Third Case
parent
To delete P
M
Determine the next
node based on
Inorder(LNR)
x
E
B
A
P
N
D
V
T
Z
*Usually, the Inorder node has only one child or no
children at all
86
Delete Node: Third Case
parent
To delete P
M
Determine the next
node based on
Inorder(LNR)
x
E
P
parent of y
B
A
N
D
V
T
Z
y
87
Delete Node: Third Case
parent
To delete P
M
Copy
x
E
P
parent of y
B
A
N
D
V
T
Z
y
88
Delete Node: Third Case
parent
To delete P
M
Copy
x->data = y->data
x
E
P
parent of y
B
A
N
D
V
T
Z
y
89
Delete Node: Third Case
parent
To delete P
M
Copy
x->data = y->data
x
E
T
parent of y
B
A
N
D
V
T
Z
y
90
Delete Node: Third Case
parent
To delete P
M
Copy
x->data = y->data
x=y
E
T
parent of y
B
A
N
D
V
T
y
Z
x
91
Delete Node: Third Case
parent
To delete P
M
Delete T
E
T
parent of y
B
A
N
D
V
T
y
Z
x
92
Delete Node: Third Case
parent
To delete P
M
Delete T (as in case 1)
parent_y->Lchild = NULL
E
T
parent of y
B
A
N
D
V
T
Z
y
93
Delete Node: Third Case
parent
To delete P
M
Delete T
E
T
parent of y
B
A
N
D
V
Z
94
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
95
Summary
• Binary trees
• Binary search trees
• Recursive traversal algorithms
96