Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Data Structure
By
Muhammad Nouman Zafar
Perfectly Balanced tree
• The heights of the left and right subtrees of the root are equal.
• The left and right subtrees of the root are perfectly balanced binary
trees.
Balanced Tree(AVL tree – by G. M. Adelson-
Velskii and E. M. Landis )
• The heights of the left and right subtrees of the root differ by at most
1.
• The left and right subtrees of the root are AVL trees.
Balance Factor
• 𝑏𝑓 = ℎ𝑟 − ℎ𝑙
• ℎ𝑟 is the height of right subtree.
• ℎ𝑙 is the height of left subtree.
• IF bf>=1, tree is right high.
• IF bf<=1, tree is left high.
• IF bf==1, tree is equal high or balanced.
16 25
7 18 30
5 17 19
AVL Trees
Not AVL tree:
20
16 25
7 18 30
5 17 19 27
Which trees are AVL?
Types of rotation in AVL
• Single Rotation
• Right Rotation
• Left Rotation
• Double Rotation
• Right Left Rotation
• Left Right Rotation
• Right Right Rotation
• Left Left Rotation
AVL Tree Rotations
Single rotations: insert 14, 15, 16, 13, 12, 11, 10
14
15
15
16
• Need to rotate.
AVL Tree Rotations
Single rotations:
15
16
• Need to rotate.
AVL Tree Rotations
Single rotations:
• Rotation type:
14
15
16
AVL Tree Rotations
Single rotations:
15
14 16
AVL Tree Rotations
Single rotations:
14 16
13
12
• Rotation type:
15
14 16
13
12
AVL Tree Rotations
Single rotations:
15
13 16
12 14
13 16
12 14
11
• Rotation type: 15
13 16
12 14
11
AVL Tree Rotations
Single rotations:
13
12 15
16
11 14
13
12 15
16
11 14
10
• Rotation type:
13
12 15
16
11 14
10
AVL Tree Rotations
Single rotations:
13
11 15
10 12 14 16
11 15
10 12 14 16
AVL Tree Rotations
Double rotations:
• AVL violation - rotate
13
11 15
16
10 12 14
2
AVL Tree Rotations
Double rotations:
• Rotation type:
13
11 15
16
10 12 14
2
AVL Tree Rotations
Double rotations:
11 15
2 12 14 16
1 10
• Now insert 3.
AVL Tree Rotations
Double rotations:
11 15
2 12 14 16
1 10
3
AVL Tree Rotations
Double rotations:
• Rotation type:
13
11 15
2 12 14 16
1 10
3
AVL Tree Rotations
Double rotations:
10 15
2 11 14 16
1 3 12
• Now insert 4.
AVL Tree Rotations
Double rotations:
10 15
2 11 14 16
1 3 12
4
AVL Tree Rotations
Double rotations:
• Rotation type:
13
10 15
2 11 14 16
1 3 12
4
AVL Tree Rotations
Double rotations:
10
2 13
1 11 15
3
4 12 14 16
• Now insert 5.
AVL Tree Rotations
Double rotations:
10
2 13
1 11 15
3
4 12 14 16
2 13
1 11 15
3
4 12 14 16
5
AVL Tree Rotations
Single rotations:
• AVL balance restored:
10
2 13
1 11 15
4
5 12 14 16
3
• Now insert 7.
AVL Tree Rotations
Single rotations:
• AVL violation – rotate.
10
2 13
1 11 15
4
5 12 14 16
3
7
AVL Tree Rotations
Single rotations:
• Rotation type:
10
2 13
1 11 15
4
5 12 14 16
3
7
AVL Tree Rotations
Double rotations:
4 13
2 11 15
5
7 12 14 16
1 3
• Now insert 6.
AVL Tree Rotations
Double rotations:
4 13
2 11 15
5
7 12 14 16
1 3
6
AVL Tree Rotations
Double rotations:
• Rotation type:
10
4 13
2 11 15
5
7 12 14 16
1 3
6
AVL Tree Rotations
Double rotations:
4 13
2 11 15
6
12 14 16
1 3 5 7
4 13
2 11 15
6
12 14 16
1 3 5 7
8
AVL Tree Rotations
Double rotations:
• Rotation type:
10
4 13
2 11 15
6
12 14 16
1 3 5 7
8
AVL Tree Rotations
Final tree:
4 13
2 11 15
6
12 14 16
1 3 5 8
7 9