AVL Tree
AVL Tree
AVL Tree
AVL Tree
03/26/2024 Md. Golam Moazzam, Dept. of CSE, JU 1
AVL Tree
AVL Tree
AVL tree is a type of Binary Search Tree (BST) in which at any given
node, absolute difference between heights of left sub-tree and right sub-
tree cannot be greater than 1. This property of the AVL tree helps to keep
the tree height balanced.
Height of left sub tree - Height of right sub tree = -1/0/1. This is known as
Balance Factor (BF).
The time complexity for searching an element in skewed BST is worst.
So, such trees are converted to AVL tree.
AVL tree has been named after their inventor Adelson, Velski and Landis.
14
19
11
53
17
7 12
60
13 16 20
4 8
17 17
11
2–1=1 2–2=0
14 14
0 – 1 = -1
0
1 1
17 17
11 11
0
0 0
53
7 7
0 1
14 14
0 – 1 = -1 0 – 1 = -1
0 -1
17 17
7 7
0 -1 0
0
0 0
53 53
11 11
4 4
0
13
17 14
7 0 – 1 = -1
-2 0
0 Critical 17
node 53 7
11
4 0
R -2 Critical node
1 0
53
11
13 4
R
0 -1
L
12 12
R
0
Right rotation between two
elements: 13 and 12 13
Left
rotation
14 14
0 – 1 = -1
0 – 1 = -1
Critical 1-3= -2
node 17 0
7 17
11
R 0 0
0
53 0
11 53
4
R 7 12
0 12 0 0
0
8 0 4 13
8
13
After Right
rotation 8 was to the left of 11
14
0
Now this is balanced.
0
53
11
0
0 -1 0
60
12 17
7
0 0 0
4 13
8
14
2-1=1 14
2-1=1
0
53 0
53
11
0 11
-1 -1 0
0 0
60 0 -1
17 60
7 12 17
7 12
0 0
0 0 0 0
0 0 0
19
4 8 13 16 19
4 8 13
14 14
3-1=2 3-1=2
0 Critical 0
53 53 Critical node
node
11 11
0 L
0
1-2= -1 L 1-2= -1
0 -1 0 -1
60 60
17 12 19
7 12 7
R
-1 L
0 0 0 0 0 0
0 0
16 19 4 8 13 17
4 8 13 20
0 0
Left rotation between 20 16
two elements: 17 and
19.
Now right
3-3=0
rotation.
14
3-1=2 14
0 0
53 Critical
0
11 node
19
L 0 11
1-2= -1 0
0 -1
60 -1 1
0
12 19 53
7
7 12 17
L
0 0 0 0
0 0 0 0 0
4 8 13 17 0
20
4 8 1 20 60
16
0 3
16
0 -1
-2 1-1=0
Critical
B B B node F
0 -1
R 0
0
F F
B I
R 0
For RR insertion I
Single left rotation
N Imbalanced
Balanced N
0
L
1-2 = -1
F F Balanced
0 0
0
B I L
B
R
0 0
L N
I
R
1-3 = -2 0
Critical
F node L
R 1-2 = -1 -1
0 0
Single left rotation
B L F N
R
0 -1 0 0
0
I N B I P
0
Imbalanced Balanced
P
3-2=1 3-2=1
L L
1-2=-1 -1 0 -1
F N F N
0 -1 0 1 -1 0
B I P B I P
0 0 0
Balanced J A Balanced J
3-2=1
3-2=1
L
L
0 -1
0 -1
F N
F N
0 0 0
1 0 0
B I P
B I P
0 0 0 0
0 0 0
A D H J
A H J
Balanced
Balanced
3-2=1 4-2=2
Critical
L L node
0 0 0
2-3=-1 L
N N
F F
R
0 0 0 1-2 = -1 0 0
0 0
I M P I M P
B B
0 0 0 0 0 0 0 -1
A D H J A D H J
0
Balanced Imbalanced K
4-2=2
Critical Right
L node L rotation
0 L 0
2-3=-1 L
N N
F I
R
1-2 = -1 0 0 L 0 0
0
I M P F J M P
B
0
0 0
0 0 0 -1
B H
A D J 0 K
H
0
0
A
D
K Imbalanced
For LR insertion
1. Left rotation: F, I
2. Then right rotation
3-3=0
2-2=0
2-1=1
L
F
0
0 -1
0
N
B H J
0 0 0
0 0
K M P
A D