On this page
Every search, insert and delete in a binary search tree walks one path from the root downwards, so the cost is set by the tree's height. The same seven numbers can give a tree seven levels deep or three, depending only on the order you insert them. This post shows both, then shows how a self-balancing tree repairs the bad case with a few rotations.
What is a binary search tree?
A binary search tree (BST) is a binary tree in which each node's key is larger than every key in its left subtree and smaller than every key in its right subtree, as defined in Sedgewick and Wayne's Algorithms, section 3.2. The same page notes that search, insert and delete take time proportional to the height of the tree in the worst case.
How do search and insertion work?
To search for a key k, start at the root and compare:
If k equals the node's key, you have found it.
If k is smaller, go to the left child; if larger, go right.
If the child you need is empty, k is not in the tree.
Insertion is the same walk: where you fall off the tree, you hang the new node. Nothing above it moves, which is why a plain BST can end up badly shaped.
Why does sorted data make a binary search tree slow?
Insert 1, 2, 3, 4, 5, 6, 7 in that order. Each key is bigger than everything already in the tree, so it always goes right. You get a chain: 1 at the root, 2 below it, and so on, seven levels deep. Searching for 7 now takes seven comparisons, exactly as many as scanning a list.
Now insert the same keys in the order 4, 2, 6, 1, 3, 5, 7. The root 4 splits the rest into {1, 2, 3} and {5, 6, 7}, and each half splits again. The tree is perfect: three levels, and no search needs more than three comparisons.
With n keys in sorted order, the chain has height n. Real data often arrives sorted: roll numbers, timestamps, names in alphabetical order.
How tall is a binary search tree built from random keys?
If the n keys arrive in a uniformly random order, the tree is usually fine. Luc Devroye proved in the Journal of the ACM in 1986 that the height divided by ln n tends, in probability, to c = 4.31107..., where c is the solution of c ln(2e/c) = 1 with c at least 2.
Bruce Reed sharpened this in a 2003 Journal of the ACM paper: the expected height is 4.311 ln n minus 1.953 ln ln n plus a constant, and its variance stays bounded as n grows.
A typical search is shorter than the height. Sedgewick and Wayne's BST section gives about 2 ln n compares, roughly 1.39 log2 n, for a search in a BST built from n random keys.
So random order gives logarithmic height, but you cannot choose the order your data arrives in.
How do AVL and red-black trees keep height O(log n)?
Both add a rule about shape and restore it after every insert or delete with rotations. A rotation turns a parent and child around each other in constant time while keeping the ordering property.
AVL tree (Adelson-Velsky and Landis, 1962): at every node, the two subtree heights differ by at most one. The AVL tree article, citing Knuth, gives the height as less than log base φ of (n + 2), where φ ≈ 1.618 is the golden ratio, which works out to about 1.44 log2 n.
Red-black tree (Guibas and Sedgewick, 1978): nodes are coloured red or black, no red node has a red child, and every root-to-leaf path has the same number of black nodes. Sedgewick and Wayne's balanced trees section shows that their version has height no more than 2 log2 n.
How does an AVL tree rebalance the sorted keys 1 to 7?
Insert the sorted keys again, rotating whenever a node's subtree heights differ by two.
Insert 1, 2, 3. Node 1 has an empty left side and two levels on the right. Rotate left at 1: now 2 is the root with children 1 and 3.
Insert 4, then 5. Node 3 is now out of balance. Rotate left at 3: 4 takes its place with children 3 and 5.
Insert 6. The root 2 has one level on the left and three on the right. Rotate left at 2: 4 becomes the root, with 2 (children 1, 3) on the left and 5 (child 6) on the right.
Insert 7. Node 5 is out of balance. Rotate left at 5: 6 takes its place with children 5 and 7.
Four rotations later you have the perfect three-level tree, from the input that built a seven-level chain before.
If rotations still feel abstract, the Fermi Concept Library covers every concept in mathematics and computing, and the Fermi Problem Library has a step-by-step solver that explains the reasoning.
For related structures, see how a binary heap keeps a priority queue in order with sift-up and sift-down, why comparison sorting needs about n log n comparisons, and how hash tables trade order for speed through their load factor.



