DS Lecture Week 11
DS Lecture Week 11
2
Construct Binary Search Tree (BST)
Construct binary search tree for the following data
50 , 25 , 75 , 22 , 40 , 60 , 80 , 90 , 15 , 30
50
25 75
22 40 60 80
15 30 90
4
Delete node from Binary Search Tree
Delete node a
a Delete node a b
5
Delete node from BST
Delete node a
a C
b C
6
Balanced Tree
Binary Search Tree gives advantage of Fast Search, but sometimes in few cases we are not
able to get this advantage. E.g. look into worst case BST
Balanced binary trees are classified into two categories
Height Balanced Tree (AVL Tree)
Weight Balanced Tree Worst search time cases for Binary Search Tree
50 20
40 30
30
40
20
50
7
Height Balanced Tree (AVL Tree)
A tree is called AVL tree (Height Balanced Tree), if each node possessed one of the following
properties
A node is called left heavy, if the longest path in its left sub tree is one longer than the longest path of its
right sub tree (represented as 1).
A node is called right heavy, if the longest path in its right subtree is one longer than the longest path of its
left sub tree (represented as -1).
A node is called balanced, if the longest path in both the right and left sub-trees are equal (represented as
0).
0 -1
0 0 0
-1
0 1
-1
0
0 -1
0
Critical Node 1
J 0
K
Right
Rotation
1’ 0
K Z 1 X 0 J
0 0’
1’ 0
X Y Y 0 N 0 Y 0 Z
0 0’
N 0’
10
Right Rotation
a. Detach left child’s right sub-tree
b. Consider left child to be the new parent
c. Attach old parent onto right of new parent
d. Attach old left child’s old right sub-tree as left sub-tree of new right child
Critical Node 1 13 0 7
Right
Rotation
1’ 0 7 0’ 0 15 1 5 0 13
1’ 0 5 0’ 0 10 10 0 3 0 10 0 15
3 0’
Insert node 3
11
Left Rotation
a. Detach right child’s leaf sub-tree
b. Consider right child to be new parent
c. Attach old parent onto left of new parent
d. Attach old right child’s old left sub-tree as right sub-tree of new left child
Critical Node X -1 Y 0
Left
0’ 0 T1 Y 0 -1’ Rotation 0 X T3 -1
0’ 0 T2 T3 0 -1’ T2 0 T1 T2 0 n 0
n 0’
12
Select Rotation based on Insertion Position
Case 1: Insertion into Left sub-tree of nodes Left child Case - 1 Case - 2
Single Right Rotation
Case 2: Insertion into Right sub-tree of node’s Left child
Left Right Rotation
Case 3: Insertion into Left sub-tree of node’s Right child
Right Left Rotation
Case 4: Insertion into Right sub-tree of node’s Right child
Single Left Rotation
Case - 3 Case - 4
13
Insertion into Left sub-tree of nodes Left child
Case 1: If node becomes unbalanced after insertion of new node at Left sub-tree of nodes Left
child, then we need to perform Single Right Rotation of unbalanced node to balance the node
Right Rotation
a. Detach leaf child’s right sub-tree
b. Consider leaf child to be the new parent
c. Attach old parent onto right of new parent
d. Attach old leaf child’s old right sub-tree as leaf sub-tree of new right child
Case - 1
14
Insertion into Left sub-tree of nodes Left child
Critical Node
1 Case - 1
J
0
Right K
1’ 0 K Z 0 0’ Rotation
1 X 0 J
1’ 0 X Y 0 0’
Y Y
0 N 0 0 Z
N 0’
1’ 0 5 0’ 0 10 10
10 15
3 0
0 0
3 0’
15
Insertion into Right sub-tree of node’s Right child
Case 4: If node becomes unbalanced after insertion of new node at Right sub-tree of nodes
Right child, then we need to perform Single Left Rotation of unbalance node to balance the
node
Case - 4
Left Rotation
A. Detach right child’s leaf sub-tree
B. Consider right child to be new parent
C. Attach old parent onto left of new parent
D. Attach old right child’s old left sub-tree as right sub-tree of new left child
Single Left Rotation
of
unbalanced node
16
Insertion into Right sub-tree of node’s Right child
Critical Node X 0 Y 0 Case - 4
Left
0’ 0 Y 0 -1’ Rotation
T1
0 X T3 -1
0’ 0 T2 T3 0 -1’
T2 0 T1 T2 0 n 0
n 0’
0 50 Critical Node
70 0
Left
0’ B 40 70 0-1’ Rotation of
Node 50 0 50 80 -1
0’ 0 60 80 0 -1’
60 0 40 0 60 0 90
Insert 90 90 0’
17
Insertion into Right sub-tree of node’s Left child
Case 2: If node becomes unbalanced after insertion of new node at Right
sub-tree of node’s Left child, then we need to perform Left Right Rotation
for unbalanced node. Case - 2
Left Right Rotation
Left Right Rotation Left Rotation of Left Child
Left Rotation of Left Child followed by followed by
Right Rotation of Parent Right Rotation of Parent
Case - 2
J Critical Node J Y 0
Right Rotation
Left Rotation
0 K 0 Z of Y Z
of
Parent (J) 1 K J 0
Left Child (K)
0 X -1 Y K n
n
0 X 0 n 0 Z
0 n X
18
Insertion into Right sub-tree of node’s Left child
Case - 2
12 12 1 12
Left Rotation Right Rotation
of Node 4 of Node 8
Critical Node 8 16 8 16 0 6 16 1
-1 4 0 10 14
6 10 14 0 4 8 -1 0 14
0 2 1 6
4
0 2 0 5 0 10
0 5 5
2 5
10 1 15 0 7 13 0 3 0 7 0 15
7
7 0 15