6.5Binary Search Tree with Linked Nodes
Unbalanced trees: the worst case
Every operation on a binary search tree takes as long as the tree's height. If the tree is balanced that is very short, but the shape of the tree is decided by the order of insertion, not by the values. This lesson shows how a bad order turns the tree into a linked list, and what the fix is.
- That the same values build very different trees depending on insertion order
- How far apart search times are in a balanced and an unbalanced tree
- How self-balancing trees solve the problem, as a short preview
1Same values, two different trees
Insert the values 10, 20, 30, 40 and 50 into an empty tree in that order. Each new value is larger than all the earlier ones, so it always goes to the far right. The result is a chain where every node has only a right child:
Insert the same five values in the order 30, 20, 40, 10, 50 and the tree looks completely different. The root is the middle value and the rest spread evenly on both sides:
Searching for 50 in the chain takes five comparisons; in the balanced tree, three. With five nodes the difference is small, but it becomes a gulf as the number of nodes grows.
2Height decides everything
A binary tree with n nodes has a height of at least ⌊log₂ n⌋ and at most n - 1. In the worst case a search makes one comparison more than the height. The table puts the two extremes side by side:
| Nodes n | Best height | Worst height | Comparisons in a worst-case search (good / bad) |
|---|---|---|---|
| 7 | 2 | 6 | 3 / 7 |
| 1,000 | 9 | 999 | 10 / 1,000 |
| 1,000,000 | 19 | 999,999 | 20 / 1,000,000 |
In a balanced tree of a million values every search ends within 20 steps. If the same values were inserted in sorted order, a search may take a million steps; that is no better than walking an ordinary list from start to end. Search, insertion and deletion all walk the same path, so all three are O(log n) in a balanced tree and O(n) in a chain.
3Which orders ruin the tree?
- Increasing order: 1, 2, 3, ... Every value goes to the far right and the tree becomes a chain leaning right.
- Decreasing order: 9, 8, 7, ... This time the tree becomes a chain leaning left.
- Zigzag: 10, 50, 20, 40, 30. The values come small, large, small in turn; every node still has one child and the height is 4.
- Many insertions and deletions over time: when deletions always take the successor from the same side, the tree can slowly lean to one side.
In real life data often arrives sorted or nearly sorted: increasing record numbers, events in date order, an alphabetical list of names. So an unbalanced tree is not just an exam question but a common problem. If the values are inserted in a truly random order, the height stays proportional to log n on average, but that is not a guarantee.
4The fix: self-balancing trees
The fix is for the tree to correct its shape after every insertion and deletion. Balanced trees do this with small link changes called rotations: a node and its child swap places, the inorder order stays the same, but the height goes down.
The AVL tree is the first example of this idea, described in 1962 by Adelson-Velsky and Landis. At every node the heights of the left and right subtrees may differ by at most 1; when the difference reaches 2, a rotation is done. The red-black tree keeps the balance looser and does fewer rotations. Java's java.util.TreeMap is a red-black tree, and C++ compilers usually implement std::map with one as well.
AVL rotations and heaps will be covered in the upcoming "Heaps and Balanced Trees" part. Until it is published, this is all you need: in a balanced tree, search, insertion and deletion take O(log n) even in the worst case.
5Try it
If 5, 4, 3, 2, 1 are inserted into an empty BST in that order, what is the height of the tree?
The answer is B. Each value is smaller than the one before, so it always goes left and a chain of five nodes forms. Height counts edges: a chain of 5 nodes has 4 edges.
Show the answer
The answer is B. Each value is smaller than the one before, so it always goes left and a chain of five nodes forms. Height counts edges: a chain of 5 nodes has 4 edges.
About how many comparisons does an unsuccessful search make in a balanced BST with 1,000,000 nodes?
The answer is A. The height of a balanced tree is about log₂ 1,000,000 ≈ 19. The search makes one comparison per level from the root down to a leaf: about 20.
Show the answer
The answer is A. The height of a balanced tree is about log₂ 1,000,000 ≈ 19. The search makes one comparison per level from the root down to a leaf: about 20.
Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.