Home/Blog/Programming/Insertion and deletion of nodes in an AVL tree
Home/Blog/Programming/Insertion and deletion of nodes in an AVL tree

Insertion and deletion of nodes in an AVL tree

26 min read
Nov 23, 2023
content
Binary search tree
AVL tree
Basic terminology
Inserting a node in an AVL tree
Single rotation
Double rotation
Types of balancing operations
Deleting a node from a BST
Deleting node from an AVL tree
Deleting an internal node
Propagation of effect of delete
Double rotation as a single operation
Summary
References

Become a Software Engineer in Months, Not Years

From your first line of code, to your first day on the job — Educative has you covered. Join 2M+ developers learning in-demand programming skills.

Binary search tree#

A binary search tree (BST) is a recursive data structure in which each node has at most two children, and the value of the left child is less than that of the right child according to the chosen property. Recursive means that each node in a binary search tree and its descendants form a binary search tree. A logical outcome of the above definition is that the value of the left child must be less than that of the right child of each node, according to the chosen property.

New York City has a population of 8.8 million and an area of 783 km2, Los Angeles has a population of 3.9 million and an area of 1299 km2, and Chicago has a population of 2.7 million and an area of 600 km2. If a binary search tree is constructed using city names, the tree may look like the following.

Binary search tree using city names as metric
Binary search tree using city names as metric

If we build the binary search tree using the city area property, it may look like the following.

Binary search tree using city area as metric
Binary search tree using city area as metric

It is important to note that the structure of a simple binary search tree solely depends upon the sequence in which nodes are inserted in it. If the name property is used and the insertion sequence is Chicago, Los Angeles, and New York, the resulting binary search tree will look like as shown below.

Binary search tree obtained by inserting Chicago, Los Angeles and New York in sequence
Binary search tree obtained by inserting Chicago, Los Angeles and New York in sequence

If a binary search tree is built by inserting Honolulu, Huntsville, Lakewood, Boston, Baltimore, Chicago, Newark, and Orlando in sequence, we get the following tree. The numbers next to the city names show the insertion sequence.

Inserting US city names in a binary search tree
Inserting US city names in a binary search tree

If we now insert Pittsburg and Quincy in the above binary search tree it looks like the following.

Inserting two more city names in the binary search tree
Inserting two more city names in the binary search tree

The above tree is indeed a binary search tree, but the right side looks more like a linked list. The search operation in such trees requires more than ln2N\ln_2Noperations, NN in the worst case, where N is the total number of nodes.

AVL tree#

AVL trees solve the problem associated with the binary search trees like the one shown above. AVL trees maintain the balance by performing certain operations on the tree, after nodes are inserted. Let's explore how AVL tree insertion and deletion works.

Basic terminology#

We define some terms which will be used in the discussion of AVL trees.

  1. The node in a tree which does not have a parent node is called the root of that tree.

  2. A node which does not have any child is called a leaf node.

  3. Two nodes that share the same parent are called siblings.

  4. All nodes from a given node to the root node of a tree are called its ancestors.

  5. All nodes from a given node to leaf nodes, including the leaf nodes, are called its descendants.

  6. The maximum number of edges from any node to a leaf node is called the height of the tree or subtree rooted at that node. The height of the tree shown above is 6. The height of the tree or subtree with Boston as root is 1. The height of the tree or subtree with Newark as root is 3.

  7. The difference between the heights of the right and the left subtrees of a node is called a balance factor of the node.

In an AVL tree the balance factor of each node must be 1-1, 00, or +1+1. If, on inserting a new node or deleting an existing node, the balance factor of any node becomes 2−2 or +2+2, appropriate operations are performed on the tree to bring the balance factor of that node within the permissible range.

Inserting a node in an AVL tree#

We start by inserting Honolulu, Huntsville, Lakewood, Boston, Baltimore, Chicago, Newark, and Orlando in sequence, and track how the balance factor changes after each insertion. After inserting Honolulu, the tree looks like this.

The first node inserted becomes the root node of the tree
The first node inserted becomes the root node of the tree

Huntsville comes after Honolulu in lexicographic order, so it becomes the right child of Honolulu. New balance factors of both nodes are within the permissible range. Therefore, the tree is still an AVL tree.

Tree after inserting Huntsville
Tree after inserting Huntsville

Lakewood comes after Huntsville, hence, it becomes the the right child of Huntsville and the tree looks like shown below.

