Starting from an empty priority queue, what does each min() / removeMin() return in this sequence: insert(5,A), insert(9,C), insert(3,B), min(), removeMin(), insert(7,D), removeMin(), removeMin(), removeMin(), removeMin()?
min() → (3,B); then removeMin() returns (3,B), (5,A), (7,D), (9,C), and finally null because the queue is empty.
| Operation | Returns | Contents afterwards |
|---|---|---|
| insert(5,A) | {(5,A)} | |
| insert(9,C) | {(5,A), (9,C)} | |
| insert(3,B) | {(3,B), (5,A), (9,C)} | |
| min() | (3,B) | unchanged |
| removeMin() | (3,B) | {(5,A), (9,C)} |
| insert(7,D) | {(5,A), (7,D), (9,C)} | |
| removeMin() | (5,A) | {(7,D), (9,C)} |
| removeMin() | (7,D) | {(9,C)} |
| removeMin() | (9,C) | {} |
| removeMin() | null | {} |
Two things to notice. First, (7,D) was inserted after (5,A) and (9,C), yet it overtakes (9,C): only the key decides, never the arrival time. Second, min() only looks, so the next removeMin() returns the same entry.
The contents column is written as a sorted list for readability, but that is not a requirement. A priority queue may organise its entries internally however it likes (unsorted list, sorted list, heap); the only promise is that removeMin() returns the entry with the smallest key.