AVL Tree
AVL Tree
• 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.
• Definition: Let T be a non-empty binary tree with TL and TR as its left and right subtrees. The tree is height
balanced if:
• Since AVL trees are height balance trees, operations like insertion and deletion have low time complexity.
Bf = 1 Bf = 2
Bf = 1 Bf = 1 Bf = 2 Bf = 1
Bf = 1 Bf = 0
Bf = 1 Bf = 0
Bf = 0 Bf = 0
Bf = 0
Bf = 0 Bf = 1
Bf = 0
AVL Tree
Not an AVL Tree
To insert a node with a key 10 in the binary tree, the algorithm requires seven comparisons, but if you insert the
same key in AVL tree, from the above 1st figure, you can see that the algorithm will require three comparisons.
AVL Tree Rotations
• In AVL tree, after performing operations like insertion and deletion we need to check the balance factor of
every node in the tree.
• If every node satisfies the balance factor condition then we conclude the operation otherwise we must make it
balanced.
• Whenever the tree becomes imbalanced due to any operation we use rotation operations to make the tree
balanced.
• Rotation is the process of moving nodes either to left or to right to make the tree balanced.
Single Right Rotation (LL Rotation)
• When BST becomes unbalanced, due to inserting a new node into the left subtree of the left subtree of a given
node, then we perform LL rotation (single right rotation), LL rotation is clockwise rotation, which is applied on
the edge below a node having balance factor 2.
5
1 0
0 1
Insert 4 5 Insert 3 4
4 LL Rotation
5 0 0 0
0
4 3 5
3 Make it root
balanced balanced balanced
Not balanced
In such case just make median element as root and other two nodes will be the children.
Single Left Rotation (RR Rotation)
When BST becomes unbalanced, due to a node is inserted into the right subtree of the right subtree of A, then we
perform RR rotation (single Left), RR rotation is an anticlockwise rotation, which is applied on the edge below a
node having balance factor -2
-2
-1 5 0
0 5 -1 6
Insert 6 Insert 7 RR Rotation
5 0 6 0 0
6 0
5 7
7
balanced Make it root
balanced balanced
Not balanced
The node whose balance factor is other than (1, 0, -1) is termed as “critical node”. We always check from the leaf
node to root, which node is critical node.
Double Rotation (RL Rotation)
Double rotations are bit tougher than single rotation which has already explained before. RL rotation = Right
rotation + Left rotation, i.e., first Right rotation is performed on subtree and then Left rotation is performed on
full tree, i.e. the first node from the path of inserted node whose balance factor is other than -1, 0, or 1.
-2
-2
-1 5 0
5
0 7
Insert 8 5 Insert 7 1
Right Rotation -1 Left Rotation
5 0 8 0 0
7
8 0 5 8
0
7 Make it root 8
balanced balanced balanced
Not balanced Not balanced
Double Rotation (LR Rotation)
LR rotation = Left rotation + Right rotation, i.e., first Left rotation is performed on subtree and then Right
rotation is performed on full tree. Full tree means the first node from the path of inserted node whose balance
factor is other than -1, 0, or 1.
2 2
1 5 5 0
0 1 4
Insert 3 5 Insert 4 -1
Left Rotation Right Rotation
5 0 3 4 0 0
3 0 0 3 5
4 3
balanced balanced balanced
Not balanced Not balanced
Make it root
AVL Tree Construction
14 14
1 0
0 1 0 1 -1
14 14
14 11 17 11 17
0 0 0
0 0 0
17 11 17
7 7 53
Insert 14
Insert 17
Insert 11
Insert 7 Insert 53
1
0
14
14
2 -1
0 -1
11 17
Insert 4 7 17
1 0
LL Rotation
0 0 0
7 53
4 11 53 2
0 14
4 1
-2 -1
14
7 17
-1 -1
0 -2 0
7 17
4 11 53
Insert 13 0 -1 0 Insert 12
1
R
4 11 53
13
0 0 L
13
RL Rotation
12
2 2
1
14 14
14
-2 -1 -2 -1
-1 -1
7 17 7 17
7 17
0 -2 0 0 -2 0
0 0 0
4 11 53 4 11 53
Left Rotation 4 12 53
1 Right Rotation -1
R
0 0
13 12
0 R0 11 13
12 13
2
1
Insert 8 14
14
-2 -1
0 -1
7 17
11 17
0 1 0 Right Rotation
R 0 1 0
4 12 53
7 12 53
1 0
L 0 0 0
11 13
4 8 13
0
RL Rotation
Here find out the median element of 7, 12, and 11. It is 11, so it will be the root (parent) and 7 and 11 will
be the children.
1
1
Insert 60 14
14
0 -2
0 -2
11 17
11 17
0 1 1
0 1 R 1
7 12 53
7 12 53
0 0 0 0
0 0 0 R 0
4 8 13 60
4 8 13 60
0
1
14 14
1
0 0
0
53 53
11 11
Left Rotation Insert 19
0 -1 -1 0
0 -1 0 0
60 7 12 17 60
7 12 17
0 0 0
0 0 0
4 13 4 8 19
8
0
0
14
1 14
0
1
53 0
11
53
-1 -1 0 Insert 16 11
0
60 0 -1 0 0
7 12 17
7 12 17 60
0 0
0
4 0 0
8 19 0 0
4 8 19
16
-1
-1
14
14
2
2 1
1
53
53 11
Insert 20 11 L
0 0 -1 0
0 0 -1 0
7 12 17 60
7 12 17 60
0 0 R -1
0 0 1 0
0 4
4 8 19
8 19 16
16
0
0
20
20
again find out the median element of 53, 17, and 19. It is 19, so it will be the root (parent) and 17 and 53 will be
the children.
-1
0
14 14
2
1 0
1
53 19
11 11
L
0 0 -1 0 RL Rotation 0
0 0 1
7 12 17 60 53
7 12 17
0 0 R -1 0 0 0
0 0 0
4 8 19 4
16 8 20 60
16
0
20
Construct an AVL tree using the following keys
H, I, J, B, A, E, C, F, D, G
Step 1. Insert H, I, J
Step 2. Insert B, A
On inserting the above elements, especially in case of A, the BST becomes unbalanced as the Balance Factor of H and
I is 2, we consider the first node from the last inserted node i.e. H. Since the BST from H is left-skewed, we will
perform LL Rotation on node H.
Step 3. Insert E
• On inserting E, BST becomes unbalanced as the Balance Factor of I is 2, since if we travel from E to I we find
that it is inserted in the left subtree of right subtree of I, we will perform LR Rotation on node I. LR = RR + LL
rotation.
• We first perform RR rotation on node B.
• Now perform LL rotation on the node I.
Step 4. Insert C, F, D
• On inserting C, F, D, BST becomes unbalanced as the Balance Factor of B and H is -2, since if we travel from D
to B we find that it is inserted in the right subtree of left subtree of B, we will perform RL Rotation on node I. RL
= LL + RR rotation.
• We first perform LL rotation on node E.
• Now perform RR rotation on node B
Step 5. Insert G
• On inserting G, BST become unbalanced as the Balance Factor of H is 2, since if we travel from G to H, we find
that it is inserted in the left subtree of right subtree of H, we will perform LR Rotation on node I. LR = RR + LL
rotation.
• We first perform RR rotation on node C.
• We then perform LL rotation on node H.
AVL Tree Construction/Insertion
Insert the following elements in an AVL tree and write down the preorder, inorder, and postorder traversals for
the resultant tree.
a. 5, 6, 7, 1, 3, 4, 9, 7, 2