37-AVL Trees - Terminology and Concepts-05-11-2024
37-AVL Trees - Terminology and Concepts-05-11-2024
2. Double Rotation
1. Left Right Rotation(LR Rotation)
2. Right Left Rotation(RL Rotation)
Single Left Rotation (LL Rotation)
TREE is Balanced
Insert 2 BF=-1
1
BF=0
2
TREE is Balanced
Construction of AVL Tree (1 to 8)
Insert 3
BF=-2 BF=-2
1 1
BF=-1 BF=-1
2 2
BF=0 BF=0
3 3
TREE is imbalanced
BF=0 LL ROTATION
2
BF=0 BF=0
1 3 TREE is balanced
Construction of AVL Tree (1 to 8)
Insert 4
BF=-1
2
BF=0 BF=-1
1 3
BF=0
4
TREE is balanced
Construction of AVL Tree (1 to 8)
Insert 5
BF=-2
BF=-2
2 2
BF=-2
BF=-2 BF=0
3 1 3
1
BF=0 BF=-1
BF=-1
4 4
BF=0
BF=0
5 5
LL ROTATION AT 3
TREE is imbalanced
Construction of AVL Tree (1 to 8)
Result of Inserting 5
BF=-1
2
BF=0 BF=0
1 4
BF=0 BF=0
3 5
TREE is balanced
Construction of AVL Tree (1 to 8)
Insert 6
BF=-2
2
BF=0 BF=-1
1 4
BF=0 BF=-1
3 5
BF=0
6
TREE is imbalanced
Construction of AVL Tree (1 to 8)
Rotation process at Inserting 6
BF=-2
2
BF=0 BF=-1
1 4
BF=0 BF=-1
3 5
?? BF=0
6
LL Rotation at 2
Construction of AVL Tree (1 to 8)
Result of Inserting 6
BF=0
4
BF=0 BF=-1
2 5
TREE is balanced
Construction of AVL Tree (1 to 8)
Insert 7
BF=-1
4
BF=0 BF=-2
2 5
TREE is imbalanced
Construction of AVL Tree (1 to 8)
Rotation Process when Inserting 7
BF=-1
4
BF=0 BF=-2
2 5
LL Rotation at 5
Construction of AVL Tree (1 to 8)
Result of Inserting 7
BF=0
4
BF=0 BF=0
2 6
TREE is balanced
Construction of AVL Tree (1 to 8)
Insert 8
BF=-1
4
BF=0 BF=-1
2 6
TREE is balanced
Deletion Operation
Similar to deletion operation in BST
Every deletion operation, we need to
check with the Balance Factor condition
If the tree is balanced after deletion go
for next operation otherwise perform
suitable rotation to make the tree
Balanced.