Intuition
Binary search halves a sorted array at each step. A binary search tree builds that halving into the structure itself: each node is a “middle” that sends smaller values left and larger values right.
Unlike a sorted array, a tree can take new values without shifting anything. A new value just follows the comparisons down until it finds an empty spot.
How it works
- Find: start at the root. If the value matches, stop. If it’s smaller, go left; if larger, go right. Reaching an empty spot means it isn’t there.
- Insert: search for the value. The empty spot where the search ends is exactly where it belongs.
- Delete has three cases:
- A leaf: just remove it.
- One child: that child’s subtree moves up into its place.
- Two children: copy in its successor (the smallest value in its right subtree), then delete the successor, which has at most one child.
Walking the tree left, node, right (an in-order walk) visits the values in sorted order.
Complexity
Every operation follows one path from the root, so its cost is the tree’s height. A balanced tree with n nodes is about log₂ n levels tall; 1,000,000 nodes need about 20 levels. But inserting already-sorted values builds a chain, n levels tall, and every operation becomes O(n). Self-balancing trees fix this by restructuring as they go.
Common mistakes
- Inserting sorted data into a plain BST. It degrades into a linked list. Shuffle first or use a balanced tree.
- Deleting a node with two children by removing it. Its subtrees would be lost. Replace it with its successor or predecessor.
- Checking only parent–child order. Every value in the whole left subtree must be smaller than the node, not just its left child.
In other languages
| Language | Sorted map or set in the standard library |
|---|---|
| Python | None (the sortedcontainers package is popular) |
| JavaScript | None |
| Java | TreeMap, TreeSet (red-black trees) |
| C | None |
| Go | None |
| Rust | BTreeMap, BTreeSet (B-trees: many values per node) |
Check yourself
Check yourself
Insert 5, 3, 8, 1, 4 into an empty BST. What is the root's left child?
5 is the root. 3 is smaller, so it becomes the left child; 1 and 4 then go below 3.
Check yourself
Insert 1, 2, 3, 4, 5 in that order. What shape is the tree?
Each value is larger than everything before it, so each goes right of the last: a chain 5 levels deep. Searching it is no faster than a linked list.
Sources
- Cormen, Leiserson, Rivest and Stein, Introduction to Algorithms, chapter 12, “Binary Search Trees” (3rd edition).
- Donald Knuth, The Art of Computer Programming, Vol. 3, section 6.2.2.
Finished this page?
Your list