Quiz Entry - updated: 2026.11.26
How is a heap used to implement a priority queue, and why is min() then $O(1)$?
Each node of the heap stores one entry (key, value), ordered by key. The heap-order property puts the entry with the smallest key at the root, so min() just returns the root.
The mapping from priority queue to heap is direct:
- One entry per node. The heap is ordered by the entries' keys; the values just travel along. For example the root might hold (2, Susi), its children (5, Patrick) and (6, Markus), and below (5, Patrick) the entries (9, Jakob) and (7, Anna).
min()reads the root: $O(1)$.insert(k, v)adds a node at the next free position and restores the order by moving it up (upheap): $O(\log n)$.removeMin()takes the root out, moves the last node into its place and restores the order by moving it down (downheap): $O(\log n)$.size()/isEmpty()read a stored counter: $O(1)$.
Diagrams and exercises usually draw only the keys and leave the values out, since the values never influence where an entry goes.
Go deeper:
MIT 6.006 Lecture 8: Binary Heaps โ a full lecture building a priority queue on an array-based heap, with notes.