Binary Search Trees and Heaps
Lesson 15 of 20Watch inserts compare their way down a BST — then a new minimum sift all the way to the top of a min-heap.
The big picture
Two trees, two promises. A BST promises total order — smaller left, larger right. A heap promises only that parents beat children — and buys O(1) minimums with it.
The BST rule
At every node: smaller values live in the left subtree, larger in the right. One comparison per level picks the direction.
Search cost = height
A balanced BST with 1,000,000 nodes is about 20 levels deep — 20 comparisons find any value, insert any value.
Inserts land at leaves
A new key walks a root-to-leaf compare path and attaches to the first empty slot. Nothing ever shifts or moves.
The heap trade
A min-heap only promises parent ≤ children. The minimum is always at the root (O(1)), but hunting an arbitrary value costs O(n).
Heaps live in arrays
Children of index i sit at 2i+1 and 2i+2; the parent is (i-1)/2. No pointers, no allocations — completeness makes the math work.
STL gives you both
std::map is a balanced BST under the hood; std::priority_queue is a heap — a max-heap unless you flip the comparator.
BST insert live
Two inserts, two compare walks — each one ends at the first empty slot.
A BST holding 50, 30, 70, 20, 40. The rule: go left for smaller, right for larger. Watch 60 find its slot.
Heap array trick
A heap stores its whole tree inside a plain array — the indexes do all the pointing.
Walkthrough
A BST insert walks the compare path in code — then 9 sifts up a real min-heap array.
1#include <iostream>2using namespace std;3struct Node {4int val;5Node* left;6Node* right;7};8Node* insert(Node* root, int v) {9if (!root) return new Node{v, nullptr, nullptr};10if (v < root->val) root->left = insert(root->left, v);11else root->right = insert(root->right, v);12return root;13}14int main() {15Node* root = insert(nullptr, 50);16insert(root, 30);17insert(root, 70);18insert(root, 60);19cout << root->right->left->val << endl;20return 0;21}
The very first insert: root is nullptr — line 9 will return a brand-new node.
Heapify up & down
Insert 9 and watch sift-up — then extract the minimum and watch sift-down repair the heap.
Seven values already form a valid min-heap: 10 on top, and every parent ≤ its children. We insert 9.
Predict the output
One loop checks every child against its parent — is this array a valid min-heap?
1#include <iostream>2using namespace std;34int main() {5int h[] = {5, 9, 7, 12, 10, 6};6bool ok = true;7for (int i = 1; i < 6; i++)8if (h[(i - 1) / 2] > h[i]) ok = false;9cout << (ok ? "min-heap" : "broken");10return 0;11}
Hint: Child i must be ≥ its parent h[(i-1)/2]. Five pairs to check — one of them fails. Integer division: (5-1)/2 = 2.
Concept check
BSTs and heaps are both trees — click each term to pin down exactly what it promises.
Key takeaways
BST rule: smaller goes left, larger goes right — one comparison per level.
BST insert and search cost O(h): log n when balanced, n when the tree degenerates into a line.
Heaps promise less than BSTs — parent ≤ children — but hand you the minimum in O(1).
A heap is a complete tree, so it lives in an array: kids at 2i+1 and 2i+2, boss at (i-1)/2.
Sift-up repairs inserts; sift-down repairs extract-min — both are O(log n) swaps along one path.
std::priority_queue is your STL heap; std::map is your STL balanced BST.