This is no longer an AVL tree and the highlighted nodes form the axis of rotation
This is no longer an AVL tree and the highlighted nodes form the axis of rotation

The balance factor of Honolulu is +2+2 and the tree is no longer an AVL tree. If we somehow make Huntsville the root of the tree and Honolulu its left child, the balance factor of all nodes would become 00 and the tree would again become an AVL tree.

Single rotation#

Treating the arrow from Honolulu to Huntsville as axis of rotation and, keeping Huntsville fixed, perform a counter-clockwise rotation, making Huntsville the root of the tree and Honolulu its left child as shown below. All balance factors are 0 and the tree becomes a valid AVL tree.

A counter-clockwise rotation brings all balance factors within the permissible range
A counter-clockwise rotation brings all balance factors within the permissible range

Next node to insert is Boston. Boston must become the left child of Honolulu. After the insertion, the tree looks like the following. All balance factors are within the permissible range.

AVL tree after inserting Boston
AVL tree after inserting Boston

The next node to insert is Baltimore. Baltimore comes before Boston in the lexicographic order, hence, it must become the left child of Boston. After inserting Baltimore, the tree looks like as shown below.

The AVL tree loses balance after inserting Baltimore
The AVL tree loses balance after inserting Baltimore

The balance factor of Honolulu is outside the permissible range. So is the balance factor of Huntsville. After inserting a new node, we go up the tree, updating balance factors, and stop at the first node whose balance factor is outside the permissible range. Therefore, we stop at Honolulu and do not go to Huntsville to update its balance factor. That is why there is a question mark after 3-3 in the above tree.

A balance factor of 2-2 indicates the left subtree is bigger than the right one. A clockwise rotation is expected to bring the balance factor within the permissible range. The axis of rotation is Honolulu-Boston as shown below.

Axis for clockwise rotation
Axis for clockwise rotation

Keeping Boston fixed, perform a clockwise rotation to make Honolulu the right child of Boston, and make Boston the left child of Huntsville, as shown below.

The tree after clockwise rotation
The tree after clockwise rotation

It is interesting to note that after performing the clockwise rotation, the balance factor of the node above the axis of rotation, Huntsville in this case, remains unchanged. Before inserting Baltimore, the balance factor of Huntsville was 1-1, and after performing the clockwise rotation, it is still 1-1. This is an interesting and useful result.

The next node in the list is Chicago. Chicago comes after Boston but before Honolulu in the lexicographic order. Hence, it must become the left child of Honolulu. After inserting Chicago, the tree looks like as shown below.

The tree is no longer an AVL tree because the balance factor of the root node is -2
The tree is no longer an AVL tree because the balance factor of the root node is -2

The balance factor of 2-2 shows the left subtree of Huntsville is bigger than the right one. As before, we expect a clockwise rotation would bring the balance factor of the root node within the permissible range. The axis of rotation is Huntsville-Boston as shown below.

The tree is no longer an AVL tree because the balance factor of the root node is -2
The tree is no longer an AVL tree because the balance factor of the root node is -2

Keeping Boston fixed, we rotate Huntsville clockwise to make it the right child of Boston, which incidentally becomes the new root node. The right subtree of Boston is now orphaned as Huntsville has taken its place. That orphaned subtree naturally becomes the left subtree of Huntsville which is without a left subtree after becoming the right child of Boston. The result is shown below.

The tree after a clockwise rotation around the Huntsville-Boston axis
The tree after a clockwise rotation around the Huntsville-Boston axis

This rotation did not work! Before the clockwise rotation, Huntsville had a balance factor of 2-2 and after the operation, the new root node Boston has a balance factor of +2+2. We need to find out why the rotation worked in earlier cases and not in this case. If we look at all the previous rotations, the nodes forming the axis of rotation had balance factor with the same sign. In this failed case, the nodes forming the axis of rotation had opposite signs. That is the reason the rotation operation failed to bring the balance factor within the permissible range.

Double rotation#

The solution is simple; make the balance factor of Boston negative. The tree, before the rotation operation, was in the state shown below.

The tree after inserting Chicago
The tree after inserting Chicago

We want to make the balance factor of Boston negative. To accomplish this, we perform a counter-clockwise rotation on the Boston-Honolulu axis as shown below.

The axis of rotation for the first of the two rotations
The axis of rotation for the first of the two rotations

