F2. AVLTrees
F2. AVLTrees
Algorithms
Unit 6 Advanced Data Structures
1
What is an AVL Tree?
● An AVL tree is a self-balancing Binary Search Tree (BST) where the
difference between heights of left and right subtrees cannot be more than one
for all nodes.
○ The technique of balancing the height of binary trees was developed by
Adelson, Velskii and Landi and hence given the short form as AVL tree or
Balanced Binary Tree.
○ AVL trees use balance factor to get a balanced tree. Balance factor of any node
is defined as height(left subtree) - height(right subtree).
○ The balanced factor of a node in a binary tree can have value 1,
○ -1, 0 depending on whether the height of its left subtree is greater, less than or
equal to the height of the right subtree.
2
AVL Trees – Balanced Factor
• For AVL tree, the absolute value of balanced factor for any node can’t be greater than 1 i.e.,
each node must have a balance factor of either -1, 0 or 1.
3
AVL Tree
70
50 90
30 60 80 95
20 40
AVL Tree??
4
AVL Tree
● If at any time heights of left and right subtrees differ by more than one, then
rebalancing is done to restore AVL property, this process is called rotation
70
50 90
30 80 95
20 40
5
Why do we need AVL Tree?
● 10, 20, 30, 40, 50, 60
10
○ Search for 60
○ Time complexity is O(N)
20
30
40
50
60
6
Why do we need AVL Tree?
● 10, 20, 30, 40, 50, 60
10
○ Search for 60
○ Time complexity is O(LogN)
20
40
30
20 60
40
10 30 50 70
50
60
7
Common Operations on AVL Tree
• Creation of AVL trees,
• Search for a node in AVL trees
• Traverse all nodes in AVL trees
• Insert a node in AVL trees
• Delete a node from AVL trees
40
• Delete the entire AVL trees
20 60
10 30 50 70
8
Common Operations on AVL Tree
• Creation of AVL trees,
• AVL newAVL = new AVL( )
• rootNode = Null 40
10 30 50 70
9
AVL Tree – PreOrder Traversal
• Time complexity : O(N)
• Space complexity : O(N) 70
50 90
30 60 80 95
20 40
10
AVL Tree – InOrder Traversal
• Time complexity : O(N)
• Space complexity : O(N) 70
50 90
30 60 80 95
20 40
11
AVL Tree – PostOrder Traversal
• Time complexity : O(N)
• Space complexity : O(N) 70
50 90
30 60 80 95
20 40
12
AVL Tree – LevelOrder Traversal
• Time complexity : O(N)
• Space complexity : O(N) 70
50 90
30 60 80 95
20 40
13
AVL Tree – Search
80 70
50 90
30 60 80 95
20 40
50 90
30 60 80 95
20 40
15
AVL Tree – Insert a Node
• Case 1 : Rotation is not required
70
75
50 90
30 60 80 95
20 40
16
AVL Tree – Insert a Node
• Case 1 : Rotation is not required
70
50 90
30 60 80 95
20 40 75
17
AVL Tree – Insert a Node
• Case 2 : Rotation is required
70
LL - left left condition
LR - left right condition
RR - right right condition
RL - right left condition 50 90
30 60 80 95
20
18
AVL Tree – Insert a Node
• Case 2 : Rotation is required
10 70
LL - left left condition
50 90
30 60 80 95
20
19
AVL Tree – Insert a Node
• Case 2 : Rotation is required
70
LL - left left condition
50 90
30 60 80 95
20
10
20
AVL Tree – Insert a Node
• Case 2 : Rotation is required
70
LL - left left condition
Right rotation
50 90
30 60 80 95
20
10
21
AVL Tree – Insert a Node
• Case 2 : Rotation is required
70
LL - left left condition
Right rotation
50 90
20 60 80 95
10 30
22
AVL Tree – Insert a Node
• Case 2 : Rotation is required
20 70
LL - left left condition
Right rotation – example 2
50 90
30 60
23
AVL Tree – Insert a Node
• Case 2 : Rotation is required
70
LL - left left condition
Right rotation – example 2
50 90
30 60
20
24
AVL Tree – Insert a Node
• Case 2 : Rotation is required
50
LL - left left condition
Right rotation – example 2
30 70
20 60 90
rotateRight(disbalancedNode) {
newRoot = disbalancedNode.leftChild
disbalancedNode.leftChild = disbalancedNode.leftChild.rightChild
newRoot.rightChild = disbalancedNode
update height of disbalancedNode and newRoot
return newRoot
}
25
AVL Tree – Insert a Node
• Case 2 : Rotation is required
70
LR - left right condition
25
50 90
30 60 80 95
20
26
AVL Tree – Insert a Node
• Case 2 : Rotation is required
70
LR - left right condition
50 90
30 60 80 95
20
25
27
AVL Tree – Insert a Node
• Case 2 : Rotation is required
70
LR - left right condition
1. Left rotation
2. Right rotation
50 90
30 60 80 95
25
20
28
AVL Tree – Insert a Node
• Case 2 : Rotation is required
70
LR - left right condition
1. Left rotation
2. Right rotation
50 90
25 60 80 95
20 30
29
AVL Tree – Insert a Node
• Case 2 : Left Right Condition
Step 1 : rotate Left disbalancedNode.leftChild
Step 2 : rotate Right disbalancedNode
rotateLeft(disbalancedNode)
newRoot = disbalancedNode.rightChild
disbalancedNode.rightChild = disbalancedNode.rightChild.leftChild
newRoot.leftChild = disbalancedNode
update height of disbalancedNode and newRoot
return newRoot
rotateRight(disbalancedNode)
newRoot = disbalancedNode.leftChild
disbalancedNode.leftChild = disbalancedNode.leftChild.rightChild
newRoot.rightChild = disbalancedNode
update height of disbalancedNode and newRoot
return newRoot
30
AVL Tree – Insert a Node
• Case 2 : Rotation is required
75 50
RR - right right condition
40 60
70
31
AVL Tree – Insert a Node
• Case 2 : Rotation is required
50
RR - right right condition
40 60
70
75
32
AVL Tree – Insert a Node
• Case 2 : Rotation is required
50
RR - right right condition
Left Rotation
40 70
60 75
33
AVL Tree – Insert a Node
• Case 2 : Rotation is required
75 50
RR - right right condition
60 70
34
AVL Tree – Insert a Node
• Case 2 : Rotation is required
50
RR - right right condition
60 70
75
35
AVL Tree – Insert a Node
• Case 2 : Rotation is required
65
RR - right right condition
60 75
40
36
AVL Tree – Insert a Node
• RR – right right condition
rotateLeft(disbalancedNode)
newRoot = disbalancedNode.rightChild
disbalancedNode.rightChild = disbalancedNode.rightChild.leftChild
newRoot.leftChild = disbalancedNode
update height of disbalancedNode and newRoot
return newRoot
37
AVL Tree – Insert a Node
• Case 2 : Rotation is required
65 50
RL - right left condition
40 60
70
38
AVL Tree – Insert a Node
• Case 2 : Rotation is required
50
RL - right left condition
1. Right rotation
2. Left rotation
40 60
70
65
39
AVL Tree – Insert a Node
• Case 2 : Rotation is required
50
RL - right left condition
1. Right rotation
2. Left rotation
40 60
65
70
40
AVL Tree – Insert a Node
• RL – right left condition
Step 1: rotate Right disbalancedNode.rightChild
Step 2: rotate Left disbalancedNode
rotateRight(disbalancedNode)
newRoot = disbalancedNode.leftChild
disbalancedNode.leftChild = disbalancedNode.leftChild.rightChild
newRoot.rightChild = disbalancedNode
update height of disbalancedNode and newRoot
return newRoot
rotateLeft(disbalancedNode)
newRoot = disbalancedNode.rightChild
disbalancedNode.rightChild = disbalancedNode.rightChild.leftChild
newRoot.leftChild = disbalancedNode
update height of disbalancedNode and newRoot
return newRoot
41
AVL Tree – Exercise
• Create an AVL tree using the following set of numbers: 10, 34, 24, 3, 66, 47, 18, 8, 26,
55, 82, 29
42
AVL Tree – Insertion
43
AVL Tree – Insertion
44
AVL Tree – Insertion
45
AVL Tree – Exercise
• Construct a AVL tree from the given nodes:
• 9, 15, 20, 8, 7, 13, 10
46
AVL Tree – Deletion
• The deletion operation in AVL Tree is similar to deletion operation in BST.
• After deletion, update the height of the current node of the AVL tree.
• Now check the BF at the current node by getting the difference of height of left
sub-tree and height of right sub-tree.
• If BF is greater than 1, that means height of LST is greater than RST (left-left or
left-right case).
• If BF is less than 1, then that means the height of the RST is greater than the
height of the LST (right-right or right-left).
47
AVL Tree – Deletion
• Consider the AVL tree:
48
AVL Tree – Deletion
• At this point, there is a height imbalance at node 3 with the left-left case. We do a
right rotation at node 3.
49
Time and Space Complexity of AVL
50
Binary Search Tree vs AVL Tree
51