You are on page 1of 24

Data Structures

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.

03/26/2024 Md. Golam Moazzam, Dept. of CSE, JU 2


AVL Tree
AVL Tree
AVL trees are height balancing binary search tree. It checks the height of
the left and the right sub-trees and assures that the difference is not more
than 1. This difference is called the Balance Factor.
4 rotations:
LL, RR – Single Rotation
LR, RL – Double Rotation

Both AVL Trees

03/26/2024 Md. Golam Moazzam, Dept. of CSE, JU 3


AVL Tree
Example 1:
Construct AVL tree by inserting the following data:
14, 17, 11, 7, 53, 4, 13, 12, 8, 60, 19, 16, 20

14

19
11

53
17
7 12

60
13 16 20
4 8

Final AVL Tree

03/26/2024 Md. Golam Moazzam, Dept. of CSE, JU 4


AVL Tree
Construct AVL tree by inserting the following data:
14, 17, 11, 7, 53, 4, 13, 12, 8, 60, 19, 16, 20
Construction:
0 – 1 = -1 1–1=0
Balance 0
Factor
14 14
14
0 0
0

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

03/26/2024 Md. Golam Moazzam, Dept. of CSE, JU 5


AVL Tree
Construct AVL tree by inserting the following data:
14, 17, 11, 7, 53, 4, 13, 12, 8, 60, 19, 16, 20
Construction:

14 For AVL tree,


0 – 1 = -1
2
At each node Balance Factor (BF) is - 1/0/1
Critical 17 Otherwise, the node is called critical node.
node 11
L
0
1
53
7
L
0

03/26/2024 Md. Golam Moazzam, Dept. of CSE, JU 6


AVL Tree
Construct AVL tree by inserting the following data:
14, 17, 11, 7, 53, 4, 13, 12, 8, 60, 19, 16, 20
Construction:

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

03/26/2024 Md. Golam Moazzam, Dept. of CSE, JU 7


AVL Tree
Construct AVL tree by inserting the following data:
14, 17, 11, 7, 53, 4, 13, 12, 8, 60, 19, 16, 20
Construction:
For R-L insertion,
-It needs 2 rotations:
First right rotation and then
14
0 – 1 = -1 left.

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

03/26/2024 Md. Golam Moazzam, Dept. of CSE, JU 8


AVL Tree
Construct AVL tree by inserting the following data:
14, 17, 11, 7, 53, 4, 13, 12, 8, 60, 19, 16, 20
Construction:
3-2=1
14
14 0 – 1 = -1
0 – 1 = -1 1-3= -2
-1 Critical node
17
17 7
7 R 0
0 2-1=1
0 0
0 53
12
53 4
12
4
1 L 0
0 0
11 13
11 13
0

This is balanced. 8 For R-L insertion,


-It needs 2 rotations:
First right rotation and then
left.

03/26/2024 Md. Golam Moazzam, Dept. of CSE, JU 9


AVL Tree
Construct AVL tree by inserting the following data:
14, 17, 11, 7, 53, 4, 13, 12, 8, 60, 19, 16, 20
Construction:
3-2 = 1

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

After Left rotation


This is balanced.

03/26/2024 Md. Golam Moazzam, Dept. of CSE, JU 10


AVL Tree
Construct AVL tree by inserting the following data:
14, 17, 11, 7, 53, 4, 13, 12, 8, 60, 19, 16, 20
Construction:

For R-R insertion,


-Just do one left
14
0 – 2 = -2 rotation one.
Critical
0 17 node
11
R
0-1= -1
0 -1
53
7 12 R
0
0 0 0
60
4 13
8

03/26/2024 Md. Golam Moazzam, Dept. of CSE, JU 11


AVL Tree
Construct AVL tree by inserting the following data:
14, 17, 11, 7, 53, 4, 13, 12, 8, 60, 19, 16, 20
Construction:
3-2 = 1

14
0
Now this is balanced.
0
53
11
0
0 -1 0
60
12 17
7

0 0 0

4 13
8

03/26/2024 Md. Golam Moazzam, Dept. of CSE, JU 12


AVL Tree
Construct AVL tree by inserting the following data:
14, 17, 11, 7, 53, 4, 13, 12, 8, 60, 19, 16, 20
Construction:
3-3 = 0
3-3 = 0

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

This is still balanced.


Still this is balanced.

03/26/2024 Md. Golam Moazzam, Dept. of CSE, JU 13


AVL Tree
Construct AVL tree by inserting the following data:
14, 17, 11, 7, 53, 4, 13, 12, 8, 60, 19, 16, 20
Construction:
For L-R insertion,
-It needs 2 rotations:
First left rotation and
then right.

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.

03/26/2024 Md. Golam Moazzam, Dept. of CSE, JU 14


AVL Tree
Construct AVL tree by inserting the following data:
14, 17, 11, 7, 53, 4, 13, 12, 8, 60, 19, 16, 20
Construction:

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

This is the final AVL tree

03/26/2024 Md. Golam Moazzam, Dept. of CSE, JU 15


AVL Tree
Example 2:
Construct AVL tree by inserting the following data:
B, F, I, N, L, P, J, A, H, D, M, K

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

03/26/2024 Md. Golam Moazzam, Dept. of CSE, JU 16


AVL Tree
Example 2:
Construct AVL tree by inserting the following data:
B, F, I, N, L, P, J, A, H, D, M, K

1-2=-1 For RL insertion


1. Right rotation: N, L
F 2. Then left rotation
F
0 0-2 = -2
0 -1
Critical
B I node
B I
R
0 1

N Imbalanced
Balanced N
0
L

03/26/2024 Md. Golam Moazzam, Dept. of CSE, JU 17


AVL Tree
Example 2:
Construct AVL tree by inserting the following data:
B, F, I, N, L, P, J, A, H, D, M, K

1-2 = -1

F F Balanced
0 0
0

B I L
B
R
0 0

L N
I
R

03/26/2024 Md. Golam Moazzam, Dept. of CSE, JU 18


AVL Tree
Example 2:
Construct AVL tree by inserting the following data:
B, F, I, N, L, P, J, A, H, D, M, K

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

03/26/2024 Md. Golam Moazzam, Dept. of CSE, JU 19


AVL Tree
Example 2:
Construct AVL tree by inserting the following data:
B, F, I, N, L, P, J, A, H, D, M, K

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

03/26/2024 Md. Golam Moazzam, Dept. of CSE, JU 20


AVL Tree
Example 2:
Construct AVL tree by inserting the following data:
B, F, I, N, L, P, J, A, H, D, M, K

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

03/26/2024 Md. Golam Moazzam, Dept. of CSE, JU 21


AVL Tree
Example 2:
Construct AVL tree by inserting the following data:
B, F, I, N, L, P, J, A, H, D, M, K

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

03/26/2024 Md. Golam Moazzam, Dept. of CSE, JU 22


AVL Tree
Example 2:
Construct AVL tree by inserting the following data:
B, F, I, N, L, P, J, A, H, D, M, 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

03/26/2024 Md. Golam Moazzam, Dept. of CSE, JU 23


AVL Tree
Example 2:
Construct AVL tree by inserting the following data:
B, F, I, N, L, P, J, A, H, D, M, K

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

This is the final AVL tree

03/26/2024 Md. Golam Moazzam, Dept. of CSE, JU 24

You might also like