3.4.7 · D3Trees

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

4,134 words19 min readBack to topic

The parent note showed you the four rotations and three examples. This page is the drill hall: we enumerate every kind of situation an AVL tree can hand you, then solve one example for each cell so no scenario is a surprise on an exam.

Before we start, let's re-anchor the symbols we'll use on every line, so nothing appears "for free":

If any of that feels shaky, the parent topic note builds it from scratch; a plain Binary Search Tree is the object we are keeping balanced, and Tree Rotations is the pure mechanics of the re-pointering.


The scenario matrix

Every AVL rebalance is described by two questions: which way is tilted (sign of ) and is the path to the deep node straight or bent. Multiply that by how the imbalance was caused (insert vs delete) plus the odd degenerate cases, and you get the full space:

# Cell class What makes it distinct Example
A LL on insert , straight left path Ex 1
B RR on insert , straight right path Ex 2
C LR on insert , bent (left-then-right) Ex 3
D RL on insert , bent (right-then-left) Ex 4
E Delete, one rotation delete triggers exactly one fix Ex 5
F Delete, cascading one fix shortens a subtree and unbalances an ancestor → 2 rotations at different nodes Ex 6
G Delete, double rotation but opposite sign → LR/RL on delete Ex 7
G′ Delete, and taller child is level → still one single rotation Ex 8
H Degenerate / no rotation insert keeps $ BF
I Word problem (real-world) leaderboard framing Ex 10
J Exam twist — build worst tree minimum-node (Fibonacci) tree Ex 11

Cells A–D are the four rotation shapes on insert; E–G′ are the four deletion subtleties (single, cascading, double, and the tie); H is the "do nothing" boundary; I–J are applied and analytical. We now hit each cell exactly once.


Cell A — LL on insert


Cell B — RR on insert


Cell C — LR on insert


Cell D — RL on insert


Cell E — delete with a single rotation


Cell F — delete that cascades (two rotations at different nodes)


Cell G — delete that triggers a DOUBLE rotation at one node


Cell G′ — delete where the taller child is level,


Cell H — degenerate: insert with no rotation


Cell I — real-world word problem


Cell J — exam twist: build the sparsest (worst) AVL tree


Recall Self-test: name the cell before you rotate

Insert into left-of-left ::: LL, single right rotation Insert into right-of-right ::: RR, single left rotation Insert into right-of-left (left-heavy ) ::: LR, rotateLeft(child) then rotateRight(z) Insert into left-of-right (right-heavy ) ::: RL, rotateRight(child) then rotateLeft(z) On delete, how do you pick between LL and LR when ? ::: by : ⇒ LL, ⇒ LR On delete with , what does mean? ::: taller child is level ⇒ still a single rotation (RR), since On delete, what makes need a double rotation? ::: the taller child leans the opposite way to Is ever "capped" at ? ::: No — it is a plain subtraction; we simply rebalance the lowest node before any larger value can build up Max rotations for one insert vs one delete ::: insert at most 1; delete up to , at multiple nodes How many nodes in the thinnest height-3 AVL tree? :::