Why balance matters
Inserting sorted keys turns a BST into a chain with O(n) search. Compare the tree’s height with log n to see why balance matters.
Before you start: Validating a BST and the in-order property.
The idea
Insert 1 to 7 in sorted order and the BST becomes a chain. Finding 7 takes 7 comparisons.
Each new key is bigger than every key so far. So it always goes right, below the last one.
Intuition
The same 7 keys fit in a tree of height 3. Every search then takes at most 3 comparisons.
Search walks one root-to-leaf path. The height is the longest such path, so it caps the cost.
Each level holds twice as many keys as the one above. Height 10 already holds 1,023 keys.
A tree of height h holds up to 2^h − 1 keys. So n keys need a height of only about log2 n.
Complexity
Search costs the height of the tree. A chain gives O(n), while a balanced tree of height about log2 n gives O(log n).
The middle-first build visits each key once, so it costs O(n) time. Its recursion is only O(log n) deep.
Pitfalls
Equal heights at the root are not enough. Every node needs subtrees whose heights differ by at most 1.
Here each side of 4 is a short chain. With more keys those chains grow, and search becomes linear again.
Variations
Self-balancing trees repair the shape after each insert. AVL keeps each balance factor (bf, left minus right height) within ±1.
Red-black trees use node colours instead. A middle-first build works only when all keys are known up front.
Code
Pseudocode
build(keys)
root ← null
for key in keys
root ← insert(root, key)
record height(root)
return root
Python
def build(keys):
root = None
heights = []
for key in keys:
root = insert(root, key)
heights.append(height(root))
return root, heights
JavaScript
function build(keys) {
let root = null;
const heights = [];
for (const key of keys) {
root = insert(root, key);
heights.push(height(root));
}
return { root, heights };
}
Java
List<Integer> build(int[] keys) {
TreeNode root = null;
List<Integer> heights = new ArrayList<>();
for (int key : keys) {
root = insert(root, key);
heights.add(height(root));
}
return heights;
}
Practice problems (6)
Height after sorted inserts Warm-up
The keys 1, 2, 3, 4, 5, 6 are inserted in this order into an empty BST. No rebalancing happens.
Answer each question before the step plays.
Cost of search in a chain Warm-up
A BST was built by inserting the keys 1, 2, …, n in increasing order. What is the worst-case cost of searching it for a key?
Choose an insertion order for balance Standard
You will insert the keys 1 to 7 into an empty plain BST and want a tree of height 3. A safe rule is middle first: insert the middle key, then do the same for the keys left of it, then for the keys right of it.
Answer each question before the step plays.
Height of a tree Standard
Return the height of the binary tree with the given root. The height is the number of nodes on the longest path from the root down to a leaf. An empty tree has height 0 and a single node has height 1.
Is the tree height-balanced? Standard
A binary tree is height-balanced when, at every node, the heights of the left and right subtrees differ by at most 1. Return true if the tree with the given root is height-balanced and false otherwise. An empty tree is balanced.
Build a balanced BST from a sorted array Challenge
The array nums holds distinct integers in increasing order. Build a height-balanced BST that holds every key and return its root.
Make the tree unique: the root of each range lo..hi is the key at index (lo + hi) / 2 rounded down. Its left subtree is built from the keys before it, its right subtree from the keys after it.
Related topics
Next on the path: AVL trees and rotations