Heaps and priority queues
A complete tree stored in an array keeps the minimum at the root and both insert and extract in logarithmic time.
A binary search tree keeps everything ordered. Often you do not need that — you only need to know the smallest element, then remove it and know the next smallest. A heap provides exactly that weaker guarantee for a fraction of the bookkeeping, and it needs no pointers at all: it fits in a flat array.
Weak order is enough
In a heap, every parent is smaller than its children, but siblings and cousins have no relation to each other. That is all that is required for the minimum to sit at the root. The weaker invariant means insert and extract only walk a single root-to-leaf path instead of rebalancing a tree.
Insert values and extract the minimum below. Each operation really sifts through the array; the tree drawn is just the same array read by index.
Insert and extract-min — the smallest always floats to the root
—
The heap property is local: every parent is smaller than its children, which is enough for the minimum to be at index 0 but not enough for the array to be sorted. Both insert and extract-min do at most log₂ n swaps, because each step halves the distance to the root or to a leaf.
The array is the tree
A heap is a complete binary tree — every level full except possibly the last, filled left to right — which lets it be stored contiguously with no pointers:
- index has children at and ;
- index has its parent at .
Complete means the array has no gaps, so the height is always — the tree is balanced by construction. That is what a heap gives up the BST's fully sorted order to gain.
Sift up and sift down
Two procedures maintain the property:
- Sift up after appending: while the new element is smaller than its parent, swap them. At most one path up, so .
- Sift down after removing the root: move the last leaf to the root, then repeatedly swap it with the smaller child until both children are larger. Again .
import heapq
heap = []
heapq.heappush(heap, 5)
heapq.heappush(heap, 2)
smallest = heapq.heappop(heap) # 2The array heap is the heap; heapq provides the operations. There is no
separate class.
Priority queues
The usual reason to reach for a heap is a priority queue: a queue that removes the item with the highest priority rather than the oldest. Because both operations are , scheduling many tasks costs overall instead of the a repeatedly re-sorted list would cost. Dijkstra's shortest-path algorithm and event simulation both run on this structure.
Heap is not sorted
The array is only partially ordered. Iterating it does not give sorted output,
and heap[1] is the smaller of the root's two children but not necessarily
the second-smallest element overall. To get sorted order, repeatedly pop.
Illustrative vs real
The simulator keeps the heap small enough to draw, and shows the array and its tree side by side. Real heaps hold millions of entries and are often built bottom-up in from an unsorted array. The index arithmetic and the two sift routines are identical; only the scale changes.
Check yourself
Eduspheria wiki · Programming & Data Structures, Data structures
0 / 5 answered
From the exam paper
Modeled on NITJ AI-507, End-Sem December 2024
0 / 5 answered
Where next: hash tables — O(1) lookup without maintaining any order at all.