How a binary search tree stays searchable
A binary search tree finds a key among a million in about twenty comparisons, or takes a million. Here is the rule that makes it fast, and the insert order that quietly breaks it.
01 The rule: left smaller, right bigger
Each node holds a key and up to two children. The rule is about whole subtrees, not just children: every key anywhere below a node's left side is smaller than it, and every key below its right side is bigger.
That one rule means that at any node you know which half the key you want must be in. You never have to look at the other half.
- in the wrong subtree
- the bound it breaks
9 is bigger than its parent 3, so as 3's right child it looks right. But it sits in 8's left subtree, where every key must be smaller than 8. A search for 9 would turn right at 8 and never find it.
02 Insert: search, then fill the empty slot
To insert a key, search for it. The search ends at an empty slot, because the key is not there yet, and that slot is exactly where the key belongs. Existing nodes never move.
- compared on the way
- just inserted
The tree starts empty. The keys will arrive in this order: 8, 3, 10, 1, 6, 14, 4, 7, 13.
The output shows the bonus: an in-order walk (left, node, right) of a binary search tree gives the keys sorted. That follows directly from the rule.
03 Search: one comparison per level
Search follows the same path insert would. Each comparison discards one whole subtree.
- not looked at yet
- comparing now
- compared
- ruled out
- the key
The search for 7 starts at the root. No key has been compared yet.
A miss costs the same. Searching for 5 goes 8, 3, 6, 4, then tries 4's right child, finds None, and stops. That None is where insert(root, 5) would put it.
- comparing now
- compared
- just inserted
Start from the nine-key tree the first trace built. Insert 5 into it: nothing is rebuilt, the new key only needs a free slot.
04 The code
Search is a loop that walks down. Insert is the same walk, written recursively so each parent can attach the new child on the way back.
05 When the tree goes skewed
The work is one comparison per level, so the cost is the tree's height. The shape depends only on the insert order. Insert keys that are already sorted, and each new key is bigger than everything so far: it always goes right, and the tree becomes a linked list.
- deepest key
Every node has only a right child. Searching for 5 compares against all five keys, exactly like scanning a list.
This is why library ordered maps use self-balancing trees, which rotate nodes after inserts and deletes to keep the height near log₂ n. Java's TreeMap is a red-black tree, and C++ std::map is typically one too. Python has no built-in tree map; dict is a hash table.
06 When it bites
node.left.key < node.keyChecking only parent and childThe broken tree above passes this check at every node. Pass the allowed range down instead: the left side gets an upper bound, the right side a lower bound.for k in range(1, 2001): root = insert(root, k)Sorted input and recursionThe chain gets one level deeper per key. In CPython the recursive insert raises RecursionError once the depth passes the default limit of about 1,000.insert(root, 6) # againDuplicate keysDecide once: ignore them (as above), keep a count in the node, or always send equal keys the same way. Mixing rules breaks search.delete(root, 3)Deleting a node with two childrenYou cannot just remove it. Copy in its in-order successor, the smallest key in its right subtree, then delete that node, which has at most one child.