Keeping Honolulu fixed, rotate Boston counter-clockwise to become the left child of Honolulu. Chicago is orphaned by this operation and must become the right child of Boston which does not have a right child after rotation. Here is what the resulting tree looks like.

Adjusting the balance factor sign
Adjusting the balance factor sign

Once the balance factors have the same sign, we perform a clockwise rotation on the Huntsville-Honolulu axis. Honolulu becomes the new root node, and Huntsville becomes its right subtree, as shown below.

The above operation is called a double rotation. A double rotation is required when the nodes on the axis of rotation have balance factors of opposite signs. If balance factors have the same sign, a single clockwise or counter-clockwise rotation is sufficient to bring the balance factor within the permissible range.

Resulting AVL tree after inserting Chicago
Resulting AVL tree after inserting Chicago

The next node to insert is Newark. It comes after Lakewood in the lexicographic order. Hence, it must become the right child of Lakewood, as shown below.

The tree after inserting Newark
The tree after inserting Newark

Axis of rotation is Huntsville-Lakewood. Both nodes have positive balance factors. A single counter-clockwise rotation is required. After the counter-clockwise rotation, the tree looks as shown below.

The tree after inserting Newark
The tree after inserting Newark

Types of balancing operations#

When the balance factor of a node exceeds the permissible range, one of the four possible types of rotations must be performed to bring the balance factor within the permissible range.

We first consider the case in which the balance factor of a node goes from +1+1 to +2+2. The state of the tree before the insertion that causes imbalance is shown below. T1T_1, T2T_2, and T3T_3 are AVL trees with height hh.

State of AVL tree before insertion that leads to imbalance at node A
State of AVL tree before insertion that leads to imbalance at node A

The balance factor of node BB is 00 and that of node AA is +1+1. The tree with root node AA is a balanced tree. Any new node inserted in the tree would end up in either the subtree T1T_1 or the subtree T2T_2 or the subtree T3T_3. If the new node is inserted in T1T_1, the balance factor of node AA would decrease from +1+1 to 00. This is not what we want—we want the balance factor of nodeAA to increase from +1+1 to +2+2. This can only happen if the new node is inserted in either the subtree T2T_2 or the subtree T3T_3. It is important to keep in mind that after the insertion operation, node AA is the first node which becomes unbalanced; neither of its subtrees lose balance. We will discuss both cases but first consider the case where the new node is inserted in T3T_3 causing its height to increase from hhto h+1h+1.

A new node is inserted in tree T_3
A new node is inserted in tree T_3

The balance factor of node BB will increase from 00 to +1+1, and that of node AA will increase from +1+1 to +2+2. A rotation operation is required to bring the balance factor of node AAwithin the permissible range. The axis of rotation is ABA\rule[0.25em]{0.15in}{0.5pt} B and both nodes have positive balance factors. A single counter-clockwise rotation is required. After performing the rotation operation, node BB becomes the new root node and node AA, along with its left subtree T1T_1, becomes the left child of node BB. The left subtree of node BB is orphaned and must be attached to node AA which, after rotation, has no right child or subtree. The state of the tree after the rotation operation is shown below.

State of the tree after the counter-clockwise rotation
State of the tree after the counter-clockwise rotation

We now talk about the case where the new node ends up in subtree T2T_2. This changes the balance factor of node BBfrom 00 to 1-1. The balance factor of node AA goes from +1+1 to +2+2 just as in the previous case where the new node ended up in the subtree T3T_3.

A new node is inserted in tree T_2
A new node is inserted in tree T_2

Once again, the node that loses balance is AA, and the axis of a counter-clockwise rotation is ABA\rule[0.25em]{0.15in}{0.5pt} B. However, in this case the balance factors of the nodes have opposite signs. Hence, a double rotation is required; first rotation to make the balance factor of node BB positive, followed by the second rotation to bring all balance factors within the permissible range.

The axis of rotation to make the balance factor of node BB positive, by performing a clockwise rotation, consists of node BB and its left child. We have to zoom in on subtree T2T_2, to reveal the left child of node BB as shown below.

A new node ends up in either T_2L or T_2R
A new node ends up in either T_2L or T_2R

