Chapter 12 · Balanced Trees

AVL Trees

A BST can degenerate into a linked list. An AVL tree prevents this by detecting imbalance after every modification and fixing it with a rotation — a constant-time structural rearrangement. Result: O(log n) guaranteed, always.

What is an AVL tree?

An AVL tree is a self-balancing binary search tree. For every node, the heights of its left and right subtrees differ by at most 1. After each insert or delete, it fixes any imbalance with rotations, so search, insert and delete are guaranteed O(log n). It's stricter than a red-black tree, so lookups are slightly faster but updates do more rotations.

// balance factor = height(left) - height(right); must be -1, 0 or 1
function rotateRight(y) {
  const x = y.left;
  y.left = x.right;
  x.right = y;
  return x;            // x is the new subtree root
}

The balance factor

Every node stores its balance factor = height(left subtree) − height(right subtree). An AVL tree maintains the invariant that every node's balance factor is in {-1, 0, +1}.

2010305152535

Perfectly balanced AVL tree — all balance factors are 0

20 (root)

bf = 0

10, 30

bf = 0

5, 15, 25, 35

bf = 0 (leaves)

All balance factors are 0 — a perfectly balanced AVL tree. Height = 2 for 7 nodes.

The 4 rotation cases

1 / 5
1

Detect imbalance

After every insert or delete, recalculate the balance factor at each node: balance = height(left) - height(right). If |balance| > 1, a rotation is needed.

1 of 5

Live demo — BST shape explorer

Observe how different insertion orders produce different tree shapes. In an AVL tree, rotations would rebalance automatically to keep height at O(log n).

Binary Search Tree
preset:
normalpathfounddelete

Tree is empty — insert a value or click a preset above

→ Insert values to build your BST

Rotation pseudocode

avl-rotations.pseudo
1rotateRight(y): // y is unbalanced, x is left child
2 x <- y.left
3 T2 <- x.right // save x's right subtree
4 x.right <- y // x's right is now y
5 y.left <- T2 // y's left is T2
6 updateHeight(y) // update in bottom-up order
7 updateHeight(x)
8 return x // x is the new root
9 
10rotateLeft(x): // mirror of rotateRight
11 y <- x.right
12 T2 <- y.left
13 y.left <- x
14 x.right <- T2
15 updateHeight(x)
16 updateHeight(y)
17 return y
18 
19balance(node): // called after every insert/delete
20 bf <- balanceFactor(node) // height(left) - height(right)
21 if bf > 1: // left-heavy
22 if balanceFactor(node.left) < 0:
23 node.left <- rotateLeft(node.left) // LR case
24 return rotateRight(node) // LL case
25 if bf < -1: // right-heavy
26 if balanceFactor(node.right) > 0:
27 node.right <- rotateRight(node.right) // RL case
28 return rotateLeft(node) // RR case
29 return node // already balanced

AVL vs BST vs Red-Black

Plain BST

+ Simplest to implement

− O(n) worst case with sorted input

AVL Tree

+ Strictest balance — height never exceeds 1.44 log n. Fastest lookups.

− More rotations on insert/delete than Red-Black

Red-Black Tree

+ Fewer rotations — better insert/delete throughput. Used in most std libraries.

− Slightly taller than AVL (height up to 2 log n)

Time complexity

search — guaranteed, height never exceeds 1.44 log2(n)O(log n)

Logarithmic time

insert — plus O(1) rotation at the imbalanced ancestorO(log n)

Logarithmic time

delete — may trigger O(log n) rotations up the pathO(log n)

Logarithmic time

single rotation (right or left) — constant time pointer updatesO(1)

Constant time

🎯 Quick Check

You insert a node and find a node with balance factor +2 and its left child has balance factor +1. Which rotation fixes this?

🧠AVL height bound
An AVL tree with n nodes has height at most 1.44 log₂(n). For 1 million nodes: max height = 1.44 × 20 = 28. For 1 billion nodes: max height = 1.44 × 30 = 43. O(log n) is a hard guarantee, not an average.

AVL Trees: summary

Key points

  • Four imbalance cases: LL, RR (single rotation), and LR, RL (double rotation).
  • Each node stores its height (or balance factor) to detect imbalance in O(1).
  • Height is always under about 1.44 log₂ n.

Frequently asked questions

›AVL tree vs red-black tree?

AVL trees are more strictly balanced, which makes lookups faster. Red-black trees rotate less, which makes inserts and deletes cheaper; that's why most standard libraries use red-black trees.