Professional Documents
Culture Documents
A. K. Siromoni
An AVL tree is a type of balanced binary search tree. Named after their
inventors, Adelson,Velskii and Landis, they were the first dynamically balanced
trees to be proposed.
An AVL tree is a binary search tree which has the following properties:
Data : 3 2 1 4 6 7 9 10 11 13 12
0
3
1
3
0 2
3
2
1 Imbalanced as node 3 is having
2
balancing factor -2
Balanced 0
1
Imbalancing in AVL Tree
2
A 2
A
1
B -1
0 B
0
C
C
-2
-2
A A
-1 1
B B
0 0
C C
p Left Rotation
A
q = right (p) // q = &B
B q Hold = left (q ) // Hold = &Y
left (q ) = p // &B -> left = A
right (p) = Hold //right (p) = &Y
Y C right (f) / left(f) = q //&X -> left = &B
Hold = Y A p C
B q
3. left (q ) = p // &B -> left = A
Y A p C
B q
p Right Rotation
A
q = left (p) // q = &B
B q Hold = right (q ) // Hold = &Y
right (q ) = p // &B -> right = A
left (p) = Hold //left (p) = &Y
C Y right (f) / left(f) = q //&X -> left = &B
Hold = Y C A p
B q
3. right (q ) = p // &B -> right = A
Y
B q C A p
Data : 3 2 1 4 6 7 9 10 11 13 12
0
3
1
3
0 2 3
2
1 Imbalanced as node 3 is having
2
balancing factor 2
Balanced 0
1
0
After Right Rotation 2
0 0
1 3
AVL Tree Insertion
Data : 3 2 1 4 6 7 9 10 11 13 12
2 0 -1 -2
2 2
0 0 0 -1 0 -2
1 3 1 3 1 3
0 -1
4 4
-1
2 0
6
0 0
1 4
0 Imbalanced, Rotation should be given
0 with respect to nearest ancestor
3 6
AVL Tree Insertion
Data : 3 2 1 4 6 7 9 10 11 13 12
0
-2 4
2 Imbalanced again, Rotation should be
0 given with respect to nearest ancestor 0 -1
-1 2 6
1 4 0 0 0
0 -1 1 3
3 6 7
7 0 -1
4
0 -2
2 6
0 0
1 -1
3 7
0
Imbalanced again, Rotation should be 9
given with respect to nearest ancestor
AVL Tree Insertion
Data : 3 2 1 4 6 7 9 10 11 13 12
Data : 3 2 1 4 6 7 9 10 11 13 12
-2 -1
4 4
0 0
-2 -1
2 2 7
7
0 0 0 0
0
1 -2 1 3
3 6 9 6 10
-1 0 0
10 9 11
0
11
AVL Tree Insertion
Data : 3 2 1 4 6 7 9 10 11 13 12
Data : 3 2 1 4 6 7 9 10 11 13 12
0 0
12 13
AVL Tree Insertion
Data : 3 2 1 4 6 7 9 10 11 13 12
-1
4
0
0
2 10
0
0
1 3
0
0
12
7
0 0
0 11 13
6
9
Advantages of AVL Tree
For General BST
14, 77, 50, 45, 12, 17, 78, 81, 66, 55, 89, 52
3. https://www.youtube.com/watch?v=-9sHvAnLN_w
RobEdwardsSDSU – AVL 1 Introduction
4. https://www.youtube.com/watch?v=7m94k2Qhg68
RobEdwardsSDSU - AVL Tree 7 complete example of adding
data to an AVL tree