Binary Search Tree
calculator.
Insert keys into a binary search tree, then see its shape, traversals and search path.
- Shape
- Balanced
- Search
- Found, 3 compares
- Leaves
- 4
This is as short as 7 keys can be, so a search never makes more than 3 comparisons.
The tree
Heavier rings: the search pathAll 7 keys are in. The search for 60 visits 50, 70, 60, and finds it.
Traversals
- In-order (sorted)
- 20, 30, 40, 50, 60, 70, 80
- Pre-order
- 50, 30, 20, 40, 70, 60, 80
- Post-order
- 20, 40, 30, 60, 80, 70, 50
- Level order
- 50, 30, 70, 20, 40, 60, 80
Keys by depth
| Depth | Keys, left to right |
|---|---|
| 0 | 50 |
| 1 | 30, 70 |
| 2 | 20, 40, 60, 80 |
The same keys in other orders
| Order | Height | Balanced | Try it |
|---|---|---|---|
| As entered | 2 | Yes | Current |
| Smallest first | 6 | No | |
| Largest first | 6 | No | |
| Middle first | 2 | Yes |
Keyboard shortcuts
- Click the tree or Tab to it, then press Right arrow to insert the next key and Left arrow to take it back out.
- Home shows the first key alone; End shows the finished tree with the search path.
- The slider also moves with the arrow keys once it has focus.
Insertion log
| # | Key | Compared with | Placed |
|---|---|---|---|
| 1 | 50 | – | Rootfirst key |
| 2 | 30 | 50 | Left of 50, depth 1after 50 |
| 3 | 70 | 50 | Right of 50, depth 1after 50 |
| 4 | 20 | 50, 30 | Left of 30, depth 2after 50, 30 |
| 5 | 40 | 50, 30 | Right of 30, depth 2after 50, 30 |
| 6 | 60 | 50, 70 | Left of 70, depth 2after 50, 70 |
| 7 | 80 | 50, 70 | Right of 70, depth 2after 50, 70 |
Working
- 1Insert the first key as the root50 becomes the root
- 2Insert 30: smaller goes left, larger goes right30 < 50 → left: empty, so left child of 50
- 3Insert 70: smaller goes left, larger goes right70 > 50 → right: empty, so right child of 50
- 4Insert 20: smaller goes left, larger goes right20 < 50 → left, 20 < 30 → left: empty, so left child of 30
- 5Insert 40: smaller goes left, larger goes right40 < 50 → left, 40 > 30 → right: empty, so right child of 30
- 6Insert 60: smaller goes left, larger goes right60 > 50 → right, 60 < 70 → left: empty, so left child of 70
- 7Insert 80: smaller goes left, larger goes right80 > 50 → right, 80 > 70 → right: empty, so right child of 70
- 8Search for 60 by the same rule60 > 50 → right, 60 < 70 → left, 60 = 60 → found
- 9Height: count the edges down to the deepest key20 is 2 edges below 50: height 2
- 10Check: an in-order walk gives the keys in rising order20 < 30 < 40 < 50 < 60 < 70 < 80
How it's worked out
A binary search tree (BST) keeps one rule at every node, which Sedgewick and Wayne call symmetric order: every key in the left subtree is smaller than the node's key, and every key in the right subtree is larger.
To insert a key, start at the root. If the key is smaller, go left; if it is larger, go right. Repeat until you reach an empty spot, and put the key there. A key equal to one already in the tree is skipped, as in a set. Searching follows exactly the same path and stops when it finds the key or runs out of tree.
The keys go in one at a time, in the order you type them, so the order decides the shape. The steps list each insertion with the keys it was compared with, and the last step checks the rule: an in-order walk must give the keys in sorted order.
Height is the number of edges on the longest path from the root down to a leaf, so a tree with one node has height 0, as NIST defines it. A tree is height-balanced when, at every node, the two subtrees differ in height by at most 1. The shortest possible height for n keys is the one where every level is full except perhaps the last.
- In-order visits the left subtree, then the node, then the right subtree. On a BST this gives the keys in sorted order.
- Pre-order visits the node first, then left, then right. Inserting the keys in this order rebuilds the same tree.
- Post-order visits left, then right, then the node, so children come before their parent.
- Level order goes row by row from the root, left to right, using a queue.
Worked examples
Nadia checks a textbook exercise
Nadia inserts 50, 30, 70, 20, 40, 60, 80. 50 becomes the root. 30 is smaller, so it goes left; 70 goes right. 60 is compared with 50, 70 and becomes the left child of 70. The tree has height 2 and is height-balanced, with every level full.
In-order gives 20, 30, 40, 50, 60, 70, 80, which is sorted, as it must be. Pre-order is 50, 30, 20, 40, 70, 60, 80, post-order is 20, 40, 30, 60, 80, 70, 50 and level order is 50, 30, 70, 20, 40, 60, 80. The search for 60 visits 50, 70, 60: 3 comparisons. On average a search for a key in this tree takes 2.43.
Omar sees what sorted input does
Omar inserts 1, 2, 3, 4, 5, 6 in order. Each key is larger than every key before it, so each one goes to the right of the last. The tree is a chain of height 5, and finding 6 takes 6 comparisons, no better than reading a list.
The same six keys inserted middle first, 3, 1, 5, 2, 4, 6, give height 2, the shortest possible. That is the job a self-balancing tree, such as an AVL or red-black tree, does for you as keys arrive.
Ten keys in a random order
Inserting 41, 20, 65, 11, 29, 50, 91, 32, 72, 99 gives height 3, and the shortest possible for 10 keys is 3. The search for 72 visits 41, 65, 91, 72.
The same keys sorted first would make a chain of height 9. Random orders usually land close to the minimum: Sedgewick and Wayne give about 1.39 lg N comparisons for an average search in a tree built from N random keys.
Reading your result
The big number is the height. Search, insert and delete each follow one path from the root, so they take time in proportion to the height. A balanced tree gains a level only when the number of keys roughly doubles, while a chain gets one level deeper with every key.
Compare the height with the shortest possible. If the two match, the shape is as good as a plain BST can get. A height far above it means the keys arrived in a near-sorted order. The table of orders shows the same keys sorted, reversed and middle first.
Different orders can still give the same tree. 2, 1, 3 and 2, 3, 1 both put 2 at the root with 1 and 3 below: their pre-orders are 2, 1, 3 and 2, 1, 3. What matters is which key arrives first in each subtree.
Duplicates are skipped. In 5, 3, 5, 8 the second 5 meets the root and goes no further, so the tree holds 3 keys and lists 5 as skipped. Some courses keep duplicates with a count in each node instead.
Common mistakes
- Counting nodes instead of edges for the height. A path with 3 nodes has 2 edges. Check which your course uses: NIST and most textbooks count edges, while some libraries, such as GLib, count nodes.
- Comparing with the parent only. The rule holds for the whole subtree. In a tree with root 50, a key of 55 cannot sit in the left subtree, even under a node of 30.
- Sorting the keys before inserting. Sorted input is the worst case and makes a chain. Insert in the order the question gives.
- Mixing up pre-order and level order. Both start at the root, but pre-order finishes the whole left subtree before it moves right.