1. The playground
The tree is a complete binary tree; the row of boxes is the same heap stored in an array. Insert appends a value and sifts it up. Extract removes the root, moves the last value to the root, and sifts it down. The colors mark the active path.
2. What to notice
- The root is always the minimum. The second-smallest value is one of its children, not necessarily the left one.
- Insert touches one root-to-leaf path. A value that starts large may not move at all; a new minimum climbs all the way to the root.
- Extract replaces the root with the last value, then sifts down. The array and the tree always show the same sequence.
- The array is partially ordered, not sorted. A parent is smaller than its children, but siblings have no order.
3. The equations behind it
The index maps are pure arithmetic, which is why the heap needs no pointers.
Index maps
Both insert and extract touch at most one path, so both are O(log n). Peek is O(1). Building a heap from an arbitrary array by bottom-up heapify is O(n).