Skip to main content

Section 32.4 AVL Trees

Now that we know how rotations can be used to restructure a tree, we can explore how to use them to maintain balance in a binary search tree. As we discussed in the previous chapter, the efficiency of many binary search tree operations depends on the height of the tree. If the tree becomes unbalanced, with some branches much deeper than others, the performance of operations like insertion, deletion, and lookup can degrade from \(O(\log n)\) to \(O(n)\text{.}\) All of the self-balancing technologies we will explore seek to prevent this and provide some guarantee that the height of the tree remains logarithmic.
The first self balancing structure we will consider is the AVL Tree, named after its inventors G.M. Adelson-Velsky and E.M. Landis. An AVL tree is a binary search tree that maintains a balance condition: for every node in the tree, the heights of the left and right subtrees can differ by at most 1. This balance condition will ensure that the height of the tree remains logarithmic in relation to the number of nodes.
To calculate the balance of nodes efficiently, each node in an AVL tree typically stores its height as an additional piece of data. (Recall that a leaf has a height of 0. This means that if we ever need to reason about the height of an empty subtree, we can consider it to be -1.) When we perform insertions or deletions, that may change the heights of nodes along the path from the modified node back up to the root. So, as we return from our recursive calls, we will need to update the heights of the nodes we visit.
We also need to check the current balance factor of the node. This value is not stored. It is calculated on demand as the difference in the heights of the left and right subtrees. (We will use the convention of height(left) - height(right) though it is arbitrary.) A balance factor of 0, 1, or -1 indicates that the node is balanced. A balance factor greater than 1 or less than -1 indicates that the node is unbalanced and requires rebalancing through rotations.
You can explore this in the animation below.

Activity 32.4.1. AVL Trees.

An AVL tree is set to insert 93. This will cause a few height updates but no rotations. Use the > button to step through the insertion process.
The number next to each node represents its height. Balance factors will be calculated and displayed temporarily but are not stored.

Instructions.

When the > button is highlighted, an animation is prepared to run. Use the Step button to step through the animation one step at a time.
When no animation is in progress, you can use the data controls to start a new animation.
You can click and drag on the animation area to pan around. Use the Ctrl + mouse wheel (or touchpad scroll gesture) while over the animation area to zoom in and out.
  • Core Controls.
    • << : Skip to the beginning of the animation.
    • < : Step backwards one step in the animation.
    • > : Step forward one step in the animation.
    • >> : Skip to the end of the animation.
    • Auto Step Speed Set to anything other than off to automatically step through the animation at the selected speed.
    • Zoom Set the zoom level. You can also use Ctrl + Mouse Wheel to zoom in and out.
  • Data Controls.
    • Value Use this area to enter a value when inserting, finding, deleting, etc...
When we find a node that is unbalanced after an insertion or deletion, we need to determine which of the four rotation cases to apply to restore balance. The four cases are:
  • Left-Left (LL) Case: The left subtree of the left child is deeper. We perform a right rotation on the unbalanced node.
  • Right-Right (RR) Case: The right subtree of the right child is deeper. We perform a left rotation on the unbalanced node.
  • Left-Right (LR) Case: The right subtree of the left child is deeper. We perform a left rotation on the left child, followed by a right rotation on the unbalanced node.
  • Right-Left (RL) Case: The left subtree of the right child is deeper. We perform a right rotation on the right child, followed by a left rotation on the unbalanced node.
Really, these are just two cases (LL and LR) and their mirror images (RR and RL). The key is to look at the direction we need to go to reach the deeper subtree. It will either be two steps in the same direction (LL or RR) or a zig-zag pattern (LR or RL).
In the β€œsame direction” case, a single rotation away from the taller child will restore balance. In the β€œzig-zag” case, we need to perform a double rotation: first a rotation on the child node to straighten out the zig-zag, and then a rotation on the unbalanced node to complete the balancing.
Use the animation below to explore these two cases.

Activity 32.4.2. AVL Trees Insertion Rotations.

First, insert 60 (type 60 into the Value text field and press Insert). Then use the > button to step through the insertion process. This will cause a Left-Left imbalance that requires a right rotation to fix.
Then press the Reset button and insert 120. This will trigger a zig-zag imbalance that requires a double rotation to fix. Step through the process to watch it unfold.

Instructions.

