Suppose you keep a million records and need to find one by its key. If they sit in a sorted array, binary search nails any lookup in about twenty comparisons. Wonderful — until you have to insert a new key in the middle, which means shifting half a million entries.
A plain binary search tree fixes inserts but has its own trap: feed it keys in sorted order and it degenerates into a linked list, turning every lookup back into a slow linear scan.
Balanced search trees are the answer to both problems at once. After every insertion or deletion they quietly reshape themselves so that the tree's height stays proportional to log n — meaning a lookup, an insert, and a delete each touch only a handful of nodes, even with billions of keys. This is the structure that indexes databases and filesystems.
Comments
Loading comments...