How do find, insert and remove perform in a binary search tree with n entries and height h, and how large can h be?
All three take $O(h)$ time, because each walks one path from the root downwards; memory is $O(n)$. The height lies between $O(\log n)$ in the best case and $O(n)$ in the worst case, so the operations range from logarithmic to linear.
| Cost | |
|---|---|
| Memory | $O(n)$: n internal and n + 1 external nodes |
find, insert, remove |
$O(h)$ |
| Height h, best case | $O(\log n)$: every level full |
| Height h, worst case | $O(n)$: a single chain |
The $O(h)$ comes from the shape of every operation: one search down a path, plus a constant amount of work at the end (expanding an external node, a removeExternal, or in case 3 a second walk down to the predecessor, which is still on a path of length ≤ h).
So the binary search tree only keeps the binary-search promise of $O(\log n)$ if it stays bushy. In the worst case it is no better than a linked list.
Go deeper:
Self-balancing binary search tree — the family of trees (AVL, red-black, splay) that keep h in $O(\log n)$.