AVL Tree
AVL Tree
Balance Factor-
In AVL tree,
• Balance factor is defined for every node.
• Balance factor of a node = Height of its left subtree – Height of its right subtree
AVL Tree Operations-
Case-01:
• After the operation, the balance factor of each node is either 0 or 1 or -1.
• In this case, the AVL tree is considered to be balanced.
• The operation is concluded.
Case-02:
• After the operation, the balance factor of at least one node is not 0 or 1 or -1.
• In this case, the AVL tree is considered to be imbalanced.
• Rotations are then performed to balance the tree.
AVL Tree Rotations-
Kinds of Rotations-
LR Rotation with example 2
State Action
A node B has been inserted into the right subtree of A the left subtree of C, because of which C has
become an unbalanced node having balance factor 2. This case is L R rotation where: Inserted node
is in the right subtree of left subtree of C
After performing RR rotation, node C is still unbalanced, i.e., having balance factor 2, as inserted
node A is in the left of left of C
Now we perform LL clockwise rotation on full tree, i.e. on node C. node C has now become the right
subtree of node B, A is left subtree of B
Balance factor of each node is now either -1, 0, or 1, i.e. BST is balanced now.
RL Rotation with example 2
State Action
A node B has been inserted into the left subtree of C the right subtree of A, because of which A has
become an unbalanced node having balance factor - 2. This case is RL rotation where: Inserted
node is in the left subtree of right subtree of A
After performing LL rotation, node A is still unbalanced, i.e. having balance factor -2, which is
because of the right-subtree of the right-subtree node A.
Now we perform RR rotation (anticlockwise rotation) on full tree, i.e. on node A. node C has now
become the right subtree of node B, and node A has become the left subtree of B.
Balance factor of each node is now either -1, 0, or 1, i.e., BST is balanced now.
Construct an AVL tree by inserting the following elements in the given
order.
63, 9, 19, 27, 18, 108, 99, 81
Example: Construct an AVL Tree by inserting numbers from 1 to 8.
Construct an AVL tree having the following elements
20 30 80 40 10 60 50 70
Construct an AVL tree having the following elements
H, I, J, B, A, E, C, F, D, G, K, L
1. Insert H, I, J
On inserting the above elements, especially in the case of H, the BST becomes unbalanced as the Balance
Factor of H is -2. Since the BST is right-skewed, we will perform RR Rotation on node H.
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.
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
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.
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.
On inserting K, BST becomes unbalanced as the Balance Factor of I is -2. Since the BST is right-skewed from
I to K, hence we will perform RR Rotation on the node I.
On inserting the L tree is still balanced as the Balance Factor of each node is now either, -1, 0, +1. Hence the
tree is a Balanced AVL tree
Property-01:
Maximum possible number of nodes in AVL tree of height H
= 2H+1 – 1
Example-