Why This Matters
Hash tables give lookup but lose order. BSTs keep order: they can find minimum, maximum, predecessor, successor, and all keys in a range.
Core Idea
For every node with key , all keys in the left subtree are below , and all keys in the right subtree are above .
| Operation | Balanced BST | Unbalanced BST |
|---|---|---|
| search | ||
| insert | ||
| delete | ||
| range query |
Worked example: inorder traversal of a BST lists keys in sorted order.
Common Mistakes
- Saying BST operations are always . That requires a height guarantee.
- Forgetting duplicate-key policy. The tree must define where equal keys go.
- Using a BST where a hash table is simpler and order is not needed.
Exercises
- Insert
5, 2, 8, 1, 3into a BST and draw it. - Explain why sorted insertion creates a chain.
- State one reason a tree map can beat a hash map.
Next Topics
References
- Cormen, Leiserson, Rivest, Stein. Introduction to Algorithms (4th ed., 2022). Ch. 12, 13.
- Sedgewick and Wayne. Algorithms (4th ed., 2011). Ch. 3.