← again tap: answer good → ↑ swipe up at the end: next
Question
What is a priority queue, and which operations does it offer?
Answer
A priority queue stores entries, each a (key, value) pair, and always hands out the entry with the smallest key first. Its core operations are insert(k, v) and removeMin(); min(), size() and isEmpty() complete it.
An ordinary queue serves elements in the order they arrived. A priority queue ignores arrival order: the key of each entry is its priority, and whatever entry has the smallest key is served next, no matter when it came in. The value is the payload that travels with it (a name, a task, an order).
| Operation | What it does |
|---|---|
insert(k, v) |
adds a new entry (k, v) |
removeMin() |
removes the entry with the smallest key and returns it |
min() |
returns the entry with the smallest key without removing it (like a stack's top() next to pop()) |
size(), isEmpty() |
number of entries / whether there are none |
Keys must be comparable, either naturally (numbers, strings) or through a comparator supplied to the queue. That total order is all the queue needs to know.
Typical uses are anything where "most urgent first" beats "first come, first served": the best bid in an auction, the best price in a stock exchange order book, a waiting system such as hospital triage or a process scheduler.
Tip: "smallest key = highest priority" is a convention. A queue that serves the largest key first works identically with the comparison reversed.
Go deeper:
Priority queue — the abstract data type, its implementations and its applications, from Dijkstra to Huffman coding.
java.util.PriorityQueue — the standard Java heap-based priority queue, with the cost of each method.
Python heapq — the standard-library min-heap on a plain list, with notes on priority-queue use.
Question
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()?
Answer
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.