When the > button is highlighted, an animation is prepared to run. Use the Step button to step through the animation one step at a time.
When no animation is in progress, you can use the data controls to start a new animation.
You can click and drag on the animation area to pan around. Use the Ctrl + mouse wheel (or touchpad scroll gesture) while over the animation area to zoom in and out.
  • Core Controls.
    • << : Skip to the beginning of the animation.
    • < : Step backwards one step in the animation.
    • > : Step forward one step in the animation.
    • >> : Skip to the end of the animation.
    • Auto Step Speed Set to anything other than off to automatically step through the animation at the selected speed.
    • Zoom Set the zoom level. You can also use Ctrl + Mouse Wheel to zoom in and out.
  • Data Controls.
    • Value Use this area to enter a value when inserting, finding, deleting, etc...
The rotation can happen at any level of the tree. For example, if you insert 900 into the tree shows in the animation above (after resetting it), the imbalance will occur 509. It would have children of height 0 and of 2 and require a left rotation to fix.
In an insertion, we should only need to perform one rotation (single or double) to restore balance. This is because the insertion of a new leaf can only increase the height of the tree by 1, and that increase can only propagate up the tree until it reaches a node that was already balanced. Once we perform a rotation to fix an unbalanced node, the height of the subtree rooted at that node will be restored to what it was before the insertion, preventing further imbalances up the tree.
Deletions can be a bit more complex. Removing a node can decrease the height of the tree, which can cause multiple nodes along the path back to the root to become unbalanced. Therefore, after a deletion, we may need to perform multiple rotations (single or double) as we backtrack up the tree to restore balance at each unbalanced node.

Activity 32.4.3. AVL Trees Deletion Rotations.

First, remove 225 (click on the 225 node and then press Remove). This will cause a Right-Right imbalance that requires a left rotation to fix. Use the > button to step through the removal process.
Then press the Reset button and remove 31. This will cause a local imbalance at the 40 node. Once that is fixed, the 211 node will become unbalanced and require a second rotation to fix. Step through the process to watch it unfold.

Instructions.

When the > button is highlighted, an animation is prepared to run. Use the Step button to step through the animation one step at a time.
When no animation is in progress, you can use the data controls to start a new animation.
You can click and drag on the animation area to pan around. Use the Ctrl + mouse wheel (or touchpad scroll gesture) while over the animation area to zoom in and out.
  • Core Controls.
    • << : Skip to the beginning of the animation.
    • < : Step backwards one step in the animation.
    • > : Step forward one step in the animation.
    • >> : Skip to the end of the animation.
    • Auto Step Speed Set to anything other than off to automatically step through the animation at the selected speed.
    • Zoom Set the zoom level. You can also use Ctrl + Mouse Wheel to zoom in and out.
  • Data Controls.
    • Value Use this area to enter a value when inserting, finding, deleting, etc...

Checkpoint 32.4.1.

Checkpoint 32.4.2.

Checkpoint 32.4.3.

Assume A was just inserted. How would we fix the imbalance using AVL tree logic?
H is the root node. It has left child D and right child N. D has a left child C and a right child G. C has a left child B. B has a left child A. G has a left child E. N has a left child J and a right child Q. J has a left child I.πŸ”—
  • Rotate C right
  • Rotate B right
  • Rotate D right
  • Rotate H right
Hint.
Where is the lowest level where the balance is off by 2?

Checkpoint 32.4.4.

Assume F was just inserted. How would we fix the imbalance using AVL tree logic?
H is the root node. It has left child D and right child N. D has a left child C and a right child G. G has a left child E. E has a right child F. N has a left child J and a right child Q. J has a left child I.πŸ”—
  • Rotate E left, then G right
  • Rotate G right, then D left
  • Rotate G right
  • Rotate D left
Hint.
Where is the lowest level where the balance is off by 2? What is the path from that node to the inserted node look like?

Checkpoint 32.4.5.

Assume I was just inserted. How would we fix the imbalance using AVL tree logic?
H is the root node. It has left child D and right child N. D has a left child C and a right child G. G has a left child E. E has a right child F. N has a left child K and a right child Q. K has a left child J and a right child L. J has a left child I.πŸ”—
  • Rotate N right
  • Rotate K left, then N right
  • Rotate K right
  • Rotate J right
  • Rotate N right, then H left
Hint.
Where is the lowest level where the balance is off by 2? What is the path from that node to the inserted node look like?
You have attempted of activities on this page.