Professional Documents
Culture Documents
Tree Part-3
Height of a Tree
The height of a tree is the length of the path from the root to the deepest node (leaf node) in
the tree.
Counting number of edges between root node and furthest leaf
Depth – 1 Depth – 1
Depth – 2 Depth – 2
Depth – 3
Depth – 4 Depth – 4
Height – 1 Height – 2
B C Depth – 1 B C B C
Depth – 1
D E F D E E F G
D
Height – 0 Height – 0 Height – 0
Depth – 2 Depth – 2 Depth – 2
G F G
Height – 0
Depth – 3
35
21
21 48
29
Hops = 5
35
13 29 79
48
24 Hops = 2
79
50 20
40 30
30
40
20
50
B R
B B B
R
B L
R
B
B R
B
J B
K
Right
Rotation
L’ B L
K Z X B J
B B’
L’ B
X Y Y B N B Y B Z
B B’
N B’
Critical Node L 13 B 7
Right
Rotation
L’ B 7 B’ B 15 L 5 B 13
L’ B 5 B’ B 10 10 B 3 B 10 B 15
3 B’
Insert node 3
Non-Linear Data Structure (Tree Part-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 R Y B
Left
B’ B T1 Y B R’ Rotation B X T3 R
B’ B T2 T3 B R’ T2 B T1 T2 B n B
n B’
L’ B 5 B’B 10 10
10 15
3 B
B B
3 B’
B’ B T2 T3 B R’
T2 B T1 T2 B n B
n B’
R 50 Critical Node
70 B
Left
B’B 40 70 B R’ Rotation of
Node 50 B 50 80 R
B’ B 60 80 B R’
60 B 40 B 60 B 90
Insert 90 90 B’
Non-Linear Data Structure (Tree Part-3) 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 B
Right Rotation
Left Rotation
B K B Z of Y Z
of
Left Child (K)
Parent (J) L K J B
B X R Y K n
n
B X B n B Z
B n X
Non-Linear Data Structure (Tree Part-3) 18
Insertion into Right sub-tree of node’s Left child
Case - 2
12 12 L 12
Left Rotation Right Rotation
of Node 4 of Node 8
Critical Node 8 16 8 16 B 6 16 L
R 4 B 10 14
6 10 14 B 4 8 R B 14
B 2 L 6
4
B 2 B 5 B 10
B 5 5
2 5
10 L 15 B 7 13 B 3 B 7 B 15
7
7 B 15
B 5 B 2
Insert 4 Insert 1
5 Critical Node B 3
Critical Node 6
B 5 L 3 B 6 L 2 B 5
L 5 2
Case 1 B 4 B 6 L 2 B 4 Case 1 B 1 B 4 B 6
Right Rotation Right Rotation
B 4 of Node 6 B 1 4 of Node 5
B 1 B’ 13 26
Insert 44
Case 2: Left Right Rotation
Left Rotation of Left Child 1 64
Critical Node 64 B 44
Followed By
R 1 Right Rotation of Parent 64 44
Right Rotation B 1 B 64
B 44 1 of Parent 64
Left Rotation of Left Child 1
B’ 98 110
Insert 85
R 44
B 13 L 98
B 1 B 26 R 64 B 110
B 85
Non-Linear Data Structure (Tree Part-3) 23
Construct AVL Search Tree
Construct AVL Search tree by inserting following elements in order of
their occurrence 60,73,75,76,79,81,82,300,0,5,73
Insert 60 Insert 76 ,79 Insert 82
B 60 R 73 76 Critical B 76
R 73 Case 4
Left Rotation 73 79 Case 4 B 73 B 81
Insert 73 B 60 75 R Critical of Node 75 B 60 B 76 Left Rotation
60 75 R 81 of Node 79 60 75 79 82
R 60 B 76 R’ B 75 B 79
B B B B
B 73 79 B’ B 82
Insert 75 Insert 81
B 76
B 73 Critical Node
60 Critical Node
73 B 73 R 79
Case 4 B 60 76 R Case 4
R 73 Left Rotation Left Rotation
of Node 60 60 75 of Node 73
B 60 B 75 B 81
B B B 75 R 79
B 75
75
B 81
Non-Linear Data Structure (Tree Part-3) 24
Construct AVL Search Tree
Construct AVL Search tree by inserting following elements in order of
their occurrence 60,73,75,76,79,81,82,300,0,5,73
Insert 300 , 0 Insert 5 Case 2: Left Right Rotation,
76 Critical Left Rotation of Left Child 0, 76
76 B
Followed By
73 81 Right Rotation of Parent 60 73 81
L 73 81 R
60 75 79 82 5 75 79 82
L 60 75 79 82 R
B B 0 60
R 0 300 300
0 B B 300
B 5
Insert 73
50 , 20 , 60 , 10 , 8 , 15 , 32 , 46 , 11 , 48
21 , 26 , 30 , 9 , 4 , 14 , 28 , 18 , 15 , 10 , 2 , 3 , 7
28 B
Make sure that the given tree remains AVL after every deletion
R 23 B 32
B 22 R 26 R 30 R 34
B 27 B 31 B 36
27 31 36
27 36 B
In-Order Traversal
22, 23, 26, 27, 28, 30, 31, 32, 34, 36 Case 4:
Left Rotation of
Delete 28 Node 32
28 B
30 L 31
R 23 32 R 23 R 34 B
B 22 R 26 30
31 R R 34 32 B 36 B
B 22 R 26
27 31 36 27 B
B B B
Delete 74
74 Critical
75 B
13
Case 1: Right Rotation
L 13 89 B
75 of Node 75 L 10 75 B
L 10 28 B 89
28 89 B
B 5 B 28
B 5
12 45
18
Commonly, weight balanced trees, the nodes may have weight equal to number of children
P 23
20 21
E T
K 15 S V
14 13
11 G 10 M
M 2
P
G 4 T 3 D G T
M L Z
D 4 L 5 P 6 Z 1
Ordered Tree
P 6
L 5 T 3
G 4 M 2 Z 1
D 4
10, 15, 20
2, 3 11, 14 16 21
4 – way Tree
No of Keys = 3
K1, K2, K3
No of Ways or children = 4
P0 P1 P2 Pn-1 Pn
P0 P1 P2 Pn-1 Pn
K0, K1, K2, ……… ,Kn-1 are keys stored in the node
Sub-Trees are pointed by P0, P1, P2, ……… ,Pn then
K0 >= all keys of sub-tree P0
………..
………..
Kn-1 < all keys of sub-tree Pn Non-Linear Data Structure (Tree Part-3) 39
Multiway Search Tree (B - Tree)
This node has capacity of 7 keys (K1 – K7) and 8 children (C1 – C8)
C1 K1 C2 K2 C3 K3 C4 K4 C5 K5 C6 K6 C7 K7 C8
20
30 70
8, 25 40, 50 76, 88
P
5, 10, 15, 20 1, 5, 9, 11
Insert - 38
Insert 7
Overflow
20
6, 10 30, 40
6 6, 11 6, 11
Overflow Overflow
1,2,5 7,10,11,12,13 1,2,5 7,10 12,13 1,2,5 7,10 12,13,16,20,24
Insert 3,4
3, 6, 11, 16
6, 11, 16 6, 11, 16
Overflow
1,2 4,5 7,10 12,13 20,24
1,2,5 7,10 12,13 20,24 1,2, 3,4,5 7,10 12,13 20,24
Insert 25
3, 6, 11, 16 11
Overflow
1,2 4,5 7,10 12, 13, 14 18, 19, 20, 24, 25 3, 6 16, 20
3, 6, 11, 16, 20 Overflow 1,2 4,5 7,10 12, 13, 14 18, 19 24, 25