AVL Tree
AVL Tree
AVL Tree
AVL Tree is invented by GM Adelson - Velsky and EM Landis in 1962. The tree is named AVL in honour of its inventors.
AVL Tree can be defined as height balanced binary search tree in which each node is associated with a balance factor
which is calculated by subtracting the height of its right sub-tree from that of its left sub-tree.
Tree is said to be balanced if balance factor of each node is in between -1 to 1, otherwise, the tree will be unbalanced
and need to be balanced.
If balance factor of any node is 1, it means that the left sub-tree is one level
higher than the right sub-tree.
If balance factor of any node is 0, it means that the left sub-tree and right sub-
tree contain equal height.
If balance factor of any node is -1, it means that the left sub-tree is one level
lower than the right sub-tree.
An AVL tree is given in the following figure. We can see that, balance factor
associated with each node is in between -1 and +1. therefore, it is an example
of AVL tree.
Complexity
Due to the fact that, AVL tree is also a binary search tree therefore, all the operations are performed in the same way as
they are performed in a binary search tree. Searching and traversing do not lead to the violation in property of AVL tree.
However, insertion and deletion are the operations which can violate this property and therefore, they need to be
revisited.
SN Operation Description
Insertion in AVL tree is performed in the same way as it is performed in a binary search tree.
1 Insertion However, it may lead to violation in the AVL tree property and therefore the tree may need
balancing. The tree can be balanced by applying rotations.
Deletion can also be performed in the same way as it is performed in a binary search tree.
2 Deletion Deletion may also disturb the balance of the tree therefore, various types of rotations are used
to rebalance the tree.
AVL Tree
AVL tree controls the height of the binary search tree by not letting it to be skewed. The time taken for all operations in
a binary search tree of height h is O(h). However, it can be extended to O(n) if the BST becomes skewed (i.e. worst case).
By limiting this height to log n, AVL tree imposes an upper bound on each operation to be O(log n) where n is the
number of nodes.
AVL Rotations
We perform rotation in AVL tree only in case if Balance Factor is other than -1, 0, and 1. There are basically four types of
rotations which are as follows:
Where node A is the node whose balance Factor is other than -1, 0, 1.
The first two rotations LL and RR are single rotations and the next two rotations LR and RL are double rotations. For a
tree to be unbalanced, minimum height must be at least 2, Let us understand each rotation
1. 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, RR rotation is an anticlockwise rotation, which is applied on the edge below a node having balance
factor -2
In above example, node A has balance factor -2 because a node C is inserted in the right subtree of A right subtree. We
perform the RR rotation on the edge below A.
2. LL Rotation
Double rotations are bit tougher than single rotation which has already explained above. LR rotation = RR rotation + LL
rotation, i.e., first RR rotation is performed on subtree and then LL rotation is performed on full tree, by full tree we
mean the first node from the path of inserted node whose balance factor is other than -1, 0, or 1.
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.
4. RL Rotation
As already discussed, that double rotations are bit tougher than single rotation which has already explained above. R L
rotation = LL rotation + RR rotation, i.e., first LL rotation is performed on subtree and then RR rotation is performed on
full tree, by full tree we mean the first node from the path of inserted node whose balance factor is other than -1, 0, or
1.
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.