← All Posts
DSA · Heaps · Overview

Heaps: The Complete Guide

A binary search tree promises you a total order: left < node < right, everywhere. A heap promises far less — only that a parent beats its children. Nothing about siblings, nothing about cousins, nothing about subtrees relative to each other.

That weakness is the entire point. A promise that weak can be maintained by a tree that is always perfectly balanced, which means the shape is fully determined by the element count, which means the tree needs no pointers at all — it collapses into a flat array where 2i+1 and 2i+2 replace child links. No allocation, no rebalancing, no per-node overhead, and the nodes every operation touches sit at the front of the array where the cache keeps them.

What mastery means here: you can write sift_up and sift_down from memory with the correct bounds guards; you can say instantly whether a problem wants a min-heap or a max-heap and defend it; you can explain why bottom-up build is O(n); you can write Dijkstra with lazy deletion and say what the stale check is for; and you know three situations where a heap is the wrong answer. This series is ordered to get you there.

The One Rule Behind Everything

If you remember one sentence from all seventeen parts, make it this:

The root of your heap should be the element you are most willing to throw away.

Every direction question resolves against it. Want the k largest? The thing you would discard is the smallest of what you have kept — so, a min-heap. Sorting ascending in place with heapsort? Each round you evict the largest remaining into the rightmost free slot — so, a max-heap. Regret greedy? You want to revoke your most expensive commitment — so, a max-heap of costs. Direction mistakes are the single most common heap bug in interviews, and this sentence eliminates all of them.

The Roadmap

Foundations

The structure itself: shape, encoding, invariant, and the two repair routines that constitute nearly all of the code.

Core Techniques

The patterns that cover essentially every heap problem you will be asked.

Advanced Structures

What to do when the plain binary heap is not enough.

Mastery

Complexity Cheat Sheet

OperationCostNote
topO(1)read index 0
pushO(log n)often O(1) in practice
popO(log n)usually the full descent
build from n itemsO(n)bottom-up, not n pushes
heapsortO(n log n)O(1) extra space, unstable
top-kO(n log k)O(k) space, streaming
k-way mergeO(N log k)O(k) space
DijkstraO(E log V)O(E log E) with lazy deletion
search / containsO(n)no ordering to guide it
merge two heapsO(n)use a pairing heap for O(1)

When a Heap Is the Wrong Answer

You needUse insteadWhy
Membership testsunordered_setheap search is O(n)
k-th smallest, once, in memorynth_elementO(n) average, beats O(n log k)
Sliding window extrememonotonic dequeO(n) versus O(n log n)
Frequent arbitrary erasestd::setheaps have no erase
Both min and maxtwo heaps or std::setone heap serves one end
Small fixed priority rangebucket queueO(1) instead of O(log n)
Sorted iterationstd::sortheap order is not sorted order

Start with Part 1.