Maintain balance in AVL and red-black trees

8 steps · about 2 h 55 min · Intermediate

  1. See why unbalanced BSTs degrade to O(n) (guide, Runestone Academy, 20 min)

    Derives balance factors and shows how sorted insertions ruin plain binary search tree performance.

  2. Master left and right rotations (reference, Wikipedia, 15 min)

    Proves that local pointer rotations change subtree heights while preserving inorder key order.

  3. Learn the AVL height-balance invariant (reference, Wikipedia, 20 min)

    Categorizes the four imbalance cases (LL, RR, LR, RL) and the exact rotations that restore balance.

  4. Step through AVL rotations interactively (interactive, University of San Francisco, 20 min)

    Animates balance factors and double rotations as keys are inserted and deleted.

  5. Implement balance-factor updates in Python (tutorial, Runestone Academy, 25 min)

    Shows how to propagate height changes upward and stop once a subtree's height stabilizes.

  6. Contrast AVL strictness with red-black invariants (reference, Wikipedia, 25 min)

    Explains the five colour rules that bound longest-to-shortest path ratios with fewer rotations.

  7. Watch recolouring and rotations in a red-black tree (interactive, University of San Francisco, 20 min)

    Makes uncle-colour cases visible so recolouring versus rotation decisions become intuitive.

  8. Unify red-black trees via 2-3 tree equivalence (guide, Princeton University, 30 min)

    Presents left-leaning red-black trees as a compact, clean implementation of balanced 2-3 search trees.