3.4.7Trees

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

2,334 words11 min readdifficulty · medium3 backlinks

WHAT is an AVL tree?


WHY does BF1|BF|\le1 guarantee O(logn)O(\log n) height?


HOW: the four rotations

A rotation is a local re-pointering of 3 nodes that preserves the BST in-order ordering while reducing height. The shape of the imbalance tells you which one.

Let zz = first unbalanced node going up, yy = its taller child, xx = yy's taller child.

Case Shape Fix
LL zz left-heavy, inserted in yy's left one right rotation at zz
RR zz right-heavy, inserted in yy's right one left rotation at zz
LR zz left-heavy, inserted in yy's right left at yy, then right at zz
RL zz right-heavy, inserted in yy's left right at yy, then left at zz
Figure — AVL tree — balance factor, rotations (LL, RR, LR, RL), insert, delete

Insert algorithm


Delete algorithm


Worked examples


Common mistakes


Recall Feynman: explain to a 12-year-old

Imagine a tower of blocks where each block must have its left and right stacks almost the same height — never off by more than one. Every time you add or remove a block, you check from the spot upward: if one side got too tall, you tip the tower a little (a rotation) so it leans back to balanced. Because it never leans much, you can always find any block by going down only a few levels — fast! The "tip" comes in 4 flavors depending on which side got too tall and whether it's a straight lean or a zig-zag lean.


Flashcards

What is the balance factor of a node?
height(left)height(right)height(left) - height(right), allowed values 1,0,+1-1,0,+1.
What height is assigned to an empty subtree?
1-1 (so a leaf has height 00).
Which rotation fixes an LL imbalance?
A single right rotation at the unbalanced node.
LR case fix?
rotateLeft(child) then rotateRight(node).
RL case fix?
rotateRight(child) then rotateLeft(node).
How do you tell LL from LR on insert?
Both are left-heavy (BF=+2BF=+2); compare key with left child's key — smaller=LL, larger=LR.
Max rotations needed after one AVL insert?
One (single or double).
Why can delete need many rotations?
A rotation can shorten the subtree, unbalancing an ancestor, so you rebalance up to the root.
What recurrence gives the minimum nodes of a height-hh AVL tree?
N(h)=1+N(h1)+N(h2)N(h)=1+N(h-1)+N(h-2) — a shifted Fibonacci, N(h)=Fh+31N(h)=F_{h+3}-1.
Worst-case AVL height in terms of nn?
1.44log2n=O(logn)\approx 1.44\log_2 n = O(\log n).
On rotation, update which node's height first?
The lower (child) node, then the parent.
Time complexity of search/insert/delete in AVL?
O(logn)O(\log n) each.

Connections

  • Binary Search Tree — AVL is a BST plus the balance invariant.
  • Tree Rotations — the shared primitive used by AVL and Red-Black Tree.
  • Red-Black Tree — looser balancing (height 2logn\le 2\log n), fewer rotations on delete.
  • Fibonacci Numbers — bounds the minimum node count / height.
  • Big-O Notation — why O(logn)O(\log n) height matters.
  • Heap vs AVL — both balanced, but heaps only order parent-child, not full in-order.

Concept Map

can degenerate to

is a self-balancing

keeps

constraint BF <= 1

BF = +2 left-heavy or -2 right-heavy

fixed by

LL single right

RR single left

LR left then right

RL right then left

derived from

Fibonacci recurrence gives

Binary Search Tree

Linked list O of n

AVL Tree

Height O of log n

Balance Factor = hleft - hright

Unbalanced node

Rotations

Left-Left case

Right-Right case

Left-Right case

Right-Left case

N of h = 1 + N h-1 + N h-2

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, normal BST ka problem ye hai ki agar tum sorted order me values insert karo (1,2,3,4,5), to tree ek seedhi line ban jaata hai — linked list jaisa — aur search O(n)O(n) ho jaata hai. AVL tree isi problem ko solve karta hai: ye har node pe balance factor maintain karta hai, jo hai height(left)height(right)height(left) - height(right), aur ye sirf 1,0,+1-1, 0, +1 ho sakta hai. Agar kisi node ka BF +2+2 ya 2-2 ho gaya, matlab tree imbalance ho gaya, to ek rotation karke usse seedha kar dete hain.

Char cases hote hain: LL (left side ke left me daala) → ek right rotation. RR (right ke right) → ek left rotation. LR (left ke right, zig-zag) → pehle child pe left, phir parent pe right. RL (right ke left) → pehle child pe right, phir parent pe left. Trick yaad rakho: same letters = single rotation, mixed letters = double rotation, aur hamesha heavy side ke opposite rotate karo.

Insert me ek important baat — ek insert ke baad maximum ek hi rotation lagti hai. Lekin delete me kayi levels pe rotation lag sakti hai, kyunki ek subtree chhota hone se uska ancestor imbalance ho sakta hai, isliye root tak rebalance karte raho. Rotation karte waqt hamesha neeche wale node ka height pehle update karo, phir upar wale ka.

Kyun important hai? Kyunki AVL ki height kabhi 1.44log2n\approx 1.44\log_2 n se zyada nahi hoti (Fibonacci se prove hota hai), isliye search, insert, delete sab guaranteed O(logn)O(\log n). Interviews aur database indexing me ye concept bahut kaam aata hai.

Go deeper — visual, from zero

Test yourself — Trees

Connections