← chapter

Binary search trees

One rule: smaller left, larger right. Every operation is a walk down one path.

The invariant does everything

Watch a BST take shape

Insert 5,3,8,1,4,7,9 — each value walks down (blue) to its spot (green).

The fatal flaw: balance

Sorted input → degenerate linked-list tree → O(n) per op. 291× slower at n=8000.

vs hash table

Takeaway

An ordered container in O(log n) — if it stays balanced. Unbalanced, sorted input kills it. Next: AVL trees keep it balanced.