AVL Trees
AVL Trees
Balanced BST
3
Contd…
• Invented by Georgy Adelson-Velsky and Evgenii Landis
in 1962.
• Height balanced binary search trees.
• Each node has a balance factor.
• Let HL and HR be the heights of left and right subtrees
of any node, then
|HL – HR| <= 1
• Balance factor (bal) of a node K is HL – HR.
– Left High (LH) = +1 (left sub-tree higher than right sub-tree)
– Even High (EH) = 0 (left and right sub-trees have same height)
– Right High (RH) = -1 (right sub-tree higher than left sub-tree)
4
Height of AVL Trees
• Guaranteed to be in the order of lg2n for a tree
containing n nodes.
• If an AVL tree has minimum number of nodes, then
one of its subtrees is higher than the other by 1.
• Let, the left subtree is bigger than the right subtree,
and
– 𝑁(ℎ) = minimum number of nodes in an AVL tree of height ℎ
rooted at 𝑟.
– 𝑁(ℎ − 1) = minimum number of nodes in the left subtree of 𝑟.
– 𝑁(ℎ − 2) = minimum number of nodes in the right subtree of 𝑟.
5
Contd…
𝑁(ℎ) = 1 + 𝑁(ℎ − 1) + 𝑁(ℎ − 2)
As per assumption 𝑁(ℎ − 1) > 𝑁(ℎ − 2), so
𝑁(ℎ) > 1 + 𝑁(ℎ − 2) + 𝑁(ℎ − 2) = 1 + 2 ⋅ 𝑁(ℎ − 2) > 2 ⋅ 𝑁(ℎ − 2)
That is,
𝑁(ℎ) > 2 ⋅ 𝑁(ℎ − 2)
Knowing 𝑁(0) = 1, this recurrence can be solved.
𝑁(ℎ) > 2 ⋅ 𝑁(ℎ − 2) > 2 ⋅ 2 ⋅ 𝑁(ℎ − 4) > 2 ⋅ 2 ⋅ 2 ⋅ 𝑁(ℎ − 6) > ⋯ > 2ℎ/2
To ensure it’s 2ℎ/2, lets check for a particular ℎ = 6
𝑁(6) > 2 ⋅ 𝑁(6 − 2) > 2 ⋅ 2 ⋅ 𝑁(4 − 2) > 2 ⋅ 2 ⋅ 2 ⋅ 𝑁(2 − 2) > 23
6
Contd…
Thus,
𝑁(ℎ) > 2ℎ/2
Taking log,
log 𝑁(ℎ) > log 2ℎ/2 ⇔ ℎ < 2 log 𝑁(ℎ)
10
Example 1: Insert 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
• Insert 10, 9, 8
(2)
10 (0)
(1) 9
9 (0) (0)
(0)
8 10
8
• Insert 7, 6
(2)
(1)
9 9
(2) (0)
8 10 (0)
7 10
(0)
12
Contd…
• Insert 4 (1)
7
(2) (0)
6 9
(1) (0) (0)
5 8 10
(0)
4
(0)
7
(0) (0)
5 9
(0) (0) (0) (0)
4 6 8 10
13
Contd…
(2)
7
• Insert 3, 2 (2)
5 9
(0)
14
Contd…
(2)
7
• Insert 1 (2) (0)
5 9
(1) (0) (0) (0)
3 6 8 10
(1) (0)
2 4
(0)
1
(1)
7
(0) (0)
3 9
(1) (0) (0) (0)
2 5 8 10
(0) (0) (0)
1 4 6
15
Example 2: Insert 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
• Insert 1, 2, 3
(-2)
1 (0)
2
(-1)
2 (0) (0)
1 3
(0)
3
• Insert 4, 5
(-2)
(-1)
2 2
(0) (-2)
1 3 (0)
1 4
(0)
(0) (-1)
1 4
(0) (-1)
3 5
(0)
6
(0)
4
(0) (-1)
2 5
(0) (0) (0)
1 3 6
17
Contd…
• Insert 7 4
(-1)
(0) (-2)
2 5
(0) (0) (-1)
1 3 6
(0)
7
(0)
4
(0) (0)
2 6
(0) (0) (0) (0)
1 3 5 7
18
Contd…
(-2)
4
• Insert 8, 9 (0)
2 6
(-2)
19
Contd…
(-2)
4
• Insert 10 (0) (-2)
2 6
(0) (0) (0) (-1)
1 3 5 8
(0) (-1)
7 9
(0)
10
(-1)
4
(0) (0)
2 8
(0) (0) (0) (-1)
1 3 6 9
(0) (0) (0)
5 7 10
20
Unbalanced Cases
• Double rotation
– Right of Left: insertion turned the left subtree of
a left high AVL tree into a right high tree.
21
Example 3: Insert 14, 17, 11, 7, 53, 4, 13, 12, 8
• Insert 14, 17, 11,7, 53, 4
(1)
14
(2) (-1)
11 17
(1) (0)
7 53
(0)
4 (0)
14
(0) (-1)
7 17
(0) (0) (0)
4 11 53
22
Contd…
• Insert 13, 12
(2) (1)
14 14
(-2) (-1) (-1) (-1)
7 17 7 17
(0) (-2) (0) (0) (0) (0)
4 11 53 4 12 53
(1)
(0) (0)
13 11 13
(0)
12
(-2) (-2)
11 11 (0)
(1) (-1) 12
13 12 (0) (0)
(0) (0) 11 13
12 13 23
(-2)
7
(0)
4 12
(1) Contd…
(1) (0)
11 13 (2)
14 • Insert 8
(0) (-2) (-1)
8 7 17
(0) (1) (0)
(-2) 4 12 53
7
(0) (-1) (1) (0)
4 11 11 13
(0)
(0)
8 12
(-1)
8
(0) (1)
13 14
(0) (-1)
(-1)
11 11 17
(-1) (0)
(-2) (-1) (0)
7 12 53
7 12 (0)
(0) (0) (0)
(0)
4 8 13 4 (0) 8 13 24
Example 4: 50, 25, 37, 35, 60, 70, 30, 45, 34, 40, 55
(-2) (-2)
25 25 (0)
(1) (-1) 30
35 30 (0) (0)
(0) (0) 25 35
30 35 26
(-1)
Contd… 37
(-1) (2)
• Insert 45, 34, 40 30 60
(0) (2) (0)
25 (1) 35 50 70
(0)
(1)
34 45
(0)
(0) 40
37
(-1) (1)
30 60
(0) (0) (0)
25 (1) 35 45 70
(0) (0)
34 40 (0) 50
27
(2)
60
Contd… • Insert 55 (-1) (0)
(-1) 45 70
37 (0) (-1)
(-1) (2)
40 50
30 60 (0)
(0) (-1) (0)
55
25 (1) 35 45 70 (2)
60
(0) (0)
34 40 (-1) 50 (1) (0)
50 70
(0) (1) (0)
(0) 55 45 55
37 (0)
(0) 40 (0)
(-1)
30 50 50
(1) (0)
(0) (1) (0) 45 60
25 (1) 35 45 60 (0) (0) (0)
(0)
(0)
34
(0)
55 70
(0) 40 55 70
40 28
(-2)
10
Example 5: Insert 15 (0) (2)
(-1) 4 16
10 (0) (0) (-1) (0)
(0) (1) 1 7 13 19
4 16 (0) (-1)
(0) (0) (0) (0)
12 14
1 7 13 19 (0)
(0) (0) (-1)
15
12 14 10
(0) (0)
4 16
(0) (0) (1) (0)
1 7 13 19
(0) (0) (0)
12 14 15 29
(-2)
25 34 45 30
Case 1: Left of Left (Insertion)
BalFactor > 1 and Key < Node→left→data
2 0
1 A B 0
B A
T3 h T1 h
T1 h T2 h T2 h T3 h
31
Case 2: Right of Right (Insertion)
BalFactor < -1 and Key > Node→right→data
-2 0
A -1 0 B
B A
h T1 h T3
h T2 h T3 h T1 h T2
32
A
Case 3: Right of Left (Insertion)
C
B
T4 h
h–1
2 T3
-1 A
B T1 h T2
1
C T4 h 0
C
-1
T1 h 0 B A
h–1
T2 T3
h–1
T1 h T2 T3 T4 h
BalFactor > 1 and
Key > Node→left→data 33
A
Case 4: Left of Right (Insertion)
C
B
h T1
h–1
-2 T2
A 1
B T3 h T4
1
h T1 C
C 0
0
h T4 A B -1
h–1
T2 T3
h–1
h T1 T2 T3 h T4
BalFactor < -1 and
Key < Node→right→data 34
Node Structure
For a new node
• data = value
• Four elements • left = right = NULL
– data <dataType> • height = 1
– left <pointer to Node>
– right <pointer to Node>
– height <int> // Height of a node
OR
bal <LH (= 1), EH (= 0), RH (= -1)> // Balance factor
35
NODE* rotateLeft(NODE *root)
Left Rotation { NODE *tempPtr;
Algorithm rotateLeft(root) tempPtr = root -> right;
1. Exchange right subtree root->right = tempPtr -> left;
of root with left subtree tempPtr -> left = root;
of its right subtree. // Update height of root
2. Make its right subtree // Update height of tempPtr
new root. return tempPtr; }
root root tempPtr
X a X a b
tempPtr tempPtr root
b b X a X c X
X d X X c X X d X X c X X d X
36
NODE* rotateRight(NODE *root)
Right Rotation { NODE *tempPtr;
Algorithm rotateRight(root) tempPtr = root -> left;
1. Exchange left subtree of root->left = tempPtr -> right;
root with right subtree tempPtr -> right = root;
of its left subtree. // Update height of root
2. Make its left subtree // Update height of tempPtr
new root. return tempPtr; }
root
a X root tempPtr
tempPtr a X b
b tempPtr root
b X c X a X
X c X X d X
X c X X d X X d X
37
Insertion (recursion)
1. Insert the new-node with value Key using normal
BST insertion.
2. Update height of the ancestor node, say Node.
3. Get the balance factor of this ancestor node, i.e.
Node.
4. BalFactor = (height of Node→left —
height of Node→right).
5. If BalFactor > 1 and Key < Node→left→data
6. Return rotateRight(Node). // Left of Left.
38
Example – 1 BalFactor > 1 and
Key < Node→left→data
Height = 1 5
Key 1 Height = 1
39
Contd...
7. If BalFactor < -1 and Key > Node→right→data
8. Return rotateLeft(Node); // Right of Right.
40
Example – 2 BalFactor < -1 and
Key > Node→right→data
Height = 1 20
Key 30 Height = 1
41
Contd...
7. If BalFactor < -1 and Key > Node→right→data
8. Return rotateLeft(Node); // Right of Right.
9. If BalFactor > 1 and Key > Node→left→data
10. Node→left = rotateLeft(Node→left);
11. Return rotateRight(Node); // Right of Left.
42
Example – 3 BalFactor > 1 and
Key > Node→left→data
Height = 1 5
43
Contd...
7. If BalFactor < -1 and Key > Node→right→data
8. Return rotateLeft(Node); // Right of Right.
9. If BalFactor > 1 and Key > Node→left→data
10. Node→left = rotateLeft(Node→left);
11. Return rotateRight(Node); // Right of Left.
12. If BalFactor < -1 and Key < Node→right→data
13. Node→right = rotateRight(Node→right);
14. Return rotateLeft(Node); // Left of Right.
44
Example – 4 BalFactor < -1 and
Key < Node→right→data
Height = 1 20
Node
10 Height = 3, BalFactor = -2
Node→right =
rotateRight(Node→right); 20 Height = 2, BalFactor = 1
rotateLeft(Node);
Key 15 Height = 1
45
BST Deletion
• Search for a node to remove.
• If the node is found, then there are three cases:
1. Node to be removed has no children.
Set corresponding link of the parent to NULL and
dispose the node.
2. Node to be removed has one child.
Link single child (with it's subtree) directly to the
parent of the removed node.
3. Node to be removed has two children.
Find inorder successor of the node.
Copy contents of the inorder successor to the node
being removed.
Delete the inorder successor from the right subtree. 46
Example – BST Deletion
44 • Delete 62, 17, and 78
17 78
32 50 88
48 62 84 92
80
82
47
After deleting 62
44 • Delete 62, 17, and 78
17 78
32 50 88
48 84 92
80
82
48
After deleting 17
44 • Delete 62, 17, and 78
32 78
50 88
48 84 92
80
82
49
After deleting 78
44 • Delete 62, 17, and 78
32 80
50 88
48 84 92
82
50
AVL Deletion
REQUIRES NO ROTATION
• Case 2
OR
51
• Case 3 Contd…
Before
deletion
After
deletion
1 3
53
Contd… (-1)
Right 2
• After deleting 5 rotation (0) (1)
1 4
(0)
(2)
4 3
(0)
2
(0) (0)
(1)
1 3 3
(1) (0)
2 4
Left followed by (0)
Right rotation 1
54
Example – 2 Delete 32
(-1) (-2)
44 44
(-1) (1) (0) (1)
17 78 17 78
(0) (0) (0)
(0) (0)
32 50 88 50 88
(0) (0) (0)
(0)
48 62 (0) 48 62
50
(0) (0)
44 78
(0) (0) (0) (0)
Right followed
17 48 62 88
by left rotation
55
Example – 3
• Delete 9
(-1)
10
(1) (0)
5 17
(-1) (-1)
(-1) (0)
2 9 12 20
(0) (1) (0) (-1)
(0)
3 11 15 18 30
(0)
(0)
13 33
56
(-1)
Contd… 10
(2) (0)
5 17
(-1) (-1)
(-1)
2 12 20
(0) (1) (0) (-1)
(0)
3 11 15 18 30
(0)
(0)
(-2) 13 33
10
(0) (0)
3 17
(-1) (-1)
(0) (0)
2 5 12 20
(0) (1) (0) (-1)
11 15 18 30
(0)
(0)
13 33 57
Contd…
(1)
17
(-1) (-1)
10 20
(-1) (0) (-1)
(0)
3 12 18 30
(0) (0) (0)
(0) (1)
2 5 11 15 33
(0)
13
58
Case 1: Left of Left (Deletion)
BalFactor > 1 and balance(Node→left) >= 0
2 -1/0
0/1 A B 1/0
B A
T3 h T1 h
T3
T1 h T2 T2
h–1
h or h or
h–1 h–1
59
Case 2: Right of Right (Deletion)
BalFactor < -1 and balance(Node→right) <= 0
-2 1/0
A 0/-1 -1/0 B
B A
h T1 h T3
T1
T2 h T3 T2
h–1
h or h or
h–1 h–1
60
A
Case 3: Right of Left (Deletion)
C
B T4 h – 1
h–1
2 T3
A
h-1
-1 T1 T2
B
C T4 h 0
T1 C
0
0 B A
T2 T3
h–1
h–1
h–1
h–1
h–1 T1 T2 T3 T4
BalFactor > 1 and
balance(Node→left) < 0 61
Case 4: Left of Right (Deletion) A
C
h–1
T1 B
h–1
-2 T2
A 1
h–1
B T3 T4
h T1 C
T4
h–1 C 0
0 A B 0
h–1
T2 T3 h–1
h–1
h–1
T1 T2 T3 T4
BalFactor < -1 and
balance(Node→right) > 0
62
Deletion (recursion)
1. Delete the node with value Key using normal BST
deletion.
2. Update height of the ancestor node, say Node.
3. Get the balance factor of this ancestor node, i.e.
Node.
4. BalFactor = (height of Node→left —
height of Node→right).
5. If BalFactor > 1 and balance(Node→left) >= 0
6. Return rotateRight(Node). // Left of Left.
63
Example – 1 BalFactor > 1 and
balance(Node→left) >= 0
(1) (1)
10 10
(1) (0) Delete (0) (0)
5 20 5 20
Key = 20
(0) (0)
(0)
1 1 8
(2) (2)
10 Node 10 Node
(1) (0)
5 5
(0) (0)
(0)
1 rotateRight(Node) 1 8
64
Contd...
7. If BalFactor > 1 and balance(Node→left) < 0
8. Node→left = rotateLeft(Node→left);
9. Return rotateRight(Node); // Right of Left.
65
Example – 2 BalFactor > 1 and
balance(Node→left) < 0
(1) (2)
4 4 Node
(-1) (0) (-1)
2 5 Delete Key = 5 2
(0) (0)
3 3
Node→left = rotateLeft(Node→left);
rotateRight(Node);
66
Contd...
7. If BalFactor > 1 and balance(Node→left) < 0
8. Node→left = rotateLeft(Node→left);
9. Return rotateRight(Node); // Right of Left.
10. If BalFactor < -1 and balance(Node→right) <= 0
11. Return rotateLeft(Node); // Right of Right.
67
Example – 3 BalFactor < -1 and
balance(Node→right) <= 0
(-1) (-1)
20 20
(0) (-1) Delete (0) (0)
10 25 10 25
Key = 10
(0) (0)
(0)
31 22 31
(-2) (-2)
Node 20 Node 20
(-1) (0)
25 25
(0) (0)
(0)
31 rotateLeft(Node) 22 31
68
Contd...
7. If BalFactor > 1 and balance(Node→left) < 0
8. Node→left = rotateLeft(Node→left);
9. Return rotateRight(Node); // Right of Left.
10. If BalFactor < -1 and balance(Node→right) <= 0
11. Return rotateLeft(Node); // Right of Right.
12. If BalFactor < -1 and balance(Node→right) > 0
13. Node→right = rotateRight(Node→right);
14. Return rotateLeft(Node); // Left of Right.
69
Example – 4 BalFactor < -1 and
balance(Node→right) > 0
(-1) (-2)
4 Node 4
(0) (1) (1)
2 8 Delete Key = 2 8
(0) (0)
5 5
Node→right = rotateRight(Node→right);
rotateLeft(Node);
70
Create AVL: 10, 20, 15, 25, 30, 16, 18, 19.
Delete 30.
• Insert 10, 20, 15
(-2)
10 (0)
(1) Right Rotation at 20 15
20 (0) (0)
Left Rotation at 10
(0) 10 20
(-2)
15 15
(0) (-2) Left Rotation (-1)
10 20 at 20 15
• Insert 25, 30 (0) (0)
(-1)
25 10 25
(0) (0)
(0)
30 20 30 71
Contd…
• Insert 16
(-2)
15 (0)
(0) (1) Right Rotation at 25 20
10 25 Left Rotation at 15 (0) (-1)
(1) (0) 15 25
20 30 (0) (0) (0)
(0) 10 16 30
16
72
Contd…
• Insert 18, 19
(2)
20
(-2) (-1) Left Rotation (1)
15 25 at 16
(0)
20
(-2) (0)
(-1) (-1)
10 16 30
15 25
(-1) (0) (0) (0)
18
10 18 30
(0)
(0) (0)
19
16 19
73
Contd…
• Delete 30 (2)
20 Left Rotation at 15
(-1) (0) Right Rotation at 20
15 25
(0) (0)
10 18 (0)
(0) (0)
18
16 19 (0) (0)
15 20
(0) (0) (0) (0)
10 16 19 25
74
Summary
• AVL trees are always balanced thus worst-case
complexity of all operations (search, insert, and
delete) is O(log n).
• Rotations performed for height balancing are constant
time operations, but takes a little time.
• Difficult to program & debug.
• Needs space to store either a height or a balance
factor.
• Suitable for applications where search or look-up is
the most frequent operations as compared to
insertion or deletion.
75