Hello! Welcome to your next lesson.
In our last session, we established a crucial, if somewhat unsettling, fact about Binary Search Trees: while they promise logarithmic time complexity, this is not a guarantee. We saw how inserting sorted data can degrade a BST into a linked list, pushing its performance from an efficient to a costly .
This lesson addresses the engineering solution to that problem. Your goal is to explain the rebalancing mechanism of an AVL or Red-Black tree using rotations. We will focus primarily on AVL trees as they provide a clear and intuitive introduction to the core concepts of self-balancing. Understanding this mechanism is a key step toward an architectural mindset—it’s about appreciating the "self-healing" systems that provide performance guarantees where simpler structures fail.
1. The AVL Tree: A Stricter Binary Search Tree
The problem with a standard BST is its lack of rules about structure. An AVL tree (named after its inventors, Adelson-Velsky and Landis) solves this by adding one simple but powerful constraint.
An AVL tree is a binary search tree where for every node, the heights of its left and right subtrees can differ by at most 1.
To enforce this, we introduce the concept of a Balance Factor (BF) for each node:
In an AVL tree, the balance factor of every node must be -1, 0, or 1. If an insertion or deletion causes any node's balance factor to become -2 or +2, the tree is considered "imbalanced," and a rebalancing procedure is triggered immediately.
To get a clear definition of AVL trees and the balance factor, let's watch the beginning of this excellent video from Abdul Bari.
10.1 AVL Tree - Insertion and Rotations
This video section defines what an AVL tree is and how the balance factor is calculated for each node to determine if the tree is balanced.
Watch from 13:50 to 17:43. Focus on how the balance factor is defined and used to identify an imbalanced node.
2. Rotations: The Rebalancing Mechanism
When a node becomes imbalanced, the tree performs rotations to restore the AVL property. A rotation is a local transformation of pointers that changes the structure of the tree while preserving the binary search tree property (i.e., the in-order traversal remains the same).
An imbalance is always caused by an insertion into one of four possible locations relative to the first imbalanced node found while moving up from the inserted leaf. These four cases give rise to four types of rotations.

The four cases are:
- Left-Left (LL) Case: An insertion into the left subtree of the left child of the imbalanced node. Fixed by a single right rotation.
- Right-Right (RR) Case: An insertion into the right subtree of the right child. Fixed by a single left rotation.
- Left-Right (LR) Case: An insertion into the right subtree of the left child. Fixed by a double rotation (a left rotation followed by a right rotation).
- Right-Left (RL) Case: An insertion into the left subtree of the right child. Fixed by a double rotation (a right rotation followed by a left rotation).
Let's break down each of these rotations.
3. A Deep Dive into the Four Rotation Cases
The following video segments provide a clear, step-by-step visualization of how each rotation works. As you watch, notice how nodes and their subtrees are rearranged to restore balance.
10.1 AVL Tree - Insertion and Rotations
This comprehensive video by Abdul Bari demonstrates each of the four rotation types with simple, clear examples.
Watch the following segments in order: LL Rotation (Single Right Rotation): 17:43 - 20:17 LR Rotation (Double Rotation): 20:17 - 22:36 RR Rotation (Single Left Rotation): 22:36 - 23:48 RL Rotation (Double Rotation): 23:48 - 25:09
Key Observations from the Rotations:
- Single Rotations (LL and RR): These are straightforward adjustments for "outside" insertions (left-of-left or right-of-right). The middle node of the three involved nodes moves to the root of the subtree.
- Double Rotations (LR and RL): These are required for "inside" or "zigzag" insertions (right-of-left or left-of-right). A single rotation would not balance the tree. The double rotation first straightens the "zig" into a "zag" and then performs a single rotation to complete the rebalancing.
A crucial point is that these rotations don't just happen with three nodes; they correctly re-parent entire subtrees. The next video segment explains this generalization.
10.1 AVL Tree - Insertion and Rotations
This segment expands on the rotation logic to show how larger subtrees are handled during the re-parenting process.
Watch from 25:09 to 32:30. Pay close attention to how the subtrees (like B's right child in the LL case) are reattached to maintain the BST property.
Test your understanding!
Consider an LR imbalance case, where the path of nodes is a "zigzag". Why is a single right rotation on the grandparent node insufficient to balance the subtree? What would the tree look like if you tried?
Show answer
In an LR case, the imbalanced path looks like a zigzag (e.g., node A has a left child B, which has a right child C). A single right rotation on A would move B up to the root position. However, C would remain the right child of B. The tree would still be imbalanced, just in a different configuration (it would now be an RL imbalance relative to B). A double rotation is necessary to first perform a left rotation on B to straighten the path into an LL case, and then a right rotation on A to perform the final balancing.
4. Putting It All Together: Building an AVL Tree
Now that we understand the individual mechanics, let's see them in action. We'll watch the process of building a complete AVL tree, one insertion at a time. This will solidify your understanding of when and how each rotation is applied.
10.1 AVL Tree - Insertion and Rotations
This final segment from Abdul Bari's video is a practical demonstration of creating an AVL tree from scratch by inserting a sequence of keys.
Watch from 32:30 to 41:43. For each insertion, try to predict the balance factors and identify which node becomes imbalanced and what type of rotation is needed before the video reveals it.
5. An Alternative: Red-Black Trees
AVL trees are not the only type of self-balancing BST. Another widely used structure is the Red-Black Tree. The learning outcome mentions either, so it's valuable from an architectural perspective to know that alternatives exist and how they differ.
A Red-Black tree maintains balance using a different set of rules based on node coloring (red or black).
This article from Programiz provides a concise definition of a Red-Black tree and its five core properties.
Read the introductory section up to 'Operations on a Red-Black Tree'. Focus on understanding the five properties that a Red-Black tree must maintain. These properties, while more complex than the AVL balance factor, collectively ensure the tree's height remains logarithmic.
Like AVL trees, Red-Black trees use rotations to fix violations of their properties that occur after insertions or deletions. However, they also use another operation: recoloring.

AVL vs. Red-Black Trees: A Quick Comparison
- Strictness: AVL trees are more strictly balanced than Red-Black trees. This can lead to faster lookups in AVL trees.
- Modifications: The looser balancing of Red-Black trees means that insertions and deletions may require fewer rotations. In practice, many standard library implementations (like
std::mapin C++ orTreeMapin Java) use Red-Black trees because they often offer better performance for write-heavy workloads.
Conclusion
In this lesson, we demystified the "magic" behind self-balancing trees. You now understand the core mechanism that prevents a BST from degrading and guarantees efficient performance.
Key Takeaways:
- AVL Trees enforce balance by ensuring the heights of subtrees for any node differ by at most 1. The balance factor is used to detect violations.
- Rotations are the fundamental operations used to restructure the tree and restore balance when the AVL property is violated.
- There are four imbalance cases based on the insertion path: LL, RR, LR, and RL, which are fixed with single or double rotations.
- Red-Black Trees are an alternative that uses node coloring and a different set of properties, but also relies on rotations and recoloring to maintain balance.
- The choice between AVL and Red-Black trees involves a trade-off between lookup speed (stricter balance) and modification speed (fewer rotations).
By understanding rotations, you've grasped a fundamental concept in data structures that has profound implications for designing robust and performant systems.
In the next lesson, we will shift our focus back to general tree algorithms by exploring level-order traversal for a binary tree using a queue. This traversal method gives us a different way to view and process the nodes in a tree, which has its own set of practical applications.