Node CC is the left child of node BB. While we know the new node ends up in the left subtree of node BB, we do not know whether it ends up in the subtree T2LT_{2L} or the subtree T2RT_{2R}. Furthermore, the height of the tree with node CC as its root must change after insertion because otherwise the height of the left subtree of node BB would not change, and as a result, that of node AA would also not change. This leads us to the conclusion that before the insertion both subtrees T2LT_{2L} and T2RT_{2R} had height h1h-1 and after the insertion one of them has height hh and the other one has height h1h-1. This is depicted in the above figure by dotted lines in subtrees T2LT_{2L} and T2RT_{2R}.

After performing a clockwise rotation on BCB\rule[0.25em]{0.15in}{0.5pt} C axis with CC fixed, the balance factor of the new right child of node AA becomes positive, as shown in the figure below.

A single clockwise rotation to make the balance factor positive
A single clockwise rotation to make the balance factor positive

Since the balance factors of both node AA and its right child node CC are positive, we can perform a counter-clockwise rotation along ACA\rule[0.25em]{0.15in}{0.5pt} C axis with node CC fixed. The node CC becomes the new root node of the tree and node AA becomes the left child of node CC, as shown below.

A single counter-clockwise rotation to make the balance factor of root node 0
A single counter-clockwise rotation to make the balance factor of root node 0

The balance factor of the new root node (node CC) is 00. The left subtree of node CC has a balance factor of 00 or 1-1, depending on the height of T2LT_{2L}. Similarly the balance factor of the right subtree of node CC is either +1+1 or 00, depending on the height of height of T2RT_{2R}.

In the foregoing paragraphs we discussed operations to perform when the balance factor of a node becomes +2+2. If the balance factor of a node becomes 2-2, operations similar to the ones described above are performed to bring the balance factor of the node within the permissible range. If balance factors of the unbalanced node and its left child have the same sign, only a single clockwise rotation is required, as shown below.

A new node is inserted in tree T_1
A new node is inserted in tree T_1

After the single clockwise rotation, node BB becomes the new root note of the tree and node AA becomes its right child and the subtree T2T_2 becomes the left subtree of node AA, as shown below.

The tree after a single clockwise rotation
The tree after a single clockwise rotation

If the balance factors have opposite signs, a counter-clockwise rotation followed by a clockwise rotation is required. In the figure shown below, the balance factors of nodes AA and BB are of opposite signs. A single rotation along the ABA\rule[0.25em]{0.15in}{0.5pt} B axis will not fix the imbalance.

A new node is inserted in tree T_1
A new node is inserted in tree T_1

To make the balance factor of node BB negative, we have to zoom in on the subtree T1T_1 as shown in the figure below. The right child of node BB is labeled CC, and the left and the right subtrees of node CC are labeled T2LT_{2L} and T2RT_{2R}in the figure. As discussed previously, the height of one of the subtrees of node CC is hh and that of the other one is h1h-1.

A new node ends up in either T_2L or T_2R
A new node ends up in either T_2L or T_2R

We first perform a single counter-clockwise rotation to make the balance factor of node BB negative. The first rotation is performed on BCB\rule[0.25em]{0.15in}{0.5pt} C axis with node CC fixed. The rotation is counter-clockwise. The result of this rotation is shown below. The new left child of node AA is node CC.

A single counter-clockwise rotation makes the balance factor of the left child of node A negative
A single counter-clockwise rotation makes the balance factor of the left child of node A negative

In the figure above, node AA and its left child have balance factors of the same sign. A single clockwise rotation about ACA\rule[0.25em]{0.15in}{0.5pt} C axis, with node CC fixed, is required. The new root node of the tree is node CC, as shown below.

A single counter-clockwise rotation makes the balance factor of the left child of node A negative
A single counter-clockwise rotation makes the balance factor of the left child of node A negative

If the balance factor of node C is 1-1, after the tri-node rebalance, the left child of the new root node will have a balance factor of 00 and the right child will have a balance factor of +1+1. If the balance factor of node C is +1+1, after the tri-node rebalance, the left child of the new root node will have a balance factor of 1-1 and the right child will have a balance factor of 0. If the balance factor of node C is 00, after the tri-node rebalance, both children of the new root node will have a balance factor of 00.

Deleting a node from a BST#

Deleting a node from a binary search tree is slightly more involved than insertion. Deleting a leaf node is simple; just update the parent node and we are done. If the node to be deleted has only one subtree, that subtree is attached to the parent of the deleted node and we are done. If, however, the node to be deleted has both left and right subtrees, we have to bring another node in its place. Consider the binary search tree shown below:

