Definition · Min-heapA min-heap is a complete binary tree whose every node satisfies the heap property: the key at a node is less than or equal to the keys at both of its children. Complete means every level is full except possibly the last, which is filled from the left.
The root therefore holds a minimum, since it is an ancestor of every node and the property is transitive along a path. A max-heap reverses the inequality and nothing else about it changes.
Completeness is what allows the array layout, and the array layout is what removes the pointers. Write the levels out in order, left to right. Then for a position i counted from 1, the children sit at 2i and 2i + 1 and the parent at ⌊i/2⌋. The lab draws its array with positions counted from 0, where the same tree has its children at 2i + 1 and 2i + 2 — the arithmetic differs by where you start counting and the tree is identical, which is worth confirming once on the picture rather than believing.
array 1 3 2 6 8 7 4 9 positions from 1
1 level 0
3 2 level 1
6 8 7 4 level 2
9 level 3
every path down increases: 1 3 6 9, 1 3 8, 1 2 7, 1 2 4
siblings are unrelated: 3 sits left of 2, 8 left of 7
That array is what the lab holds after inserting 1, 9, 2, 8, 3, 7, 4, 6 in that order, and it is a perfectly valid heap. It is also not sorted, not nearly sorted, and not sorted after any prefix: the second element is 3 and the third is 2. If a heap were sorted then building one would sort, and sorting costs more than building a heap does — which is the argument that ought to make the misconception uncomfortable before the picture does.
Insert, by sifting up
Put the new key in the first free array position, which keeps the tree complete. Then while it is smaller than its parent, swap the two. The property can only be violated between the new key and its ancestors, and each swap repairs one level, so the walk stops at the root at the latest: at most ⌊log₂ n⌋ comparisons and as many swaps.
Example · The insertion that swaps, and the one that does notInserting 8 into the three-element heap the lab has after 1, 9, 2 compares 8 against its parent 9, swaps, compares against 1, and stops: 2 comparisons, 1 swap, against a bound of ⌊log₂ 4⌋ = 2. Inserting 7 a step later costs 1 comparison and no swap at all, because it lands under 2 and is bigger. Both are O(log n); one of them did nothing.
Extract-min, by sifting down
The root is the answer. Removing it leaves a hole, so move the last element into the root — which keeps the tree complete — and then push it down: compare its two children with each other, then compare it with the smaller one, and swap if it is larger. Two comparisons per level, ⌊log₂ n⌋ levels, so at most 2⌊log₂ n⌋.
heap 1 3 2 6 8 7 4 9 extract the minimum, 1
move 9 to the root: 9 3 2 6 8 7 4
children of 9 are 3, 2 → smaller is 2 → 9 > 2, swap
2 3 9 6 8 7 4
children of 9 are 7, 4 → smaller is 4 → 9 > 4, swap
2 3 4 6 8 7 9
comparisons 4, swaps 2, bound 2⌊log₂ 7⌋ = 4 tight here
Four comparisons against a bound of four: the sift reached a leaf, which is the worst case at that size, and the counter and the bound agree. That agreement is a coincidence of this input, and the very next extraction in the lab also costs 4 while the inserts before it cost between 0 and 2 against bounds of 0 to 3. Over the whole ten-operation sequence the lab counts 18 comparisons and 7 swaps, and reports that every single step came in at or under its bound.
That last report is the one to be careful with. “Every step came in under its bound” is a statement about ten operations on eight keys. The bound itself is a statement about a path in a complete tree, and the proof is one line: a complete tree on n nodes has height ⌊log₂ n⌋, a sift visits each level at most once, and each level costs at most two comparisons. The lab can show you the walk; it cannot show you that there is no taller tree.