Binary Search Tree
Binary Search Tree
S.Bharathiraja,
Associate Professor,
School of Computing Science and Engineering,
VIT-Chennai.
A Taxonomy of Trees
General Trees – any number of children / node
• the values in all nodes in the left subtree of a node are less than
41 89
34 56 72 95
Organization Rule for BST (Not a BST)
63
34 95
41 56 72 89
Binary Tree
struct searchtree
{
short int Element; // Data Field
node *right, *left; // Address Field
};
BST Operations: Search
Searching in the BST
method search(key)
• implements the binary search based on comparison of the items
in the tree
if (T!=Null)
while(T->Left!=Null)
T=T->Left; 2000 10 3000
return T;
T
}
4000 5 Null 5000 12 6000
if (T!=Null)
while(T->Right!=Null)
T=T->Right; 2000 10 3000
return T;
T
}
4000 5 Null 5000 12 6000
5 9 10 > 9
4 6 8 10
Insertion in BST - Pseudocode
if tree is empty
create a root node with the new key
else
compare key with the top node
if key = node key
replace the node with the new value
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
Insertion into a BST
2000 10 3000
else
if(x<T->Element) 2000 3000
T->Left=Insert(X,T->Left);
Null 3 Null Null 11 Null Null 13 Null
else
if(x>T->Element) 4000 5000 6000
T->Right=Insert(X,T->Right);
/*else x is in the Tree already; we Will do nothing */
return(T);
}
Write non-recursive algorithm to perform insertion in a BST
For the following data’s form a Binary Search tree by
doing insertion operation. Show the step by step insertion
operation.
12 , 4 , 6 , 5 , 3 , 14 , 13 , 23 , 15
12
14
4
3 6 13 23
5 15
BST Shapes
17
4 19
3 14 18
7 15
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
parent 7
cursor 5 9
4 6 8 10
7
Removing 4
replace the link in the
5 9
parent with null
6 8 10
Removal in BST: Example
Case 2: removing a node with 2 SUBTREES (Complete Node)
-(i)replace the node's value with the max value in the left subtree
(Inorder Predecessor)4->5->6->7->8->9->10
delete the max node in the left subtree
7 6
5 9 5 9
4 6 8 10
4 8 10
Removal in BST: Example
Case 2: removing a node with 2 SUBTREES
-(i)replace the node's value with the min value in the right subtree
(Inorder Successor)4->5->6->7->8->9->10
delete the min node in the right subtree
7 8
5 9 5 9
4 6 8 10
4 6 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
Case 4: removing a node with 1 EMPTY SUBTREE
the node has no right child:
link the parent of the node to the left (non-empty) subtree
Removing 5
parent
parent
7 cursor 7
cursor
5 9 5 9
4 8 10 4 8 10
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");