CSE3144 Β· Advanced Data Structures Β· Jul–Nov Semester 2026 Β· Lecture 8 of 36 Β· CO CSE3144.2 Β· Unit II

AVL Trees: Rotations, Insertion & Deletion

Last time we ended on a promise: when a balance factor hits Β±2, the tree performs an O(1) "rotation" that restores balance without breaking the BST property. Today: the repair kit β€” rotations, and full AVL insertion/deletion traces, all animated.

Dr. Manu ShrivastavaCourse Instructor Β· Consultation Fri 2–5 PM, LHC 308F
40 minutesSession outcome: apply AVL rotations to keep BST operations O(log n)
L8 Β· 00 β€” Agenda 40 min total

Today, minute by minute

Rotations: the one legal move

00–08

Single rotations (LL, RR), animated β€” and why they preserve the BST property.

Double rotations (LR, RL)

08–16

Watch a single rotation FAIL on a zig-zag, then fix it in two moves.

AVL insertion, end to end

16–27

Six keys, balance factors live, two violations repaired on the way.

AVL deletion

27–35

Delete, walk up, rebalance β€” and why deletion can cascade.

Recap + homework

35–40

The four-case cheat sheet; the median always wins.

Quick recap β€” Lecture 7

A BST is binary search stored as pointers: O(h) search, insert, delete β€” but h is a hostage of input order, and sorted input drives h to nβˆ’1. An AVL tree adds one rule β€” every node's balance factor bf(x) = height(left) βˆ’ height(right) stays in {βˆ’1, 0, +1} β€” which caps h at 1.44 log n by construction. The enforcement mechanism is the rotation, starting now.

CSE3144 β€” Lecture 8
L8 Β· 01 β€” Single rotations ~8 min

The one legal move: rotate the deep side up

The intuition

A violation means one side hangs too deep. The repair: lift the middle key to the top and let the other two spread beneath it. Three keys 10 < 20 < 30 can form five BST shapes β€” only one has height 1: the median on top. Every rotation is just a pointer-level way of saying "put the median on top."

Why is it legal? Because a rotation never changes the inorder sequence β€” it re-parents nodes but every key keeps the same left/right relationships to every other key. Sorted order in = sorted order out. Heights change; the dictionary doesn't.

Naming the cases

Let z = the lowest node with bf ±2, y = z's taller child, x = y's taller child. The shape of the path z→y→x names the case: LL (left-left: straight line leaning left) · RR (mirror) · LR and RL (zig-zags — next slide). Straight lines need ONE rotation; zig-zags need two.

Interactive β€” LL and RR, numeric then general
CSE3144 β€” Lecture 8 Β· Single rotations
L8 Β· 02 β€” Double rotations ~8 min

Zig-zags: where one rotation fails β€” watch it fail

Interactive β€” LR: the failed shortcut, then the real fix; RL mirrored
CaseShape of z β†’ y β†’ xDetect byRepair
LLleft, then left (straight)bf(z) = +2, bf(y) β‰₯ 0right-rotate z
RRright, then right (straight)bf(z) = βˆ’2, bf(y) ≀ 0left-rotate z
LRleft, then right (zig-zag)bf(z) = +2, bf(y) = βˆ’1left-rotate y, then right-rotate z
RLright, then left (zig-zag)bf(z) = βˆ’2, bf(y) = +1right-rotate y, then left-rotate z

The one-line memory trick: in every case, of the three nodes x, y, z, the median key ends up on top with the other two as its children. If you forget the case table in an exam, just ask: "which of these three keys is the middle one?" β€” that's your new subtree root.

CSE3144 β€” Lecture 8 Β· Double rotations
L8 Β· 03 β€” AVL insertion ~11 min

AVL insertion: BST insert + walk back up + repair once

The algorithm
1. insert exactly as in Lecture 7 (walk, link)
2. walk BACK UP the insertion path:
      recompute bf at each ancestor
3. at the FIRST node with bf = Β±2:
      classify (LL/RR/LR/RL), rotate
      stop β€” the subtree height is restored,
      ancestors above are automatically fine

At most one repair per insertion (single or double) β€” because the rotation returns the subtree to its pre-insertion height, so no ancestor ever notices anything happened. Total: O(log n) walk + O(1) surgery.

The demo sequence

We insert 10, 20, 30, 40, 50, 25 β€” mostly ascending, exactly the poison that killed the plain BST in Lecture 7. Watch the AVL tree take the same input and stay height 2. Node labels show key (bf); a bf of Β±2 glows amber; teal = restructured.

Interactive β€” six insertions, two violations, two repairs
CSE3144 β€” Lecture 8 Β· Insertion
L8 Β· 04 β€” AVL deletion ~8 min

AVL deletion: the same idea, with one twist

The algorithm
1. delete exactly as in Lecture 7
   (leaf / one-child / successor trick)
2. walk back UP from the deleted spot,
   recomputing bf at each ancestor
3. rotate wherever bf hits Β±2
   // but do NOT stop after the first fix…

The twist: a deletion-repair can shrink the subtree it fixes β€” which may unbalance the ancestor above, which needs its own rotation, and so on. Deletion can cascade up to O(log n) rotations (insertion never needs more than one). Still O(log n) total.

The demo

We start from the tree the insertion animation just built and delete 50, then 40 β€” the second deletion strips the right side bare and forces a rotation with a subtle feature: the violating node's child has bf = 0, a shape that only deletion can produce (insertion never creates it β€” good quiz question).

Interactive β€” two deletions, one rotation
CSE3144 β€” Lecture 8 Β· Deletion
L8 Β· 05 β€” Recap & what's next ~5 min

Lecture 8 takeaways

Lecture 9

Red-Black Trees

AVL's rival: slightly looser balance (h ≀ 2 log n), but cheaper maintenance β€” the tree behind C++ std::map and Java TreeMap. Same rotations, new bookkeeping: colors.

Homework β€” bring to Lecture 9
  • Insert 1, 2, 3, 4, 5, 6, 7 into an AVL tree, in order. Show every balance factor and every rotation. (Sorted input β€” the plain BST's nightmare; count how many rotations AVL pays to stay height ≀ 2.9 log n.)
  • Insert 50, 25, 75, 10, 30, 5 (an LL case), then separately 50, 25, 75, 10, 30, 28 (an LR case). Draw before/after for each rotation.
  • From your Lecture 7 homework tree: compute all bf; if it isn't AVL, find the minimum set of rotations to fix it.
  • Delete the root from the final tree of today's insertion demo; show the successor trick + any rebalancing.
  • Argue in 3–4 sentences: why does an insertion repair never propagate above the first fixed node, while a deletion repair can?
  • Reading: Weiss Β§4.4 (AVL trees); CLRS ch. 13 intro (for next time).
CSE3144 β€” Lecture 8

Questions?

Dr. Manu Shrivastava β€” LHC 308F β€” Friday 2:00–5:00 PM

Next: Lecture 9 β€” Red-Black Trees and Operations.