3.4.7 · D2Trees

Visual walkthrough — AVL tree — balance factor, rotations (LL, RR, LR, RL), insert, delete

1,992 words9 min readBack to topic

We assume only that you know what a Binary Search Tree is: a tree where every left descendant is smaller and every right descendant is larger than a node. Everything else we build here.


Step 1 — What "height" even means

WHAT. We must pin down a single number that measures "how tall is this tree?" before we can bound it.

WHY. The entire promise of an AVL tree is fast search, and search cost = how many nodes you walk from the top to the bottom = the height. If we can't measure height we can't bound the search cost. (See Big-O Notation for why "cost" and "height" are the same story here.)

PICTURE. Look at the figure. The height of a node is the number of edges on the longest downward path from that node to a leaf.

Figure — AVL tree — balance factor, rotations (LL, RR, LR, RL), insert, delete

That convention is not a random choice — it is the only value that makes the formula agree with "a leaf has height 0". The parent note's first common mistake is precisely forgetting this.


Step 2 — The balance rule, drawn

WHAT. State the one constraint every AVL node obeys.

WHY. We need a precise rule to feed into a counting argument. "Almost balanced" is a feeling; "" is a number we can do algebra with.

PICTURE. For any node, subtract the two children's heights. That difference is the balance factor.

Figure — AVL tree — balance factor, rotations (LL, RR, LR, RL), insert, delete

So the two children of any node may differ in height by at most one. Keep that single sentence — the whole derivation is squeezing juice out of it.


Step 3 — Flip the question: sparsest tree, not tallest

WHAT. Instead of asking "how tall can a tree of nodes get?", ask the mirror question: "what is the fewest nodes a tree of height can possibly have?"

WHY. These two questions are two sides of the same coin, but the second one is constructive — we can literally build the sparsest tree and count it. Call that minimum count .

  • If a height- AVL tree is forced to contain many nodes even at its sparsest, then a tree with only nodes cannot afford to be tall. That is the whole logical trick.

PICTURE. The sparsest height- tree: a root, one subtree as tall as possible (), and the other subtree made as short as the AVL law still permits.

Figure — AVL tree — balance factor, rotations (LL, RR, LR, RL), insert, delete

Why can the shorter subtree be and not shorter? Because the root's balance factor is , and the AVL law caps this at . So . Making it exactly uses the fewest nodes while staying legal.


Step 4 — Write the recurrence for the sparsest tree

WHAT. Turn the picture from Step 3 into an equation.

WHY. A picture of "root + two sub-trees" is an addition waiting to happen. Counting nodes across the whole tree = 1 (the root) + nodes in the left sub-tree + nodes in the right sub-tree.

PICTURE. Each of the two triangles below is itself a sparsest tree of its own height — so it contributes and nodes.

Figure — AVL tree — balance factor, rotations (LL, RR, LR, RL), insert, delete

Let us actually crank a few values so the pattern is undeniable:


Step 5 — Spot the hidden Fibonacci

WHAT. Show that is the Fibonacci sequence wearing a disguise.

WHY. Fibonacci numbers are the sequence whose recurrence is "add the previous two" — and they have a famous closed form involving the golden ratio. If we can pin to Fibonacci, we inherit that closed form for free and finish the height bound.

PICTURE. Add to every and the ugly "" in the recurrence vanishes, leaving pure Fibonacci.

Figure — AVL tree — balance factor, rotations (LL, RR, LR, RL), insert, delete

Watch the algebra. Define . Substitute into the recurrence:

  • Left side: .
  • Right side: the stray from the three "+1"s? No — we added to each term, and : the constant on the right plus the two s equals the single we need on the left. The bookkeeping works out to exactly . That is the Fibonacci recurrence.

Checking against our table, gives for — the Fibonacci numbers


Step 6 — Golden ratio ⇒ logarithmic height

WHAT. Convert " grows like Fibonacci" into " grows like ".

WHY. Fibonacci numbers grow exponentially — each is about times the last. Exponential growth in means, read backwards, logarithmic growth in . That backwards read is the punchline.

PICTURE. shoots up like ; invert that curve and height flattens into a logarithm.

Figure — AVL tree — balance factor, rotations (LL, RR, LR, RL), insert, delete

The closed form of Fibonacci is with . So:

  • — grows by a factor of for each extra unit of height.
  • dividing by — a constant fudge factor; irrelevant to growth rate.

Now the reversal. A real tree with nodes and height has (it can't be sparser than the sparsest). So:

Convert the base with and :

  • — height of a perfectly balanced tree.
  • — the price of AVL's slight looseness: an AVL tree is at worst taller than perfect. Still .

That is the theorem the parent note stated. Search, insert, and delete each walk one root-to-leaf path, so all three cost . Compare with a naive Binary Search Tree which can hit .


Step 7 — The degenerate cases (do the edges break it?)

WHAT. Check the two smallest trees and the "empty" case, where any argument can silently fail.

WHY. Contract rule: cover every case. A bound that is true for tall trees but false for is a broken bound.

PICTURE. Three tiny trees drawn explicitly against the formula.

Figure — AVL tree — balance factor, rotations (LL, RR, LR, RL), insert, delete
  • Empty tree (): . Correct — zero nodes. The recurrence's base case, not derived, is chosen to make this consistent.
  • Single leaf (): , and . ✅ The formula survives its smallest non-trivial input.
  • Height 1: — a root with exactly one child. Note this child must be a leaf, and the root's , which is legal. If we demanded everywhere, height-1 with 2 nodes would be illegal and the whole Fibonacci count would collapse — this is why AVL allows , not just .

The bound therefore holds from the tiniest tree upward, with no gap.


The one-picture summary

Figure — AVL tree — balance factor, rotations (LL, RR, LR, RL), insert, delete

Everything on this page in one arc: the AVL law () forces the sparsest tree into a recursive "root + tall + short" shape, that shape's node-count is Fibonacci, Fibonacci grows like , and inverting exponential growth gives logarithmic height. Unlike a Red-Black Tree (whose bound is via a different colouring argument), AVL's tighter falls straight out of Fibonacci.

Recall Feynman retelling — say it to a friend

"Suppose I want an AVL tree to be as tall as possible using as few blocks as I can. I make one side tall, and — since the two sides can only differ by one floor — the other side just one floor shorter. Count the blocks: it's the root plus the blocks in each side, so . That 'add the previous two' rule? That's Fibonacci! And Fibonacci numbers blow up by about every step. So even the skinniest legal tree still packs in exponentially many nodes as it gets taller. Turn that around: if I only have nodes, the tree simply cannot get taller than about . That's why searching an AVL tree is always fast."

Recall Quick self-test

Why is empty-tree height defined as ? ::: So a leaf comes out to height from ; it keeps every balance-factor formula clean. What recurrence counts the fewest nodes in a height- AVL tree? ::: , with . Which famous sequence does equal? ::: , the shifted Fibonacci numbers. Where does the number come from? ::: From , the base-change of into .


Prerequisites & neighbours: Binary Search Tree · Tree Rotations · Fibonacci Numbers · Big-O Notation · Red-Black Tree · Heap