3.4.7 · D1Trees

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

1,655 words8 min readBack to topic

Before you can read the parent note (topic overview), you need every symbol it throws at you to already feel obvious. This page builds each one from nothing, in an order where each idea leans on the one before it.


1. A node — the single block

Picture one box with the number written inside and two little hooks hanging off the bottom — a left hook and a right hook. That's the atom everything else is built from.

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

Why the topic needs it: everything in an AVL tree — heights, balance, rotations — is just rules about how these boxes hook to each other.


2. Left and right children — the two hooks

In code the parent note writes node.left and node.right. Read those as "follow the left hook" and "follow the right hook". If the hook points to nothing, node.left is None.

Why the topic needs it: rotations are literally about re-hooking — taking the arrow that pointed one way and making it point another way.


3. Binary Search Tree — the ordering rule

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

Look at the figure: from any box, go left → numbers shrink, go right → numbers grow. That's the whole rule of a Binary Search Tree.

Why the topic needs it: AVL is a BST that also stays short. The ordering rule must survive every rotation — that's the promise a rotation must never break.


4. Height — how tall a subtree is

Why for empty? Because we want a leaf to come out as . A leaf's height is "1 more than its tallest child". Its children are both empty. If empty , then leaf . Clean.

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

Why the topic needs it: "stays short" means "small height." We can't talk about balance until we can measure height.


5. The and the vertical bars

Two tiny bits of notation the parent uses without pausing:

Why the topic needs it: the balance rule is written — meaning "off by at most one, doesn't matter which side." Without the bars you'd have to write two rules ( and ); the bars fold both into one.


6. Balance factor — the lean of a node

Now we can build the star of the show.

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

Read the sign like a see-saw tipping:

value picture meaning
almost level OK — allowed
tips left left-heavy, needs fixing
tips right right-heavy, needs fixing

Why the topic needs it: this single number is the alarm bell. The parent's entire algorithm is "walk up, look at , if ring the alarm and rotate."


7. Rotation — re-hooking three boxes

You don't need the full machinery here (the parent covers LL, RR, LR, RL) — just the concept: it's local surgery on 3 nodes, studied on its own in Tree Rotations. The key promise it must never break is the BST ordering from section 3.

Why the topic needs it: when , a rotation is the tool that pushes it back to .


8. — the "stays fast" label

Think of as "how many times can I halve before reaching 1." is 3 halvings, so .

Why the topic needs it: the whole reason AVL exists is to guarantee height stays instead of degenerating to (a straight line of boxes = a linked list). Contrast with a plain BST, and with a Red-Black Tree which trades a looser balance rule for faster inserts.


9. and Fibonacci — why the guarantee holds

The parent proves the height bound using Fibonacci Numbers and the golden ratio.

Why the topic needs it: the sparsest possible AVL tree of height has boxes — the Fibonacci pattern. Because Fibonacci grows like , having boxes forces height . That's the entire proof that AVL is fast. (Not a Heap, which balances shape but not order — different tool for a different job.)


Prerequisite map

Node and hooks

Left and right children

Binary Search Tree ordering

Height with empty = minus 1

max and absolute value

Balance factor BF

Rotation re-hooks 3 nodes

Big-O log n

Fibonacci and golden ratio

AVL tree


Equipment checklist

Self-test — can you answer each before revealing?

What is a node?
One box holding a key plus a left hook and a right hook to child boxes.
The BST ordering rule?
For every box, everything left is smaller, everything right is bigger.
Height of a single leaf?
(zero steps down to a leaf).
Height of an empty spot?
— so a leaf comes out as .
Height formula?
.
What does mean?
Distance from zero — drop the sign. .
Balance factor formula?
.
means?
Left-heavy — the left side is too tall, needs a rotation.
Allowed BF values?
.
What does a rotation preserve?
The BST ordering (smaller-left, bigger-right).
What does mean informally?
Doubling the boxes adds only one more step of work.
Why do Fibonacci numbers appear?
The sparsest height- AVL tree has boxes — the Fibonacci recurrence.