Balanced Binary Trees: Why Balance Is Needed and A Simple Explanation of Rotation Operations

Let’s start with one of the most common data structures: the Binary Search Tree (BST). A BST is a binary tree arranged with a specific rule: for any node, all values in its left subtree are less than its value, and all values in its right subtree are greater than its value. In the ideal case, operations like search, insertion, and deletion have a time complexity of O(log n), which seems very efficient.

However, if nodes are inserted in an “extreme” order—for example, always inserting to the right (or always to the left)—the BST degrades into a linked list. For instance, inserting 1, 2, 3, 4, 5 sequentially results in a long right chain. Searching then requires traversing from the root to the leaf, with a time complexity of O(n), losing the BST’s advantages.

What is “Balance”?

The core of a balanced binary tree (e.g., an AVL tree) is the balance factor. For each node, the balance factor is defined as: height of left subtree - height of right subtree. For a balanced tree, this balance factor must be -1, 0, or 1 (i.e., the height difference between left and right subtrees does not exceed 1).

For example: If a node has a left subtree height of 3 and a right subtree height of 1, its balance factor is 2, and the tree is “unbalanced”.

Why is Balance Necessary?

Imagine searching for a book in a library: if the bookshelves are “balanced” (each layer has roughly the same number of books), you find your target quickly. But if the shelves are tilted (100 books on the left, only 1 on the right), you might spend more time reaching the far side.

The height of a tree directly impacts operational efficiency. A balanced tree has a height approximately equal to log n (e.g., for n=1000, height ≈ 10), while an unbalanced tree can degenerate to O(n) height. Balanced binary trees adjust their structure to keep the height within log n, ensuring efficient search, insertion, and deletion.

Rotations: How to Balance the Tree?

When the tree is unbalanced (balance factor absolute value > 1), rotation operations are used to restructure it. There are four basic rotation types: LL, RR, LR, RL. These adjust the “pivot” to restore balance.

1. LL Type: Left-Left Imbalance (Right Rotation)

Scenario: Node A has a left subtree height 2 greater than its right subtree, and the left subtree itself has a taller left child.
Example:
Insertion order: 10, 5, 3, 1 (inserted sequentially into the left subtree), forming the tree:

    10
   /
  5
 /
3
 /
1

At this point, node 10 has a balance factor of 2 (left-heavy). A right rotation is needed.

Right Rotation Steps:
- Make node 5 the new root; the original root (10) becomes the right child of 5.
- If 5 had a right subtree, it becomes the left child of 10 (none here, so 10’s left child is empty).

Resulting structure:

  5
 / \
3   10
 \
  1

2. RR Type: Right-Right Imbalance (Left Rotation)

Scenario: Node A has a right subtree height 2 greater than its left subtree, and the right subtree itself has a taller right child.
Example:
Insertion order: 1, 3, 5, 7 (inserted sequentially into the right subtree), forming the tree:

1
 \
  3
   \
    5
     \
      7

Node 1 has a balance factor of -2 (right-heavy), requiring a left rotation.

Left Rotation Steps:
- Make node 5 the new root; the original root (1) becomes the left child of 5.
- If 5 had a left subtree, it becomes the right child of 1 (none here, so 1’s right child is empty).

Resulting structure:

    5
   / \
  1   7
   \
    3

3. LR Type: Left-Right Imbalance (First Left Rotate, Then Right Rotate)

Scenario: Node A’s left subtree is 2 units taller than its right subtree, but the left subtree’s right subtree is taller.
Example:
Insertion order: 10, 5, 7, 3 (insert left 5, then right 7, then left 3), forming:

    10
   /
  5
   \
    7
   /
  3

Node 10 has a balance factor of 2 (left-heavy), but its left child (5) has a balance factor of -1 (right-heavy), indicating an LR imbalance.

Adjustment Steps:
1. First Left Rotate on the left child (5): This makes 7 the new root of the left subtree:

      10
     /
    7
   / \
  5   (original right subtree of 5)
 /
3
  1. Then Right Rotate on the original root (10): Make 7 the new root, with 10 as its right child:
     7
    / \
   5   10
  /
 3

After rotation, all nodes have balance factors in {-1, 0, 1}, and the tree is balanced.

4. RL Type: Right-Left Imbalance (First Right Rotate, Then Left Rotate)

Scenario: Node A’s right subtree is 2 units taller than its left subtree, but the right subtree’s left subtree is taller.
Example:
Insertion order: 10, 15, 12, 20 (insert right 15, then left 12, then right 20), forming:

    10
     \
      15
     /
    12
     \
      20

Node 10 has a balance factor of -2 (right-heavy), and its right child (15) has a balance factor of 1 (left-heavy), indicating an RL imbalance.

Adjustment Steps:
1. First Right Rotate on the right child (15): Makes 12 the new root of the right subtree:

      10
       \
        12
         \
          15
           \
            20
  1. Then Left Rotate on the original root (10): Makes 12 the new root, with 10 as its left child:
      12
     / \
    10  15
         \
          20

After rotation, the tree is balanced.

Summary

Balanced binary trees monitor structure using the balance factor. When unbalanced, they use rotation operations (LL/RR/LR/RL) to adjust node positions, keeping the height within log n. The essence of rotation is “adjusting the pivot” to ensure left-right subtree height differences do not exceed 1, thus enabling “efficient search”.

Remember: Balance is about “preventing the tree from tilting”, and rotation is the key action to “straighten the tilted tree”.

(For more intuitive rotation effects, imagine a seesaw: when one side is too heavy, the pivot on the opposite side lifts the heavy side back to balance. Tree rotations work similarly by adjusting subtree positions.)

Xiaoye