Binary Search Tree
Binary Search Tree
y in left subtree of x,
then key[y] key[x].
18 28 190 213
y in right subtree of
x, then key[y] key[x].
12 24 27
Binary Search Trees
• Operations On BST.
Search, Minimum, Maximum, Predecessor, Successor, Insert,
and Delete.
B C
D E G F
H I J K L M N O
Worst case
Best case
Inorder Traversal
The binary-search-tree property allows the keys of a binary search tree to be printed, in
(monotonically increasing) order, recursively.
Inorder-Tree-Walk (x)
1. if x NIL 56
4. Inorder-Tree-Walk(right[p]) 12 24 27
56
26 200
18 28 190 213
If (data<root->data)
12 24 27
return (Find ( root->left, data));
else if ( data>root->data)
return (Find ( root->right, data));
return root;
}
Finding Min & Max
The binary-search-tree property guarantees that:
» The minimum is located at the left-most node.
» The maximum is located at the right-most node.
Struct Tree *FindMin(Struct Tree *root) Struct Tree *FindMax(Struct Tree *root)
{ {
if(root ==Null) If(root ==Null)
return Null; Return Null;
else If (root->left==Null) Else If (root->right==Null)
return root; Return root;
else return FindMin(root->left); Else return FindMax(root->right);
} }
Insertion in a Binary Search Tree
13, 3, 4, 12, 14, 10, 5, 1, 8, 2, 7, 9, 11, 6, 18
13 Root
13, 3, 4, 12, 14, 10, 5, 1, 8, 2, 7, 9, 11, 6, 18
13
3
13, 3, 4, 12, 14, 10, 5, 1, 8, 2, 7, 9, 11, 6, 18
13
4
13, 3, 4, 12, 14, 10, 5, 1, 8, 2, 7, 9, 11, 6, 18
13
12
13, 3, 4, 12, 14, 10, 5, 1, 8, 2, 7, 9, 11, 6, 18
13
14
3
12
13, 3, 4, 12, 14, 10, 5, 1, 8, 2, 7, 9, 11, 6, 18
13
14
3
12
10
13, 3, 4, 12, 14, 10, 5, 1, 8, 2, 7, 9, 11, 6, 18
13
14
3
12
10
5
13, 3, 4, 12, 14, 10, 5, 1, 8, 2, 7, 9, 11, 6, 18
13
14
3
1 4
12
10
5
13, 3, 4, 12, 14, 10, 5, 1, 8, 2, 7, 9, 11, 6, 18
13
14
3
1 4
12
10
8
13, 3, 4, 12, 14, 10, 5, 1, 8, 2, 7, 9, 11, 6, 18
13
14
3
1 4
2 12
10
8
13, 3, 4, 12, 14, 10, 5, 1, 8, 2, 7, 9, 11, 6, 18
13
14
3
1 4
2 12
10
7
13, 3, 4, 12, 14, 10, 5, 1, 8, 2, 7, 9, 11, 6, 18
13
14
3
1 4
2 12
10
7 9
13, 3, 4, 12, 14, 10, 5, 1, 8, 2, 7, 9, 11, 6, 18
13
14
3
1 4
2 12
10
11
5
7 9
13, 3, 4, 12, 14, 10, 5, 1, 8, 2, 7, 9, 11, 6, 18
13
14
3
1 4
2 12
10
11
5
7 9
6
13, 3, 4, 12, 14, 10, 5, 1, 8, 2, 7, 9, 11, 6, 18
13
14
3
18
1 4
2 12
10
11
5
7 9
6
BST Insertion – Pseudocode
struct Tree *insert(struct Tree *root, int data)
{
56 If(root ==Null)
{
26 200 root=struct Tree* malloc(sizeof(struct Tree ));
root->data=data;
18 28 190 213 root->left=root->right=Null;
}
12 24 27
else if(data<root->data)
root->left=insert( root->left, data);
else if (data>root->data)
root->right=insert( root->right, data);
}
Return root;
}
Exercise: Sorting Using BSTs
Sort (A)
for i 1 to n
do tree-insert(A[i])
inorder-tree-walk(root)
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
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
Removal in BST: Example
7 6
5 9 5 9
4 6 8 10 4 8 10
Deletion – Pseudocode
Simple
Efficient
Dynamic
Keys may be long and the run time may increase much.
Improvements of BST
AVL trees:
Search,Insertion, and Deletion:
O(logN)
Creating the tree – O(N)