AVL Tree
AVL Tree
AVL Tree
The above tree is AVL because the differences between the heights of left and right
subtrees for every node are less than or equal to 1.
Example of a Tree that is NOT an AVL Tree:
The above tree is not AVL because the differences between the heights of the left
and right subtrees for 8 and 12 are greater than 1.
Page 2 of 10
Insertion in AVL Tree:
Insertion in AVL tree is performed in the same way as it is performed in a binary
search tree. The new node is added into AVL tree as the leaf node. 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. Rotation is required only if, the
balance factor of any node is disturbed upon inserting the new node, otherwise
the rotation is not required.
Depending upon the type of insertion, the Rotations are categorized into four
categories.
SN Rotation Description
The new node is inserted to the right sub-tree of the right sub-
2 RR Rotation
tree of the critical node.
The new node is inserted to the right sub-tree of the left sub-
3 LR Rotation
tree of the critical node.
The new node is inserted to the left sub-tree of the right sub-
4 RL Rotation
tree of the critical node.
In an AVL tree, the balance factor of a node is the difference in height between its
left and right subtrees. The balance factor is calculated to determine if the tree is
balanced or unbalanced:
Balance factor = 0: The node is balanced, and the left and right subtrees are
the same height
Balance factor > 0: The node is right heavy, meaning the left subtree is shorter
than the right subtree
Balance factor < 0: The node is left heavy, meaning the left subtree is taller
than the right subtree
The balance factor of every node in an AVL tree must be between -1 and 1. If a
node's balance factor is outside of this range, the tree is considered unbalanced
and needs to be rebalanced.
The process of constructing an AVL tree from the given set of elements is shown
in the following figure.
At each step, we must calculate the balance factor for every node, if it is found to
be more than 2 or less than -2, then we need a rotation to rebalance the tree. The
type of rotation will be estimated by the location of the inserted element with
respect to the critical node.
All the elements are inserted in order to maintain the order of binary search tree.
Page 4 of 10
To delete a node from an AVL tree, you can follow these steps:
1. Remove the node you want to delete.
2. Update the heights of the nodes as you move up the tree from the deleted
node.
3. Check for any nodes that need to be balanced.
4. If you find an imbalance, perform rotations to balance the tree.
Here are some things to consider when deleting nodes from an AVL tree:
5. Deleting a leaf node: This is easy because there are no child nodes to
consider.
6. Deleting a node with one child node: Connect the parent and child nodes.
7. Deleting a node with two child nodes: Replace the node to be deleted with its
in-order successor or predecessor, then delete that node.
8. Action position: This is the parent node from which a node was removed. It
indicates the first node whose height may have been affected by the deletion.
9. Rotation: Rotations are performed when the balance factor is not -1, 0, or 1.
Page 5 of 10
Example:
Delete the node 30 from the AVL tree shown in the following image.
Solution
In this case, the node B has balance factor 0, therefore the tree will be rotated by
using R0 rotation as shown in the following image. The node B(10) becomes the
root, while the node A is moved to its right. The right child of node B will now
become the left child of node A.
Page 6 of 10
R1 Rotation (Node B has balance factor 1)
Example
Delete Node 55 from the AVL tree shown in the following image.
Page 7 of 10
Solution :
Deleting 55 from the AVL Tree disturbs the balance factor of the node 50 i.e. node
A which becomes the critical node. This is the condition of R1 rotation in which,
the node A will be moved to its right (shown in the image below). The right of B is
now become the left of A (i.e. 45).
R-1 rotation is to be performed if the node B has balance factor -1. This case is
treated in the same way as LR rotation. In this case, the node C, which is the right
child of node B, becomes the root node of the tree with B and A as its left and
right children respectively.
Page 8 of 10
The sub-trees T1, T2 becomes the left and right sub-trees of B whereas, T3, T4
become the left and right sub-trees of A.
Example
Delete the node 60 from the AVL tree shown in the following image.
Solution:
in this case, node B has balance factor -1. Deleting the node 60, disturbs the
balance factor of the node 50 therefore, it needs to be R-1 rotated. The node C
i.e. 45 becomes the root of the tree with the node B(40) and A(50) as its left and
right child.
Page 9 of 10
Page 10 of 10