When to use a BST
Use a balanced BST when you need:
- Keys kept in sorted order while data keeps changing, such as a live leaderboard.
- Range queries: "all orders between two dates", "all scores from 1,000 to 2,000".
- The nearest key: "the next job after now", "the closest price at or below 50".
- A guaranteed worst case of O(log n), with no bad inputs.
Use something else when:
- You only look up exact keys: use a hash map. It is faster and simpler.
- You only ever need the smallest or largest item: use a heap.
- The data never changes after loading: use a sorted array and binary search. It is smaller and faster in practice because it sits together in memory.
- The data is far bigger than memory and lives on disk: use a B-tree, which is what databases do.
Do not write your own BST in production. Use your language's sorted map (see the real-world section), because it is already balanced and tested. Writing one by hand is for learning and interviews.
How a binary search tree works
A binary search tree (BST) is a binary tree with one rule that holds at every node:
Everything in the left subtree is smaller than the node, and everything in the right subtree is larger.
That rule turns every comparison into a decision that throws away half of what is left, just like binary search on a sorted array. The difference is that a BST also lets you insert and delete without shifting elements around.
Try it
Insert a few numbers, then search for one. Watch how each comparison picks a side and how the comparison counter grows with the height of the tree, not with the number of nodes.
Binary search tree
Step 15 of 17 · Press Space to play, arrow keys to stepInsert 80: compare with 50. 80 is larger, so go right.
- comparisons:
- 1
- height:
- 3
- nodes:
- 6
Things to try:
- Insert
50, 30, 70, 20, 40, 60, 80. The tree is perfectly balanced, and any search takes at most 3 comparisons. - Now insert
10, 20, 30, 40, 50, 60, 70in that order. Every node goes right and the tree becomes a linked list. A search for70now takes 7 comparisons.
BST operations: search, insert and delete
Search. Start at the root. If the target is smaller, go left. If it is larger, go right. Stop when you find it, or when you reach an empty spot, which means the value isn't in the tree.
Insert. Search for the value. The empty spot where the search ends is exactly where the new node belongs.
Delete. There are three cases:
- Leaf: just remove it.
- One child: replace the node with its child.
- Two children: replace the node's value with its in-order successor, the smallest value in its right subtree. Then delete the successor's old node. It has at most one child, because a smallest value can't have a left child.
In-order traversal (left, node, right) visits values in sorted order. This is a favourite interview fact.
BST time complexity
| Operation | Balanced tree | Worst case (skewed) |
|---|---|---|
| Search | O(log n) | O(n) |
| Insert | O(log n) | O(n) |
| Delete | O(log n) | O(n) |
| In-order traversal | O(n) | O(n) |
| Space | O(n) | O(n) |
The worst case happens when values arrive already sorted. Self-balancing trees such as AVL and red-black trees fix this by rotating nodes (re-linking a few nodes so the tree gets shorter) after inserts and deletes so the height stays O(log n).
BST vs other data structures
The right choice depends on what you do most. Here is the same job done with each structure. n is the number of items.
| What you need | Balanced BST | Hash map | Sorted array | Heap | Linked list |
|---|---|---|---|---|---|
| Find one exact key | O(log n) | O(1) average | O(log n) | O(n) | O(n) |
| Insert a new key | O(log n) | O(1) average | O(n), shifts items | O(log n) | O(1) at the head |
| Delete a key | O(log n) | O(1) average | O(n), shifts items | O(log n) | O(n) to find it |
| Walk keys in sorted order | O(n) | O(n log n), sort first | O(n) | O(n log n) | O(n log n) |
| Range query ("between a and b") | O(log n + k) | O(n), scan all | O(log n + k) | O(n) | O(n) |
| Nearest smaller or larger key | O(log n) | O(n) | O(log n) | O(n) | O(n) |
| Smallest or largest item | O(log n) | O(n) | O(1) | O(1) | O(n) |
k is the number of results returned. The bold cells show where each structure wins. A balanced BST is rarely the fastest at any single job, but it is the only one that is good at all of the ordered jobs while still allowing cheap inserts.
How to explain a binary search tree in an interview
- State the invariant first: "left is smaller, right is larger, at every node."
- Say the complexity in terms of height h, then explain that h is log n when balanced and n when skewed.
- Mention that a hash map gives O(1) average lookup but no ordering. Pick a BST when you need sorted iteration, range queries, or the nearest smaller or larger value.