Delete node 70 from the tree
Delete node 70 from the tree

Suppose we want to delete the node 70. This node has two subtrees. We want to maintain the binary search property of the tree after the node 70 is deleted. There are only two options. We can replace the node 70 with the smallest value node (leftmost node) of its right subtree or the largest value node (rightmost node) of its left subtree.

The leftmost node of the right subtree has the smallest value in the right subtree and is greater than that of the node we want to delete. The rightmost node of the left subtree has the largest value in the left subtree and is smaller than that of the node we want to delete. The right subtree of node 70 is shown below.

Right subtree of node 70
Right subtree of node 70

The leftmost node of the above tree is 75. This node can replace the node 70 while maintaining the binary search property of the tree. Moving node 75 to the position of the deleted node 70 will orphan the right subtree of node 75. We attached the right subtree of node 75 with its parent which is node 80, as shown below.

Right subtree node 75 is attached to node 80
Right subtree node 75 is attached to node 80

The state of the binary search tree after deleting node 70 and bringing node 75 in its place is shown below.

State of the binary search tree after deleting node 70
State of the binary search tree after deleting node 70

Instead of replacing the node 70 with the leftmost node of its right subtree, let's replace it with the rightmost node of its left subtree. The left subtree of node 70 is shown below.

Left subtree of node 70
Left subtree of node 70

The rightmost node of the above tree is node 65. We can replace node 70 with this node and the binary search property of the tree will be maintained. We attached the left subtree of node 65 with its parent, which is node 55, as shown below.

Left subtree of node 70 after removing node 65
Left subtree of node 70 after removing node 65

The state of the tree after replacing node 70 with node 65 is shown below.

State of the tree after replacing node 70 with the rightmost node of its left subtree
State of the tree after replacing node 70 with the rightmost node of its left subtree

Deleting node from an AVL tree#

Let's consider the AVL tree of some three-digit prime numbers shown below. The numbers within the parentheses are the balance factors of the nodes.

An AVL tree of three digit prime numbers
An AVL tree of three digit prime numbers

When a node is deleted from an AVL tree, the heights and balance factors of all its ancestor nodes may change. If we delete the node 211, the heights and balance factors of all highlighted nodes may change, as shown below.

Nodes which may be affected by deleting the leaf node 211
Nodes which may be affected by deleting the leaf node 211

Before deleting node 211, the balance factor of its parent node 197 was +1+1 and that of its grandparent node 227 was 00. After deleting the node, the new balance factors are 00 and +1+1 respectively. The balance factor of root node 179 is not changed. The state of the AVL tree after deleting node 211 is shown below.

Nodes with updated balance factors after deleting 211
Nodes with updated balance factors after deleting 211

After the delete operation, if the balance factor of any node goes from 00 to +1+1 or from 00 to 1-1, the height of the tree rooted at that node does not change. If, on the other hand, after the delete operation, the balance factor of any node goes from +1+1 to or from 1-1 to 00, the height of the tree rooted at that node changes. The height is reduced. If the balance factor, after delete operation, goes from +1+1 to +2+2 or from 1-1 to 2-2, single or double rotation is required at that node. The following table lists all cases.

Delete

Balance Factor

Change in Height

Balancing Required

0 to +1

None

No

0 to -1

None

No

+1 to 0

Negative

Not at this node, may be up the tree

-1 to 0

Negative

Not at this node, may be up the tree

+1 to +2

Negative

Yes

-1 to -2

Negative

Yes

Continuing our discussion, let's delete the leaf node 197. The nodes affected by this operation are highlighted below, along with their balance factors. The parent of the deleted node, as a result of delete operation, has become unbalanced.

Nodes with updated balance factors after deleting 197
Nodes with updated balance factors after deleting 197

A counter-clockwise rotation operation must be performed to balance the tree. The axis of rotation is 227241227\rule[0.25em]{0.15in}{0.5pt}241, highlighted below.

The axis of rotation to fix the imbalance caused by deleting node 197
The axis of rotation to fix the imbalance caused by deleting node 197

The two nodes forming the axis of rotation have balance factors of opposite signs. Therefore, a single rotation will not fix the imbalance. A double rotation is required. The first rotation will make the balance factor of child of node 227 positive, and the second one will remove the imbalance of node 227. The axis of the first rotation is 241233241\rule[0.25em]{0.15in}{0.5pt}233, and is highlighted below.

