Heaps
A tree that always keeps its smallest value on top. Seven steps: the heap property, the array behind it, peek, insert, extract, and building a heap from scratch.
In 30 seconds
A heap is a binary tree in which every parent is no bigger than its children (a min-heap), so the smallest value is always at the top. It is kept in a plain array, read level by level.
Looking at the smallest value takes one step. Adding a value, or removing the smallest, takes a few swaps along one path from top to bottom: about log2 n of them.
A heap is not fully sorted. It does just enough ordering to hand over the next smallest item quickly, which is what priority queues and heapsort need.
Heaps, step by step
A max-heap is the same with every comparison reversed. “Heap” here is the binary heap; it has nothing to do with the heap area of memory that programs allocate from.
- Step 1: The heap property. A heap is a binary tree with one rule, the heap property: in a min-heap no child is smaller than its parent. Follow any path downward and the values never decrease, so the smallest value is always at the root. Nothing else is promised: siblings come in any order, and the tree is not sorted. A max-heap flips the rule.
- Step 2: A complete tree in an array. A heap is also kept complete: every level full, the last one filled from the left. That lets it live in a plain array, level by level, with no pointers. The children of index i are at 2i + 1 and 2i + 2, its parent at floor((i − 1) / 2). For 5 at index 1, the children are at 3 and 4: the values 9 and 6.
- Step 3: Peek: the root, in O(1). Because of the heap property, the smallest value is always the root, and the root is always the first array cell. Reading it (peek) needs no searching and no comparisons: one step, however many values the heap holds. The largest value is a different matter: it is somewhere among the leaves.
- Step 4: Insert: sift up. To insert, put the new value in the next free spot, the end of the array, so the tree stays complete. Then repair the order: while the value is smaller than its parent, swap them. Here 1 passes 3 and then 2 and becomes the new root. It can climb at most one level per swap, and a complete tree has only about log2 n levels.
- Step 5: Extract the minimum. To remove the minimum, take the root, then move the last value into its place so the tree stays complete. That value is probably too big for the top, so sift it down: swap it with its smaller child until neither child is smaller. Here 3 swaps with 2 and stops above 8. Again at most one swap per level: O(log n).
- Step 6: Build a heap from any array. Any array can be turned into a heap in place. The leaves are already tiny heaps, so start at the last node that has a child and work back to the root, sifting each value down. Here 8 sinks below 3, then 6 below 2, then 9 goes down twice, below 2 and then 5. Four swaps and the whole array obeys the heap property.
- Step 7: Why building costs only O(n). Sifting down costs at most one swap per level below the node, and most nodes are near the bottom. In a heap of 15, the 8 leaves cannot move, 4 nodes can sink one level, 2 can sink two, and only the root can sink three: 11 swaps at most. The total always stays below n, so building is O(n), cheaper than n inserts at O(log n) each.
Try it yourself
Learn this first
Spotted a mistake on this page?