When does a binary search tree degenerate into a list, and what does that mean for its performance?
When keys are inserted in sorted (or reverse-sorted) order: each new key becomes the right (or left) child of the previous one, the tree turns into a chain of height n − 1, and find, insert and remove degrade to $O(n)$.
* Same keys, different insertion order: height 2 against height 6. *
Inserting 1, 2, 3, 4, 5, 6, 7 into an empty tree: 1 is the root, 2 is larger so it goes right, 3 is larger than both so it goes right of 2, and so on. The result is a "tree" in which every node has only a right child. A search for 7 has to pass all seven nodes, exactly like scanning a linked list.
The same seven keys inserted as 4, 2, 6, 1, 3, 5, 7 give a perfectly balanced tree of height 2, where any key is found in at most three comparisons.
| Insertion order | Shape | Height | Search cost |
|---|---|---|---|
| 4, 2, 6, 1, 3, 5, 7 | full, balanced | 2 ($\approx \log_2 n$) | $O(\log n)$ |
| 1, 2, 3, 4, 5, 6, 7 | chain | 6 (n − 1) | $O(n)$ |
Sorted input is not a rare corner case: data often arrives already sorted (by ID, by timestamp). This weakness is why balanced search trees, such as AVL trees, restructure themselves on insert and remove to guarantee height $O(\log n)$ regardless of the input order. Asking callers to shuffle their data first is not an option: whoever uses a map must never have to think about insertion order. AVL and splay trees are built as extensions of the plain binary search tree: they reuse its search and insert, and add small local corrections (rotations) at the end.
The situation mirrors hashing. A hash table's performance depends on the keys it gets, and many collisions are its "bad weather"; a search tree's shape depends on the insertion order, and a degenerate chain is its bad weather. Self-balancing plays the role for the tree that rehashing plays for the hash table.
Go deeper:
Binary search tree visualization (USFCA) — insert keys in sorted order and watch the chain form, then try a shuffled order.
VisuAlgo: Binary Search Tree, AVL Tree — search, insert and remove animated, side by side with the self-balancing AVL variant.