Professional Documents
Culture Documents
CS2133 Lec08
CS2133 Lec08
CS2133 Lec08
08
Trace of insert
14
4 15
3 9 18
7 p 16 20
5 node 17
Recall that the depth of the complete binary tree built using ‘n’
nodes will be log2(n+1) – 1.
We want to print all the values stored in the nodes of the tree.
14
4 15
N node
L left right R
subtree subtree
(L,N,R), (L,R,N)
(N,L,R), (N,R,L)
(R,L,N), (R,N,L)
Traversing a Binary Tree
N node
left right
L subtree subtree R
Preorder: (N,L,R)
Inorder: (L,N,R)
Postorder: (L,R,N)
Traversing a Binary Tree
4 15
3 9 18
7 16 20
5 17
Preorder: 14 4 3 9 7 5 15 18 16 17 20
Traversing a Binary Tree
14
4 15
3 9 18
7 16 20
5 17
Inorder: 3 4 5 7 9 14 15 16 17 18 20
Traversing a Binary Tree
14
4 15
3 9 18
7 16 20
5 17
Postorder: 3 5 7 9 4 17 16 20 18 15 14
Recursive Call
Return address(F)
sp
At point of call During execution of F After call
Recursion: preorder
14
preorder(14)
14
..preorder(4)
4 15 4
....preorder(3)
3
......preorder(null)
3 9 18 ......preorder(null)
....preorder(9)
7 20 9
16
......preorder(7)
7
........preorder(5)
5 17 5
..........preorder(null)
..........preorder(null)
........preorder(null)
......preorder(null)
Recursion: preorder
14 ..preorder(15)
15
....preorder(null)
4 15 ....preorder(18)
18
......preorder(16)
3 9 18 16
........preorder(null)
7 ........preorder(17)
16 20
17
..........preorder(null)
5 17 ..........preorder(null)
......preorder(20)
20
........preorder(null)
........preorder(null)
Recursion: inorder
14 inorder(14)
..inorder(4)
....inorder(3)
4 15 ......inorder(null)
3
......inorder(null)
4
3 9 18 ....inorder(9)
......inorder(7)
........inorder(5)
7 16 20 ..........inorder(null)
5
..........inorder(null)
5 17 7
........inorder(null)
9
......inorder(null)
14
Recursion: inorder
14 ..inorder(15)
....inorder(null)
15
4 15 ....inorder(18)
......inorder(16)
........inorder(null)
3 9 18 16
........inorder(17)
7 ..........inorder(null)
16 20
17
..........inorder(null)
5 17 18
......inorder(20)
........inorder(null)
20
........inorder(null)
Non Recursive Traversal
The stack will be used to store the tree nodes in the appropriate
order.
Here, for example, is the routine for inorder traversal that uses
a stack.
Non Recursive Traversal
if( !stack.empty() )
{
p = stack.pop();
cout << *(p->getInfo()) << " ";
// go back & traverse right subtree
p = p->getRight();
}
} while ( !stack.empty() || p != NULL );
}
Non Recursive Traversal
if( !stack.empty() )
{
p = stack.pop();
cout << *(p->getInfo()) << " ";
// go back & traverse right subtree
p = p->getRight();
}
} while ( !stack.empty() || p != NULL );
}
Non Recursive Traversal
if( !stack.empty() )
{
p = stack.pop();
cout << *(p->getInfo()) << " ";
// go back & traverse right subtree
p = p->getRight();
}
} while ( !stack.empty() || p != NULL );
}
Non Recursive Traversal
if( !stack.empty() )
{
p = stack.pop();
cout << *(p->getInfo()) << " ";
// go back & traverse right subtree
p = p->getRight();
}
} while ( !stack.empty() || p != NULL );
}
Nonrecursive Inorder
push(14)
14 ..push(4)
....push(3)
3
4
4 15 ..push(9)
....push(7)
......push(5)
5
3 9 18 7
9
14
7 16 20 push(15)
15
push(18)
..push(16)
5 17 16
..push(17)
17
18
push(20)
20
Traversal Trace
recursive inorder
nonrecursive inorder
inorder(14)
..inorder(4)
push(14)
....inorder(3)
..push(4)
3
....push(3)
4
3
..inorder(9)
4
....inorder(7)
..push(9)
....push(7)
......inorder(5)
......push(5)
5
5
7
7
9
9
14
14
inorder(15)
push(15)
15 15
inorder(18) push(18)
..inorder(16) ..push(16)
16 16
..inorder(17) ..push(17)
17 17
18 18
inorder(20) push(20)
20 20
Traversal Trace
recursive inorder
nonrecursive inorder
inorder(14)
..inorder(4)
push(14)
....inorder(3)
..push(4)
3
....push(3)
4
3
..inorder(9)
4
....inorder(7)
..push(9)
....push(7)
......inorder(5)
......push(5)
5
5
7
7
9
9
14
14
inorder(15)
push(15)
15 15
inorder(18) push(18)
..inorder(16) ..push(16)
16 16
..inorder(17) ..push(17)
17 17
18 18
inorder(20) push(20)
20 20
Traversal Trace
recursive inorder
nonrecursive inorder
inorder(14)
..inorder(4)
push(14)
....inorder(3)
..push(4)
3
....push(3)
4
3
..inorder(9)
4
....inorder(7)
..push(9)
....push(7)
......inorder(5)
......push(5)
5
5
7
7
9
9
14
14
inorder(15)
push(15)
15 15
inorder(18) push(18)
..inorder(16) ..push(16)
16 16
..inorder(17) ..push(17)
17 17
18 18
inorder(20) push(20)
20 20
Traversal Trace
recursive inorder
nonrecursive inorder
inorder(14)
..inorder(4)
push(14)
....inorder(3)
..push(4)
3
....push(3)
4
3
..inorder(9)
4
....inorder(7)
..push(9)
....push(7)
......inorder(5)
......push(5)
5
5
7
7
9
9
14
14
inorder(15)
push(15)
15 15
inorder(18) push(18)
..inorder(16) ..push(16)
16 16
..inorder(17) ..push(17)
17 17
18 18
inorder(20) push(20)
20 20
Level-order Traversal
There is yet another way of traversing a binary tree that is not related to
recursive traversal procedures discussed previously.
In level-order traversal, we visit the nodes at each level before proceeding
to the next level.
At each level, we visit the nodes in a left-to-right order.
Level-order Traversal
14
4 15
3 9 18
7 16 20
5 17
Level-order: 14 4 15 3 9 18 7 16 20 5 17
Level-order Traversal
There is yet another way of traversing a binary tree that is not related to
recursive traversal procedures discussed previously.
In level-order traversal, we visit the nodes at each level before proceeding
to the next level.
At each level, we visit the nodes in a left-to-right order.
Level-order Traversal
14
4 15
3 9 18
7 16 20
5 17
Level-order: 14 4 15 3 9 18 7 16 20 5 17
Level-order Traversal
14
4 15
3 9 18
7 16 20
5 17
Queue: 14
Output:
Level-order Traversal
14
4 15
3 9 18
7 16 20
5 17
Queue: 4 15
Output: 14
Level-order Traversal
14
4 15
3 9 18
7 16 20
5 17
Queue: 15 3 9
Output: 14 4
Level-order Traversal
14
4 15
3 9 18
7 16 20
5 17
Queue: 3 9 18
Output: 14 4 15
Level-order Traversal
14
4 15
3 9 18
7 16 20
5 17
Queue: 9 18
Output: 14 4 15 3
Level-order Traversal
14
4 15
3 9 18
7 16 20
5 17
Queue: 18 7
Output: 14 4 15 3 9
Level-order Traversal
14
4 15
3 9 18
7 16 20
5 17
Queue: 7 16 20
Output: 14 4 15 3 9 18
Level-order Traversal
14
4 15
3 9 18
7 16 20
5 17
Queue: 16 20 5
Output: 14 4 15 3 9 18 7
Level-order Traversal
14
4 15
3 9 18
7 16 20
5 17
Queue: 20 5 17
Output: 14 4 15 3 9 18 7 16
Level-order Traversal
14
4 15
3 9 18
7 16 20
5 17
Queue: 5 17
Output: 14 4 15 3 9 18 7 16 20
Level-order Traversal
14
4 15
3 9 18
7 16 20
5 17
Queue: 17
Output: 14 4 15 3 9 18 7 16 20 5
Level-order Traversal
14
4 15
3 9 18
7 16 20
5 17
Queue:
Output: 14 4 15 3 9 18 7 16 20 5 17
Storing other Type of Data
The examples of binary trees so far have been storing integer
data in the tree node.
void wordTree()
{
TreeNode<char>* root = new TreeNode<char>();
static char* word[] = "babble", "fable", "jacket",
"backup", "eagle","daily","gain","bandit","abandon",
"abash","accuse","economy","adhere","advise","cease",
"debunk","feeder","genius","fetch","chain", NULL};
root->setInfo( word[0] );
void wordTree()
{
TreeNode<char>* root = new TreeNode<char>();
static char* word[] = "babble", "fable", "jacket",
"backup", "eagle","daily","gain","bandit","abandon",
"abash","accuse","economy","adhere","advise","cease",
"debunk","feeder","genius","fetch","chain", NULL};
root->setInfo( word[0] );
void wordTree()
{
TreeNode<char>* root = new TreeNode<char>();
static char* word[] = "babble", "fable", "jacket",
"backup", "eagle","daily","gain","bandit","abandon",
"abash","accuse","economy","adhere","advise","cease",
"debunk","feeder","genius","fetch","chain", NULL};
root->setInfo( word[0] );
void wordTree()
{
TreeNode<char>* root = new TreeNode<char>();
static char* word[] = "babble", "fable", "jacket",
"backup", "eagle","daily","gain","bandit","abandon",
"abash","accuse","economy","adhere","advise","cease",
"debunk","feeder","genius","fetch","chain", NULL};
root->setInfo( word[0] );
for(i=1; word[i]; i++ )
insert(root, word[i] );
inorder( root ); cout << endl;
}
Binary Search Tree with Strings
void wordTree()
{
TreeNode<char>* root = new TreeNode<char>();
static char* word[] = "babble", "fable", "jacket",
"backup", "eagle","daily","gain","bandit","abandon",
"abash","accuse","economy","adhere","advise","cease",
"debunk","feeder","genius","fetch","chain", NULL};
root->setInfo( word[0] );
2 8
1 4
3
Deleting a node in BST
6 6
2 8 2 8
1 4 1 4
3 3
Deleting a node in BST
6 6 6
2 8 2 8 2 8
1 4 1 4 1 3
3 3
Deleting a node in BST
The complicated case is when the node to be deleted has both left and right
subtrees.
The strategy is to replace the data of this node with the smallest data of the
right subtree and recursively delete that node.
Deleting a node in BST
2 8
1 5
4
Inorder
successor
Deleting a node in BST
4
Inorder
successor
Deleting a node in BST
6 6
2 8 3 8
1 5 1 5
3 3
4 4
Deleting a node in BST
6 6 6
2 8 3 8 3 8
1 5 1 5 1 5
3 3 3
4 4 4
Deleting a node in BST
Delete(2)
6 6
3 8 3 8
1 5 1 5
3 4