Insertion into a search tree is deterministic given the order. Each new key walks from the root to the empty position where a search for it would have failed, and there is exactly one such position. So the tree is a function of the sequence, and asking “how tall is the tree for these keys” is not a well-posed question.
Example · Sorted input is the worst case, and it is not a rare oneInsert 10, 20, 30, …, 150. Every key is larger than everything present, so every key becomes the right child of the previous one: a path of height 14 on fifteen keys. Reversed input does the same thing leftwards. Sorted arrival is what you get from a database dump ordered by primary key, a log replayed by timestamp, or an identifier that increments — so the worst case for this structure is also one of the most common inputs in practice.
n = 15, keys 10 20 30 … 150, three insertion orders
order height mean depth comparisons to build
sorted 10 20 … 150 14 105/15 = 7 105
reversed 150 140 … 10 14 105/15 = 7 105
shuffled 130 80 100 110 … 6 44/15 ≈ 2.93 44
bisect 80 40 20 10 30 … 3 34/15 ≈ 2.27 34
2 ln 15 = 5.416 asymptotic mean depth of a RANDOM tree
Read the last line carefully, because it is the one number on this page that was not counted. 2 ln n is the asymptotic expected depth of a node in a tree built from a uniformly random order. At n = 15 it evaluates to 5.416, and the lab draws it as a dashed reference curve and labels its readout asymptotic. It is not the height of any of the trees above — the shuffled tree’s height is 6 and the balanced tree’s is 3 — and it is not the measured mean depth either, which is 2.93 on the shuffle.
So there are three numbers on screen and they are three different quantities: the measured height, the longest path in the tree drawn; the measured mean depth, an exact fraction over the nodes of that tree; and the asymptote, a formula about the limit of an average over orders. At n = 15 the asymptote is about 85% above the measured mean depth, which is what an asymptotic formula does at small n. Push the size up and the gap narrows; it does not close from above or below in any tidy way, because one shuffle is one sample.
Theorem · Expected height of a randomly built search treeInsert n distinct keys in an order chosen uniformly at random from all n! orders. The expected height of the resulting tree is O(log n), and the expected depth of a node is 2 ln n + O(1).
This is stated here and proved elsewhere. The recurrence it comes from is the one produced by choosing a uniformly random root and recursing on the two pieces — which is exactly the recurrence “Randomised Quicksort” solves on the Sorting and Selection course, because a random pivot and a random root are the same object looked at twice. “Treaps”, on the Randomised Algorithms course, closes the loop by building a tree whose shape is random no matter what order the keys arrive in.
What a shuffle buys, and what it does not
The shuffled order above gives height 6 where sorted gives 14: a factor of more than two, and 44 comparisons to build rather than 105. That is a real and large improvement, and it is one sample. Run a different seed and the height moves. The theorem is about the average over all orders, and no single shuffle establishes it — which is the same caution as everywhere else on this course, sharpened by the fact that here the quantity in question is itself an average.
And a shuffle is not always available. It requires having all the keys before inserting any of them, which defeats the point of an online structure. If keys arrive one at a time from a sorted stream there is nothing to shuffle, and the tree degenerates as it grows. That is the gap the rest of this course closes: instead of hoping the order is random, enforce a shape condition on every insertion, which is what “Rotations and the AVL Invariant” begins.
One last measurement worth having. The bisect order — middle key first, then the middle of each half, recursively — gives height 3 at n = 15, which is ⌊log₂ 15⌋: the shortest a tree on fifteen nodes can be. It costs 34 comparisons to build. It is also useless as a strategy, for the same reason a shuffle often is: you have to know all the keys and their order in advance, which is to say you have to have sorted them already.