1. A complete tree
A heap is a complete binary tree: every level is full except possibly the last, which fills from left to right. Completeness is what lets the heap live in a flat array with no pointers. The heap property adds one rule: in a min-heap, every parent is no larger than either child. The root is therefore the minimum.
A max-heap flips the inequality. The structure is the same; only the comparison changes.
2. The array view
Store the tree level by level. The root is index 0. For any index i, the parent, left child, and right child are pure arithmetic. No node objects, no child pointers.
This is why a heap is cache-friendly and why the array on the playground and the tree are two views of exactly the same data.
3. Insert & sift-up
Insert appends the new value at the end of the array, which keeps the tree complete. It may violate the heap property with its parent, so compare and swap upward until the parent is smaller or the value reaches the root. The new value travels one root-to-leaf path, so insert is O(log n).
i = heap.length - 1
while i > 0 and heap[parent(i)] > heap[i]:
swap(heap[parent(i)], heap[i])
i = parent(i)Example: insert 0 into [1, 3, 2, 7, 5, 4]
Append 0: [1, 3, 2, 7, 5, 4, 0]. Its parent is index 2, value 2.
Swap: [1, 3, 0, 7, 5, 4, 2]. Its new parent is index 0, value 1.
Swap: [0, 3, 1, 7, 5, 4, 2]. The root is now 0, and every parent is smaller.
4. Extract & sift-down
Extract-min returns the root. To keep the tree complete, move the last element to the root and shrink the array. The new root may be larger than its children, so compare it with both children and swap with the smaller one until the heap property holds. This is sift-down, also O(log n).
i = 0
while left(i) < heap.length:
smallest = i
if left(i) < n and heap[left(i)] < heap[smallest]: smallest = left(i)
if right(i) < n and heap[right(i)] < heap[smallest]: smallest = right(i)
if smallest == i: break
swap(heap[i], heap[smallest])
i = smallest5. Build, sort, schedule
Building a heap from an unordered array takes O(n), not O(n log n), if you heapify from the last parent down to the root. Most of the nodes are near the bottom, where sift-down is cheap. Heapsort then extracts the minimum n times: O(n log n) time, O(1) extra space, not stable.
- Priority queue — insert and extract-min in O(log n), peek in O(1).
- Dijkstra / A* — the frontier is a priority queue keyed by distance.
- Scheduling — always run the earliest deadline or highest priority job.
- Top-k — keep a size-k heap instead of sorting the whole stream.
Heap vs sorted array
A sorted array gives O(1) minimum and O(log n) search, but insertion is O(n) because every later element shifts. A heap gives O(log n) insert and O(log n) extract with no shifting. If you only need the minimum repeatedly, the heap wins.
6. Practice
What are the parent and children of index 4 in a zero-based heap array?
Insert 2 into the min-heap [1, 4, 3, 9, 7, 8]. What is the final array?
Why is build-heap O(n) instead of O(n log n)?
Is heapsort stable? What is its extra-space cost?
7. Resources
Open the heap playground and insert a few values. Watch the new value climb one path, and watch the last value slide down after an extract. The array readout is the tree.
Continue the track
Open the playground for this topic
Heap Playground →Insert and extract values from a min-heap and watch the tree and the array stay in sync.