23EC460
DATA STRUCTURES
AND
ALGORITHMS
11. AVL TREE
Prepared by,
Mrs. K. Iyshwarya Ratthi
Assistant Professor,
Department of ECE
K. Iyshwarya Ratthi, Image Processing Lab,
5/3/2025 TCE 1
AVL Trees
• AVL tree is a self-balancing binary search tree invented by G.M. Adelson-Velsky
and E.M. Landis in 1962. The tree is named AVL in honour of its inventors.
• In an AVL tree, the heights of the two sub-trees of a node may differ by at most one.
Due to this property, the AVL tree is also known as a height-balanced tree.
• The key advantage of using an AVL tree is that it takes O(log n) time to perform
search, insert, and delete operations in an average case as well as the worst case
because the height of the tree is limited to O(log n).
Balance Factor
• The structure of an AVL tree stores an additional variable called the BalanceFactor.
Thus, every node has a balance factor associated with it.
• The balance factor of a node is calculated by subtracting the height of its right sub-
tree from the height of its left sub-tree.
Balance factor = Height (left sub-tree) – Height (right sub-tree)
BF =
BF =
Operations on AVL Trees
1. Searching for a Node in an AVL Tree
• Searching in an AVL tree is performed exactly the same way as it is performed
in a binary search tree.
• Due to the height-balancing of the tree, the search operation takes O(log n)
time to complete.
• Since the operation does not modify the structure of the tree, no special
provisions are required.
Operations on AVL Trees
2. Inserting a New Node in an AVL Tree
• Insertion in an AVL tree is also done in the same way as it is done in a binary
search tree. In the AVL tree, the new node is always inserted as the leaf node.
• But the step of insertion is usually followed by an additional step of rotation.
Rotation is done to restore the balance of the tree.
• However, if insertion of the new node does not disturb the balance factor, that
is, if the balance factor of every node is still –1, 0, or 1, then rotations are not
required.
If we insert a new node with the value 30, then the new tree will still be balanced and no
rotations will be required in this case.
After inserting a new node with the value 71, note that there are three nodes in the tree that
have their balance factors 2, –2, and –2, there by disturbing the AVLness of the tree. So, here
comes the need to perform rotation.
To perform rotation,
1. First task is to find the critical node.
• Critical node is the nearest ancestor node on the path from the inserted node to the root
whose balance factor is neither –1, 0, nor 1. In the tree given above, the critical node is 72.
2. The second task in rebalancing the tree is to determine which type of rotation has to be
done. There are four types of rebalancing rotations and application of these rotations depends
on the position of the inserted node with reference to the critical node.
• LL rotation The new node is inserted in the left sub-tree of the left sub-tree of the critical
node.
• RR rotation The new node is inserted in the right sub-tree of the right sub-tree of the critical
node.
• LR rotation The new node is inserted in the right sub-tree of the left sub-tree of the critical
node.
• RL rotation The new node is inserted in the left sub-tree of the right sub-tree of the critical
node.
LL rotation
• Tree (a) is an AVL tree. In tree (b), a new node is inserted in the left sub-tree of the left
sub-tree of the critical node A (node A is the critical node because it is the closest
ancestor whose balance factor is not –1, 0, or 1), so we apply LL rotation as shown in tree
(c).
• While rotation, node B becomes the root, with T1 and A as its left and right child. T2 and
T3 become the left and right sub-trees of A.
Consider the AVL tree given in Figure and insert 18 into it.
Consider the AVL tree given in Figure and insert 18 into it.
Consider the AVL tree given in Figure and insert 18 into it.
RR rotation
Tree (a) is an AVL tree. In tree (b), a new node is inserted in the right sub-tree of
the right sub-tree of the critical node A (node A is the critical node because it is the
closest ancestor whose balance factor is not –1, 0, or 1), so we apply RR rotation
as shown in tree (c). Note that the new node has now become a part of tree T3.
While rotation, node B becomes the root, with A and T3 as its left and right child.
T1 and T2 become the left and right sub-trees of A.
Consider the AVL tree given in Figure and insert 89 into it.
Consider the AVL tree given in Figure and insert 89 into it.
Consider the AVL tree given in Figure and insert 89 into it.
LR rotation
• Tree (a) is an AVL tree. In tree (b), a new node is inserted in the right sub-tree of the
left sub-tree of the critical node A (node A is the critical node because it is the closest
ancestor whose balance factor is not –1, 0 or 1), so we apply LR rotation as shown in
tree (c). Note that the new node has now become a part of tree T2.
• While rotation, node C becomes the root, with B and A as its left and right children.
Node B has T1 and T2 as its left and right sub-trees and T3 and T4 become the left
and right sub-trees of node A.
Consider the AVL tree given in Figure and insert 37 into it.
Consider the AVL tree given in Figure and insert 37 into it.
Consider the AVL tree given in Figure and insert 37 into it.
RL rotation
• Tree (a) is an AVL tree. In tree (b), a new node is inserted in the left sub-tree of the right sub-
tree of the critical node A (node A is the critical node because it is the closest ancestor whose
balance factor is not –1, 0, or 1), so we apply RL rotation as shown in tree (c). Note that the
new node has now become a part of tree T2.
• While rotation, node C becomes the root, with A and B as its left and right children. Node A
has T1 and T2 as its left and right sub-trees and T3 and T4 become the left and right sub-
trees of node B.
Construct an AVL tree by inserting the following elements in the given order.
63, 9, 19, 27, 18, 108, 99, 81.
Construct an AVL tree by inserting the following elements in the given order.
63, 9, 19, 27, 18, 108, 99, 81.
Construct an AVL tree by inserting the following elements in the given order.
63, 9, 19, 27, 18, 108, 99, 81.
Construct an AVL tree by inserting the following elements in the given order.
63, 9, 19, 27, 18, 108, 99, 81.
Construct an AVL tree by inserting the following elements in the given order.
63, 9, 19, 27, 18, 108, 99, 81.
Construct an AVL tree by inserting the following elements in the given order.
63, 9, 19, 27, 18, 108, 99, 81.
Construct an AVL tree by inserting the following elements in the given order.
63, 9, 19, 27, 18, 108, 99, 81.
Construct an AVL tree by inserting the following elements in the given order.
63, 9, 19, 27, 18, 108, 99, 81.
Construct an AVL tree by inserting the following elements in the given order.
63, 9, 19, 27, 18, 108, 99, 81.
Construct an AVL tree by inserting the following elements in the given order.
63, 9, 19, 27, 18, 108, 99, 81.