The axis of the first rotation to fix imbalance of node 227
The axis of the first rotation to fix imbalance of node 227

A clockwise rotation makes node 233 the right child of node 227, and node 241 becomes the right child of node 233, as shown below, with the axis of second rotation highlighted.

The state of the tree after the first rotation
The state of the tree after the first rotation

After the second rotation, which is counter-clockwise rotation about 227233227\rule[0.25em]{0.15in}{0.5pt}233 axis, the state of the tree is shown below.

The state of the tree after fixing the imbalance of the right child of the root node
The state of the tree after fixing the imbalance of the right child of the root node

The height of the right subtree of the root node has reduced and the root node itself has become unbalanced. We have to fix the imbalance. The balance factor of the root node is 2-2. A clockwise rotation is required. The axis of rotation is 179137179\rule[0.25em]{0.15in}{0.5pt}137, as shown below.

Axis of rotation to fix imbalance of the root node
Axis of rotation to fix imbalance of the root node

Since the balance factors of the nodes forming the axis of rotation have opposite signs, a single rotation will not restore balance. A double rotation is required; a counter-clockwise rotation about 137157137\rule[0.25em]{0.15in}{0.5pt}157 to make the balance factor of the left child of the root node negative, followed by a clockwise rotation to balance the root node.

The axis of rotation for the first of the two rotations
The axis of rotation for the first of the two rotations

When the counter-clockwise rotation is performed, node 157 becomes the left child of the root node, node 137 becomes the right child of node 157, and the left subtree of node 157 becomes the right subtree of node 137, as shown below.

The state of the tree after the first of the two rotations
The state of the tree after the first of the two rotations

The new left child of the root node is 157, and the new axis for the second of the two rotations is 179157179\rule[0.25em]{0.15in}{0.5pt}157, as shown below.

The axis of rotation for the second of the two rotations
The axis of rotation for the second of the two rotations

Since both nodes of the axis of rotation have balance factors of the same sign, a clockwise rotation will fix the imbalance of the new root node, as shown below.

The state of the tree after the root node imbalance is fixed
The state of the tree after the root node imbalance is fixed

We have seen that deleting a leaf node caused two double rotations to restore the balance and bring in a new root node. This is how the delete operation is different from the insert operations. When a node becomes unbalanced after an inert operation, one single or one double rotation restores the balance as well as the balance factors of the nodes above that level do not change. In contrast, when a node becomes unbalanced after a delete operation, restoring the balance by performing a single or a double rotation may cause a ripple effect all the way up to the root node.

Deleting an internal node#

The two nodes that we deleted above, and balanced the tree afterwards, were leaf nodes. We now consider the case in which a non-leaf node is deleted. We will see the steps required to balance the tree when an internal (non-leaf) node is deleted. Let's consider the AVL tree below.

An AVL tree of three-digit prime numbers
An AVL tree of three-digit prime numbers

We want to delete node 227 from the AVL tree shown above. This node has both left and right subtrees. To delete this node, we have to replace it with the rightmost node of its left subtree or the leftmost node of its right subtree. We replace the node to delete with the rightmost node of its left subtree. This is node 211.

The rightmost node of the left subtree of node 227 is highlighted
The rightmost node of the left subtree of node 227 is highlighted

Moving node 211 to the position of the deleted node makes node 197 unbalanced, as shown below.

A node has become unbalanced as a result of moving node 211
A node has become unbalanced as a result of moving node 211

A balance factor of 2-2 means a clockwise rotation is required. The axis of rotation would be the unbalanced node and its left child, 197191197\rule[0.25em]{0.15in}{0.5pt}191 in this case.

The axis of rotation to fix imbalance of node 197
The axis of rotation to fix imbalance of node 197

The two nodes forming the axis of rotation have balance factors of opposite signs. A single rotation would not work. A double rotation is required; a counter-clockwise rotation on 191193191\rule[0.25em]{0.15in}{0.5pt}193 axis, followed by a clockwise rotation.

The axis of rotation for the first of the two rotations
The axis of rotation for the first of the two rotations

The state of the tree after the counter-clockwise rotation is shown below.

The axis of rotation for the second of the two rotations
The axis of rotation for the second of the two rotations

Since both nodes have balance factors of the same sign, a single clockwise rotation will balance the nodes, as shown below.

