Unit-3 Non-Linear Data Structure Part-3 (Tree - III)
Unit-3 Non-Linear Data Structure Part-3 (Tree - III)
GTU # 3130702
Unit-3
Non-Linear Data Structure
Tree Part-3
Part 3
Height/Weight – Balanced Tree
Multiway Search Tree (B-Tree)
50 20
40 30
30
40
20
50
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
#3130702 (DS) Unit 3 – Non-Linear Data Structure (Tree
Dr. Pradyumansinh U. Jadeja 6
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 BR’ T2 B T1 T2 B n B
n B’
#3130702 (DS) Unit 3 – Non-Linear Data Structure (Tree
Dr. Pradyumansinh U. Jadeja 7
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 - 3 Case - 4
Case 4: Insertion into Right sub-tree of node’s Right
child
Single Left Rotation
L’ B 5 B’B 10 10
10 15
3 B
B B
3 B’
n B’
R 50 Critical Node
70 B
Left
B’B 40 70 BR’ Rotation of
Node 50 B 50 80 R
B’B 60 80 BR’
B 40 B 60 B 90
60
Insert 90 90 B’
#3130702 (DS) Unit 3 – Non-Linear Data Structure (Tree
Dr. Pradyumansinh U. Jadeja 12
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
B X R Y K n
n
B X B n B Z
B n X
Dr. Pradyumansinh U. Jadeja #3130702 (DS) Unit 3 – Non-Linear Data Structure (Tree Part-3) 13
Insertion into Right sub-tree of node’s Left child
Case - 2
12 12 L 12
Left Right
Rotation of Rotation of
Critical Node 8 16 8 16 B 6 16 L
Node 4 Node 8
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 B 3
Critical Node 6 Node
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 64 B 44
Node 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 13 L 98
B 1 B 26 R 64 B 110
B 85
#3130702 (DS) Unit 3 – Non-Linear Data Structure (Tree
Dr. Pradyumansinh U. Jadeja 18
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 RCritical 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
#3130702 (DS) Unit 3 – Non-Linear Data Structure (Tree
Dr. Pradyumansinh U. Jadeja 19
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
76 Critical Rotation, 76
76 B
Left Rotation of Left Child
73 81 0, 73 81
L 73 81 R
Followed By
60 75 79 82 Right Rotation of Parent 5 75 79 82
L 60 75 79 82 R
60
B B 0 60
R 0 300 300
0 B B 300
B 5
Insert 73
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 31 R R 34
30 32 B 36 B
B 22 R 26
27 31 36 27 B
B B B
#3130702 (DS) Unit 3 – Non-Linear Data Structure (Tree
Dr. Pradyumansinh U. Jadeja 22
Deleting node from AVL Tree
Delete 73, 74 Delete 73
73
L 74
73
13 75
L 13 75 R
10 28 74 89
L 10 B 28 74 89 B
5 In-Order Traversal
5, 10, 13, 28, 73, 74, 75, 89 5 B
Delete 74
74 Critical
75 B
13
Case 1: Right
L 13 75 B Rotation of Node 75 L 10
89 75 B
L 10 28 B 89
28 89 B
B 5 B 28
B 5
P 23
20 21
E T
K 15 S V
14 13
11 G 10 M
M2
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 M2 Z 1
D 4
#3130702 (DS) Unit 3 – Non-Linear Data Structure (Tree
Dr. Pradyumansinh U. Jadeja 26
Multiway Search Tree (B - Tree)
The nodes in a binary tree like AVL tree contains only one record
AVL tree is commonly stored in primary memory
In database applications where huge volume of data is handled, the search tree can not be
accommodated in primary memory
B-Trees are primarily meant for secondary storage
B-Tree is a M-way tree which can have maximum of M Children
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
………..
………..
Dr.KPradyumansinh
n-1 < all keys of sub-tree Pn
U. Jadeja
#3130702 (DS) Unit 3 – Non-Linear Data Structure (Tree
30
Multiway Search Tree (B - Tree)
20
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
18, 19, 20, 24,
1,2 4,5 7,10 12, 13, 14 3, 6 16, 20
25
3, 6, 11, 16, 20 Overflow 1,2 4,5 7,10 12, 13, 14 18, 19 24, 25
Dr. Pradyumansinh U. Jadeja #3130702 (DS) Unit 3 – Non-Linear Data Structure (Tree Part-3) 39
Data Structures (DS)
GTU # 3130702
Thank
You