Professional Documents
Culture Documents
Trees
• Linear access time of linked lists is prohibitive
– Does there exist any simple data structure for
which the running time of most operations
(search, insert, delete) is O(log N)?
Trees
• A tree is a collection of nodes
– The collection can be empty
– (recursive definition) If not empty, a tree consists
of a distinguished node r (the root), and zero or
more nonempty subtrees T1, T2, ...., Tk, each of
whose roots are connected by a directed edge
from r
Some Terminologies
34 41 56 63 72 89 95
0 1 2 3 4 5 6
34 41 56 72 89 95
0 1 2 4 5 6
72 95
34 56
4 6
0 2
Organization Rule for BST
• the values in all nodes in the left subtree of a node are less than
the node value
• the values in all nodes in the right subtree of a node are greater
than the node values
63
41 89
72 95
34 56
Binary Tree
struct bstnode {
int data;
struct bstnode *left, *right;
};
BST Operations: Search
Searching in the BST
method search(key)
• implements the binary search based on comparison of the
items
in the tree
7
10 > 9
5 9
4 6 8 10
Insertion in BST - Pseudocode
if tree is empty
create a root node with the new key
else if key > node key
compare key with the right subtree:
if subtree is empty create a leaf node
else add key in right subtree
else key < node key
compare key with the left subtree:
if the subtree is empty create a leaf node
else add key to the left subtree
Creating newnode into a BST: C code
9 67
3 14 36 94
1 7 44 97
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
P print(data) 22
R InOrderPrint(right
child)
9 67
3 14 36 94
1 7 44 97
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
P print(data) 22
R InOrderPrint(right
child)
9 67
3 14 36 94
1 7 44 97
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data)
R InOrderPrint(right
child)
9 67
3 14 36 94
1 7 44 97
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data)
R InOrderPrint(right
child)
9 67
3 14 36 94
1 7 44 97
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data)
R InOrderPrint(right
child)
L 9 67
3 14 36 94
1 7 44 97
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data)
R InOrderPrint(right
child)
L 9 67
3 14 36 94
1 7 44 97
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data)
R InOrderPrint(right
child)
L 9 67
L 3 14 36 94
1 7 44 97
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data)
R InOrderPrint(right
child)
L 9 67
L 3 14 36 94
1 7 44 97
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data)
R InOrderPrint(right
child)
L 9 67
L 3 14 36 94
L 1 7 44 97
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data)
R InOrderPrint(right
child)
L 9 67
L 3 14 36 94
L 1 7 44 97
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data)
R InOrderPrint(right
child)
L 9 67
Output: 1
L 3 14 36 94
L 1 7 44 97
P
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data)
R InOrderPrint(right
child)
L 9 67
Output: 1
L 3 14 36 94
L 1 7 44 97
P
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data)
R InOrderPrint(right
child)
L 9 67
Output: 1
L 3 14 36 94
L 1 7 44 97
P
R
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data)
R InOrderPrint(right
child)
L 9 67
Output: 1
L 3 14 36 94
L 1 7 44 97
P
R
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data)
R InOrderPrint(right
child)
L 9 67
Output: 1
L 3 14 36 94
L 1 7 44 97
P
R
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data)
R InOrderPrint(right
child)
L 9 67
Output: 1 3
L 3 14 36 94
P
L 1 7 44 97
P
R
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data)
R InOrderPrint(right
child)
L 9 67
Output: 1 3
L 3 14 36 94
P
L 1 7 44 97
P
R
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data)
R InOrderPrint(right
child)
L 9 67
Output: 1 3
L 3 14 36 94
P
R
L 1 7 44 97
P
R
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data)
R InOrderPrint(right
child)
L 9 67
Output: 1 3
L 3 14 36 94
P
R
L 1 7 44 97
P
R
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data)
R InOrderPrint(right
child)
L 9 67
Output: 1 3
L 3 14 36 94
P
R
L 1 L 7 44 97
P
R
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data)
R InOrderPrint(right
child)
L 9 67
Output: 1 3
L 3 14 36 94
P
R
L 1 L 7 44 97
P
R
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data)
R InOrderPrint(right
child)
L 9 67
Output: 1 3 7
L 3 14 36 94
P
R
L 1 L 7 44 97
P P
R
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data)
R InOrderPrint(right
child)
L 9 67
Output: 1 3 7
L 3 14 36 94
P
R
L 1 L 7 44 97
P P
R
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data)
R InOrderPrint(right
child)
L 9 67
Output: 1 3 7
L 3 14 36 94
P
R
L 1 L 7 44 97
P P
R R
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data)
R InOrderPrint(right
child)
L 9 67
Output: 1 3 7
L 3 14 36 94
P
R
L 1 L 7 44 97
P P
R R
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data)
R InOrderPrint(right
child)
L 9 67
Output: 1 3 7
L 3 14 36 94
P
R
L 1 L 7 44 97
P P
R R
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data)
R InOrderPrint(right
child)
L 9 67
Output: 1 3 7
L 3 14 36 94
P
R
L 1 L 7 44 97
P P
R R
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data)
R InOrderPrint(right
child)
L 9 67
Output: 1 3 7 9
P
L 3 14 36 94
P
R
L 1 L 7 44 97
P P
R R
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data)
R InOrderPrint(right
child)
L 9 67
Output: 1 3 7 9
P
L 3 14 36 94
P
R
L 1 L 7 44 97
P P
R R
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data)
R InOrderPrint(right
child)
L 9 67
Output: 1 3 7 9
P
R
L 3 14 36 94
P
R
L 1 L 7 44 97
P P
R R
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data)
R InOrderPrint(right
child)
L 9 67
Output: 1 3 7 9
P
R
L 3 14 36 94
P
R
L 1 L 7 44 97
P P
R R
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data)
R InOrderPrint(right
child)
L 9 67
Output: 1 3 7 9
P
R
L 3 L 14 36 94
P
R
L 1 L 7 44 97
P P
R R
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data)
R InOrderPrint(right
child)
L 9 67
Output: 1 3 7 9
P
R
L 3 L 14 36 94
P
R
L 1 L 7 44 97
P P
R R
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data)
R InOrderPrint(right
child)
L 9 67
Output: 1 3 7 9 14
P
R
L 3 L 14 36 94
P P
R
L 1 L 7 44 97
P P
R R
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data)
R InOrderPrint(right
child)
L 9 67
Output: 1 3 7 9 14
P
R
L 3 L 14 36 94
P P
R
L 1 L 7 44 97
P P
R R
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data)
R InOrderPrint(right
child)
L 9 67
Output: 1 3 7 9 14
P
R
L 3 L 14 36 94
P P
R R
L 1 L 7 44 97
P P
R R
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data)
R InOrderPrint(right
child)
L 9 67
Output: 1 3 7 9 14
P
R
L 3 L 14 36 94
P P
R R
L 1 L 7 44 97
P P
R R
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data)
R InOrderPrint(right
child)
L 9 67
Output: 1 3 7 9 14
P
R
L 3 L 14 36 94
P P
R R
L 1 L 7 44 97
P P
R R
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data)
R InOrderPrint(right
child)
L 9 67
Output: 1 3 7 9 14
P
R
L 3 L 14 36 94
P P
R R
L 1 L 7 44 97
P P
R R
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data) P
R InOrderPrint(right
child)
L 9 67
Output: 1 3 7 9 14 22
P
R
L 3 L 14 36 94
P P
R R
L 1 L 7 44 97
P P
R R
Continue?
Enough
Yes!
Already!
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data) P
R InOrderPrint(right
child)
L 9 67
Output: 1 3 7 9 14 22
P
R
L 3 L 14 36 94
P P
R R
L 1 L 7 44 97
P P
R R
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data) P
R InOrderPrint(right R
child)
L 9 67
Output: 1 3 7 9 14 22
P
R
L 3 L 14 36 94
P P
R R
L 1 L 7 44 97
P P
R R
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data) P
R InOrderPrint(right R
child)
L 9 67
Output: 1 3 7 9 14 22
P
R
L 3 L 14 36 94
P P
R R
L 1 L 7 44 97
P P
R R
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data) P
R InOrderPrint(right R
child)
L 9 L 67
Output: 1 3 7 9 14 22
P
R
L 3 L 14 36 94
P P
R R
L 1 L 7 44 97
P P
R R
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data) P
R InOrderPrint(right R
child)
L 9 L 67
Output: 1 3 7 9 14 22
P
R
L 3 L 14 36 94
P P
R R
L 1 L 7 44 97
P P
R R
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data) P
R InOrderPrint(right R
child)
L 9 L 67
Output: 1 3 7 9 14 22
P
R
L 3 L 14 L 36 94
P P
R R
L 1 L 7 44 97
P P
R R
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data) P
R InOrderPrint(right R
child)
L 9 L 67
Output: 1 3 7 9 14 22
P
R
L 3 L 14 L 36 94
P P
R R
L 1 L 7 44 97
P P
R R
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data) P
R InOrderPrint(right R
child)
L 9 L 67
Output: 1 3 7 9 14 22
P
36
R
L 3 L 14 L 36 94
P P P
R R
L 1 L 7 44 97
P P
R R
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data) P
R InOrderPrint(right R
child)
L 9 L 67
Output: 1 3 7 9 14 22
P
36
R
L 3 L 14 L 36 94
P P P
R R
L 1 L 7 44 97
P P
R R
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data) P
R InOrderPrint(right R
child)
L 9 L 67
Output: 1 3 7 9 14 22
P
36
R
L 3 L 14 L 36 94
P P P
R R R
L 1 L 7 44 97
P P
R R
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data) P
R InOrderPrint(right R
child)
L 9 L 67
Output: 1 3 7 9 14 22
P
36
R
L 3 L 14 L 36 94
P P P
R R R
L 1 L 7 44 97
P P
R R
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data) P
R InOrderPrint(right R
child)
L 9 L 67
Output: 1 3 7 9 14 22
P
36
R
L 3 L 14 L 36 94
P P P
R R R
L L L 44 97
1 7
P P
R R
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data) P
R InOrderPrint(right R
child)
L 9 L 67
Output: 1 3 7 9 14 22
P
36
R
L 3 L 14 L 36 94
P P P
R R R
L L L 44 97
1 7
P P
R R
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data) P
R InOrderPrint(right R
child)
L 9 L 67
Output: 1 3 7 9 14 22
P
36 44
R
L 3 L 14 L 36 94
P P P
R R R
L L L 44 97
1 7
P P P
R R
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data) P
R InOrderPrint(right R
child)
L 9 L 67
Output: 1 3 7 9 14 22
P
36 44
R
L 3 L 14 L 36 94
P P P
R R R
L L L 44 97
1 7
P P P
R R
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data) P
R InOrderPrint(right R
child)
L 9 L 67
Output: 1 3 7 9 14 22
P
36 44
R
L 3 L 14 L 36 94
P P P
R R R
L L L 44 97
1 7
P P P
R R R
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data) P
R InOrderPrint(right R
child)
L 9 L 67
Output: 1 3 7 9 14 22
P
36 44
R
L 3 L 14 L 36 94
P P P
R R R
L L L 44 97
1 7
P P P
R R R
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data) P
R InOrderPrint(right R
child)
L 9 L 67
Output: 1 3 7 9 14 22
P
36 44
R
L 3 L 14 L 36 94
P P P
R R R
L L L 44 97
1 7
P P P
R R R
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data) P
R InOrderPrint(right R
child)
L 9 L 67
Output: 1 3 7 9 14 22
P
36 44
R
L 3 L 14 L 36 94
P P P
R R R
L L L 44 97
1 7
P P P
R R R
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data) P
R InOrderPrint(right R
child)
L 9 L 67
Output: 1 3 7 9 14 22
P P
36 44 67
R
L 3 L 14 L 36 94
P P P
R R R
L L L 44 97
1 7
P P P
R R R
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data) P
R InOrderPrint(right R
child)
L 9 L 67
Output: 1 3 7 9 14 22
P P
36 44 67
R
L 3 L 14 L 36 94
P P P
R R R
L L L 44 97
1 7
P P P
R R R
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data) P
R InOrderPrint(right R
child)
L 9 L 67
Output: 1 3 7 9 14 22
P P
36 44 67
R R
L 3 L 14 L 36 94
P P P
R R R
L L L 44 97
1 7
P P P
R R R
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data) P
R InOrderPrint(right R
child)
L 9 L 67
Output: 1 3 7 9 14 22
P P
36 44 67
R R
L 3 L 14 L 36 94
P P P
R R R
L L L 44 97
1 7
P P P
R R R
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data) P
R InOrderPrint(right R
child)
L 9 L 67
Output: 1 3 7 9 14 22
P P
36 44 67
R R
L 3 L 14 L 36 L 94
P P P
R R R
L L L 44 97
1 7
P P P
R R R
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data) P
R InOrderPrint(right R
child)
L 9 L 67
Output: 1 3 7 9 14 22
P P
36 44 67
R R
L 3 L 14 L 36 L 94
P P P
R R R
L L L 44 97
1 7
P P P
R R R
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data) P
R InOrderPrint(right R
child)
L 9 L 67
Output: 1 3 7 9 14 22
P P
36 44 67 94
R R
L 3 L 14 L 36 L 94
P P P P
R R R
L L L 44 97
1 7
P P P
R R R
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data) P
R InOrderPrint(right R
child)
L 9 L 67
Output: 1 3 7 9 14 22
P P
36 44 67 94
R R
L 3 L 14 L 36 L 94
P P P P
R R R
L L L 44 97
1 7
P P P
R R R
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data) P
R InOrderPrint(right R
child)
L 9 L 67
Output: 1 3 7 9 14 22
P P
36 44 67 94
R R
L 3 L 14 L 36 L 94
P P P P
R R R R
L L L 44 97
1 7
P P P
R R R
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data) P
R InOrderPrint(right R
child)
L 9 L 67
Output: 1 3 7 9 14 22
P P
36 44 67 94
R R
L 3 L 14 L 36 L 94
P P P P
R R R R
L L L 44 97
1 7
P P P
R R R
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data) P
R InOrderPrint(right R
child)
L 9 L 67
Output: 1 3 7 9 14 22
P P
36 44 67 94
R R
L 3 L 14 L 36 L 94
P P P P
R R R R
L L L 44 L 97
1 7
P P P
R R R
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data) P
R InOrderPrint(right R
child)
L 9 L 67
Output: 1 3 7 9 14 22
P P
36 44 67 94
R R
L 3 L 14 L 36 L 94
P P P P
R R R R
L L L 44 L 97
1 7
P P P
R R R
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data) P
R InOrderPrint(right R
child)
L 9 L 67
Output: 1 3 7 9 14 22
P P
36 44 67 94 97
R R
L 3 L 14 L 36 L 94
P P P P
R R R R
L L L 44 L 97
1 7
P P P P
R R R
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data) P
R InOrderPrint(right R
child)
L 9 L 67
Output: 1 3 7 9 14 22
P P
36 44 67 94 97
R R
L 3 L 14 L 36 L 94
P P P P
R R R R
L L L 44 L 97
1 7
P P P P
R R R
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data) P
R InOrderPrint(right R
child)
L 9 L 67
Output: 1 3 7 9 14 22
P P
36 44 67 94 97
R R
L 3 L 14 L 36 L 94
P P P P
R R R R
L L L 44 L 97
1 7
P P P P
R R R R
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data) P
R InOrderPrint(right R
child)
L 9 L 67
Output: 1 3 7 9 14 22
P P
36 44 67 94 97
R R
L 3 L 14 L 36 L 94
P P P P
R R R R
L L L 44 L 97
1 7
P P P P
R R R R
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data) P
R InOrderPrint(right R
child)
L 9 L 67
Output: 1 3 7 9 14 22
P P
36 44 67 94 97
R R
L 3 L 14 L 36 L 94
P P P P
R R R R
L L L 44 L 97
1 7
P P P P
R R R R
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data) P
R InOrderPrint(right R
child)
L 9 L 67
Output: 1 3 7 9 14 22
P P
36 44 67 94 97
R R
L 3 L 14 L 36 L 94
P P P P
R R R R
L L L 44 L 97
1 7
P P P P
R R R R
Proc InOrderPrint(pointer)
root
pointer NOT NIL?
L InOrderPrint(left child)
L 22
P print(data) P
R InOrderPrint(right R
child)
L 9 L 67
Output: 1 3 7 9 14 22
P P
36 44 67 94 97
R R
L 3 L 14 L 36 L 94
P P P P
R R R R
L L L 44 L 97
1 7
P P P P
R R R R
Algorithm Example
root
. . .
InOrderPrint(root
) L 22
P
. . . R
L 9 L 67
Output: 1 3 7 9 14 22
P P
36 44 67 94 97
R R
L 3 L 14 L 36 L 94
P P P P
R R R R
L L L 44 L 97
1 7
P P P P
R R R R
Preorder Traversal
• Algorithm Preorder(tree)
1. Visit the root.
2. Traverse the left subtree, i.e., call
Preorder(left-subtree)
3. Traverse the right subtree, i.e., call
Preorder(right-subtree)
Preorder
void printPreorder(struct bstnode* node)
{
if (node == NULL)
return;
/* first print data of node */
printf("%d ", node->data);
/* then recur on left sutree */
printPreorder(node->left);
/* now recur on right subtree */
printPreorder(node->right);
}
Postorder Traversal
• Algorithm Postorder(tree)
1.Traverse the left subtree, i.e., call
Postorder(left-subtree)
2.2. Traverse the right subtree, i.e., call
Postorder(right-subtree)
3.3. Visit the root.
Postorder Traversl
void printPostorder(struct node* node)
{
if (node == NULL)
return;
// first recur on left subtree
printPostorder(node->left);
// then recur on right subtree
printPostorder(node->right);
// now deal with the node
printf("%d ", node->data);
}
BST Shapes
The order of supplying the data determines where it is placed in the BST , which
determines the shape of the BST
Create BSTs from the same set of data presented each time in a different
order:
a) 17 4 14 19 15 7 9 3 16 10
b) 9 10 17 4 3 7 14 16 15 19
c) 19 17 16 15 14 10 9 7 4 3 can you guess this shape?
BST Operations: Removal
removes a specified item from the BST and adjusts the tree
uses a binary search to locate the target item:
starting at the root it probes down the tree till it finds the target or reaches a leaf
node (target not in the tree)
Case 3:
Case 3.1:
if the node has no left child
- link the parent of the node
- to the right (non-empty) subtree
Case 3.2:
if the node has no right child
- link the parent of the target
- to the left (non-empty) subtree
Removal in BST: Example
parent
7
cursor
5 9
4 6 8 10
7
Removing 4
replace the link in the parent
with null 5 9
6 8 10
Removal in BST: Example
7 6
5 9 5 9
4 6 8 10 4 8 10
Removal in BST: Example
parent
parent
7 7
cursor
5 9 cursor 5 9
6 8 10 6 8 10
Removal in BST: Example
Removing 5
parent
parent
7 cursor 7
cursor
5 9 5 9
4 8 10 4 8 10
• Insert the following keys in empty binary
search tree:
17,13,21,10,24,23,15,27,4,11,25,16,26
Delete 4
Delete 10
Delete 13
Delete 17
Removal in BST: C code
struct node* deleteNode(struct bstnode* root, int data) else if (root->right == NULL)
{ {
struct node *temp = root->left;
if (root == NULL) return root;
// If the key to be deleted is smaller than the root's key, // then it lies in left
free(root);
subtree return temp;
if (data < root->data) }
root->left = deleteNode(root->left, data);
// If the key to be deleted is greater than the root's key, // then it lies in // node with two children: Get the inorder
right subtree successor (smallest in the right subtree)
else if (data> root->data) struct node* temp = minValueNode(root->right);
root->right = deleteNode(root->right, data);
// if key is same as root's key, then This is the node to be // Copy the inorder successor's content to this
deleted node
else root->key = temp->key;
{
// Delete the inorder successor
// node with only one child or no child root->right = deleteNode(root->right, temp-
if (root->left == NULL) >key);
{ }
struct node *temp = root->right; return root;
free(root); }
return temp;
}
Analysis of BST Operations
tree.insert ("E");
tree.insert ("C");
tree.insert ("D");
tree.insert ("A");
tree.insert ("H");
tree.insert ("F");
tree.insert ("K");
• Algorithm ?
• Running time ?
Better Search Trees