The state of the tree after fixing the imbalance of the right child of the root node
The state of the tree after fixing the imbalance of the right child of the root node

Propagation of effect of delete#

In the above example, the root node was not affected. Let's look at an example where the root node will also be affected. Consider the AVL tree shown below.

An AVL tree of prime numbers
An AVL tree of prime numbers

The above AVL tree is a slightly modified version of the one discussed previously. We have added a two-digit prime number to the tree to illustrate delete operation on an internal node. Once again, we delete node 227. After deleting the node 227 and fixing the imbalance, the state of the tree is shown below.

The state of the tree after deleting node 227 and rebalancing the right child of the root node
The state of the tree after deleting node 227 and rebalancing the right child of the root node

The double rotation operation shown above has made the root node unbalanced. A clockwise rotation is required to fix the imbalance of the root node. The axis of rotation is 179137179\rule[0.25em]{0.15in}{0.5pt}137, and both nodes of the axis have negative balance factors. Therefore, a single clockwise rotation fixes the imbalance of the new root node, as shown below.

The final state of the AVL tree after deleting node 227, and fixing all imbalances
The final state of the AVL tree after deleting node 227, and fixing all imbalances

Double rotation as a single operation#

Now that we have discussed the insert and the delete operation with examples, it is important to point out that double rotations as discussed above were meant to make it easier to understand the balancing operation when the nodes forming the axis of rotation have balance factors of opposite signs. While implementing this operation on a computer, we only look at the signs of the balance factors and re-arrange all three nodes in one step. We may call this operation a tri-node rearrangement or a tri-node rebalance. We show the operation in the following figure.

Double rotation or tri-node rearrangement in one step
Double rotation or tri-node rearrangement in one step

Summary#

Following are the main points of the above discussion about AVL tree insertion and deletion:

  • Insert operation either leaves the height of a tree and its subtrees unchanged or increases it.

    • If the height of a tree/subtree remains unchanged, the balance factors of all ancestor nodes remain unchanged.

    • If the height of a tree/subtree increases, the balance factor of its immediate ancestor changes but the height of its immediate ancestor may or may not change.

    • The balance factor of a node may change from

      • 12-1\to-2 (height increases)

      • 10-1\to0 (height unchanged)

      • 010\to-1 (height increases)

      • 0+10\to+1 (height increases)

      • +10+1\to0 (height unchanged)

      • +1+2+1\to+2 (height increases)

    • If the balance factor of a node becomes 2-2, and the balance factor of its left child is negative, a single clockwise rotation fixes the imbalance.

    • If the balance factor of a node becomes 2-2, and the balance factor of its left child is positive, a double rotation (counter-clockwise rotation followed by clockwise rotation) fixes the imbalance.

    • If the balance factor of a node becomes +2+2, and the balance factor of its right child is positive, a single counter-clockwise rotation fixes the imbalance.

    • If the balance factor of a node becomes +2+2, and the balance factor of its right child is negative, a double rotation (clockwise rotation followed by counter-clockwise rotation) fixes the imbalance.

    • Once a single or a double rotation is performed, the balance factors of all nodes above that level remain unchanged.

  • The delete operation leaves the height of a tree and its subtrees unchanged or decreases it.

    • If the height of a tree/subtree remains unchanged, the balance factors of all ancestor nodes remain unchanged.

    • If the height of a tree/subtree is reduced, the balance factors of, at least some, ancestor nodes change

    • The balance factor of a node may change from

      • 12-1\to-2

      • 10-1\to0

      • 010\to-1

      • 0+10\to+1

      • +10+1\to0

      • +1+2+1\to+2

    • If the balance factor of a node becomes +2+2 or 2-2, and the balance factor of the appropriate child node has the same sign, a single rotation fixes the imbalance.

    • If the balance factor of a node becomes +2+2 or 2-2, and the balance factor of the appropriate child node has the opposite sign, a double rotation fixes the imbalance. We also call it tri-node rearrangement or tri-node rebalance.

    • After a single or a double rotation, the height of the tree is reduced, and this may lead to more single or double rotations further up the tree.

    • Unlike the insert operation, the effect of the delete operation must be checked all the way up to the root node and may bring in a new node at root level.

References#

Explore the following courses from Educative to learn more about AVL trees.

  1. Advanced Programming Techniques in C

  2. Data Structures for Coding Interviews in Java

  3. Data Structures for Coding Interviews in C++


Written By:

Free Resources