Binary search trees
Ordered trees that keep search fast, and the rotations that keep them balanced.
- BST: search, insert, min and max In a binary search tree smaller keys go left and larger keys go right. Each comparison picks a direction, so search and insert follow one…
- BST delete; successor and predecessor Deleting from a BST has three cases: a leaf, one child, or two children replaced by the in-order successor. Find successors by going…
- Validating a BST and the in-order property A tree is a BST only if every node fits the range allowed by all its ancestors. Watch the allowed range shrink down each path.
- Why balance matters Inserting sorted keys turns a BST into a chain with O(n) search. Compare the tree’s height with log n to see why balance matters.
- AVL trees and rotations An AVL tree keeps the heights of every node’s subtrees within one. Single and double rotations restore balance after an insert.
- Red-black trees A red-black tree colours nodes red or black and enforces rules that keep it roughly balanced. Inserts recolor and rotate to fix violations.
- Ordered sets and maps: floor, ceiling, range queries Balanced trees power ordered sets and maps that answer floor, ceiling and range queries. Watch an in-order walk visit only the range.