AVL Tree
AVL Tree
AVL Tree
1
Height of a tree
• Max number of nodes in path from root to any leaf
Height(empty tree) = 0
height(a leaf) = ?
height(A) = ? A
2
Some height numbers
min # of nodes: h
2 14 18 21
3
Trees and balance
• Disadvantage(BST) : height can be N-1
1
“Not balanced” 4
2
3
2 5
4
“Balanced”
4
5 1 3
2 6
6
Is this “balanced”?
1 3 5 7 7
binary tree is said to be balanced if for every node, height of its children differ by at most one
4
AVL trees - Adelson-Velsky & Landis
• AVL tree is a self balanced binary search tree.
5
AVL tree examples
• Two binary search trees:
(a) an AVL tree
(b) not an AVL tree (unbalanced nodes are darkened)
An AVL tree is a balanced binary search tree with balance factor of every node is either -1, 0 or +1.
7
AVL Tree Rotations
• After performing every operation like insertion and deletion
we need to check the balance factor of every node in the
tree.
8
AVL Rotations
Rotation is the process of moving the nodes to either left or right to make tree balanced.
9
Single Left Rotation (LL Rotation)
Every node moves one position to left from the current position.
10
Single Right Rotation (RR Rotation)
Every node moves one position to right from the current position
11
Left Right Rotation (LR Rotation)
• Combination of single left rotation followed by single right rotation.
• First every node moves one position to left then one position to right from the
current position
12
Right Left Rotation (RL Rotation)
• Combination of single right rotation followed by single left rotation.
• First every node moves one position to right then one position to left from
the current position
13
Operations on an AVL Tree
• Insertion/Deletion
14
Insertion
15
Insertion
16
Insertion
17
Insertion
18
Insertion
19
Insertion
20
Insertion
21
Initial AVL tree with balance factors:
0 57
0 26 +1 72
-1 25 0 38 0 63 -1 94
0 3 0 37 0 47 0 78
Balance ok
22
Insert 1 and recalculate balance factors
-1 57
-1 26 +1 72
-2 25 0 38 0 63 -1 94
-1 3 0 37 0 47 0 78
0 1
Balance not ok
(Balance factor of -2 is not allowed)
23
-1 57
-1 26 +1 72
-1 3 0 37 0 47 0 78
0 1
Balance not ok
24
0 57
0 26 +1 72
0 3 0 38 0 63 -1 94
0 1 0 25 0 37 0 47 0 78
Balance ok
25
Insert 30 and recalculate balance factors
-1 57
+1 26 +1 72
0 3 -1 38 0 63 -1 94
0 1 0 25 -1 37 0 47 0 78
0 30
Balance ok
26
Insert 32 and recalculate balance factors
-2 57
+2 26 +1 72
0 3 -2 38 0 63 -1 94
0 1 0 25 -2 37 0 47 0 78
+1 30
0 32
Balance not ok
27
-2 57
+2 26 +1 72
0 3 -2 38 0 63 -1 94
0 1 0 25 -2 37 0 47 0 78
+1 30
Left Right Case
0 32
Balance not ok
28
-2 57
+2 26 +1 72
0 3 -2 38 0 63 -1 94
0 1 0 25 -2 37 0 78
0 47
-1 32
0 30
Balance not ok
29
-1 57
+1 26 +1 72
0 3 -1 38 0 63 -1 94
0 1 0 25 0 32 0 47 0 78
0 30 0 37
Balance ok
30
Insert 35 and recalculate balance factors
-2 57
+2 26 +1 72
0 3 -2 38 0 63 -1 94
0 1 0 25 +1 32 0 47 0 78
0 30 -1 37
0 35
Balance not ok
31
-2 57
0 3 -2 38 0 63 -1 94
0 1 0 25 +1 32 0 47 0 78
0 30 -1 37
Left Right Case
0 35
Balance not ok
32
-2 57
+2 26 +1 72
0 3 -2 38 0 63 -1 94
0 1 0 25 -2 37 0 47 0 78
0 32
0 30 0 35
Balance not ok
33
-1 57
+1 26 +1 72
0 3 0 37 0 63 -1 94
0 1 0 25 0 32 +1 38 0 78
0 30 0 35 0 47
Balance ok
34
-1 57
+1 26 +1 72
0 3 0 37 0 63 -1 94
0 1 0 25 0 32 +1 38 0 78
0 30 0 35 0 47
Balance ok
Exercise: insert 36
35
Remove 24 and 20 from the AVL tree.
13 13
10 24 10 20
7 20 30 7 15 30
15 25 36 25 36
13 13
10 30 10 15
7 15 36 7 30
25 25 36
36