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.
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.
1. Balance Factor represents the height difference between the left and the right sub-
trees for a given node.
2. For leaf nodes, the balance factor is 0.
3. AVL balance criteria: bf = -1, 0, +1 for all 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.
2. LL Rotation
When BST becomes unbalanced, due to a node is inserted into the left subtree of the left
subtree of C, then we perform LL rotation, LL rotation is clockwise rotation.
3. LR Rotation
Double rotations are bit tougher than single rotation. In some cases, a single tree rotation is
not sufficient to balance the tree after a height-affecting operation. One such case is the
Left-Right (LR) insertion, where an imbalance occurs when a node is inserted into the right
subtree of the left child of the imbalanced node.
4. RL Rotation
It is similar to LR rotation but it is performed when the tree gets unbalanced, upon insertion
of a node into the left subtree of the right child of the imbalance node.
1. Rotations are done only on three nodes (including the imbalanced node)
irrespective of the size of the Binary Search Tree. Hence, in the case of a large tree
always focus on the two nodes around the imbalanced node and perform the tree
rotations.
2. Upon insertion of a new node, if multiple nodes get imbalanced then traverse the
ancestors of the inserted node in the tree and perform rotations on the first
occurred imbalanced node. Continue this process until the whole tree is balanced.
This process is knowns as retracing which is discussed later in the article.
Deleting a node from an AVL tree is similar to that in a binary search tree. Deletion may
disturb the balance factor of an AVL tree and therefore the tree needs to be rebalanced in
order to maintain the AVLness. For this purpose, we need to perform rotations. The two
types of rotations are L rotation and R rotation.
If the node which is to be deleted is present in the left sub-tree of the critical node, then L
rotation needs to be applied else if, the node which is to be deleted is present in the right
sub-tree of the critical node, the R rotation will be applied.
Let us consider that, A is the critical node and B is the root node of its left sub-tree. If node
X, present in the right sub-tree of A, is to be deleted, then there can be three different
situations:
AVL Tree is a self-balancing binary search tree. The deletion of a node will be the same
as in BST. After a node has been deleted according to BST, we’ll check for the
unbalanced nodes (Nodes having a balance factor of more than 1).
2. After removing the node, go up the tree and update the heights of the nodes. It
is like updating the order of the steps when you climb up or down stairs.
3. Starting from where you deleted the node, go up towards the root. Along the
way, check if any node needs to be balanced.
Highlights: