3.2.9 · D3Linear Data Structures

Worked examples — Priority Queue — concept (heap-based implementation covered later)

3,499 words16 min readBack to topic

This page is the "no surprises" drill for the Priority Queue concept note. We take every kind of situation a priority queue can throw at you and work one example through each — slowly, from zero, guessing the answer before we compute it.


The scenario matrix

Read each row as "a kind of situation you must never be surprised by."

# Case class Concrete twist Covered by
1 Min-PQ, distinct priorities ordinary extract order Example 1
2 Max-PQ via negation want largest out first, only min-PQ available Example 2
3 Ties (equal priorities) stable vs unstable removal Example 3
4 Negative & mixed-sign priorities zero and negatives together Example 4
5 Degenerate inputs empty PQ, single element, all-equal Example 5
6 Size-capped PQ (top-K) keep only K best from a stream Example 6
7 Real-world word problem ER triage over time, insert & extract interleaved Example 7
8 Cost / limiting behaviour why vs actually matters Example 8
9 Exam twist compound key to force FIFO ties Example 9

Before any of this, one picture of the behaviour we are testing — the top always leaves first.

Figure — Priority Queue — concept (heap-based implementation covered later)

Read the figure (Figure s01 — min-PQ output belt): on the left are the four elements as they sit inside the PQ — labelled boxes A:5, B:2, C:8, D:4, drawn in their insert order A, B, C, D to stress that this order is irrelevant to what leaves. The green arrow shows the very first extract-min reaching past A into the set and grabbing the smallest priority, B:2. On the right is the output belt, a vertical stack that fills top-to-bottom in ascending priority () as the time arrow flows downward. The single idea the figure teaches: repeated min-removal turns any jumble of priorities into a sorted output stream — and that belt is exactly what we now stress-test, case by case.


Example 1 — Min-PQ, all distinct (Case 1)


Example 2 — Max-PQ from a min-PQ by negation (Case 2)


Example 3 — Ties: is a PQ stable? (Case 3)


Example 4 — Negative and mixed-sign priorities, including zero (Case 4)


Example 5 — Degenerate inputs: empty, single, all-equal (Case 5)


Example 6 — Size-capped PQ: top-K from a stream (Case 6)


Example 7 — Real-world word problem: ER triage over time (Case 7)


Example 8 — Limiting behaviour: why the balance matters (Case 8)

The figure below plots both growth curves on a log–log grid so you can see the gap open up.

Figure — Priority Queue — concept (heap-based implementation covered later)

Read the figure (Figure s02 — cost growth curves): the horizontal axis is (number of elements) and the vertical axis is total steps, both on log scales so that power-law curves appear as straight lines. The red curve is the unsorted list's cost; the green curve is the heap's cost. The red line is visibly steeper, so it climbs far faster. Two dots mark : red sits at , green at , and the yellow annotation points at the roughly vertical gap between them. The one idea: the two curves never cross back — the heap's advantage only widens as grows, which is the entire justification for learning heaps next.


Example 9 — Exam twist: force FIFO on ties with a compound key (Case 9)


Active Recall

Recall Q: In Example 7, why is the emitted priority sequence

not sorted? Because inserts happen between extracts. Each treat returns the min of whoever is present at that moment, not the global min of all patients ever.

Recall Q: For "top-K largest," why do we use a

min-PQ and drop its minimum? The kept set's smallest is the weakest survivor. extract-min removes exactly that when size exceeds K, so the K largest remain — in only memory.

Recall Q: What must you always check before

extract-min? That the PQ is non-empty. Extracting from an empty PQ is undefined (error / sentinel).


Connections

  • Parent concept note
  • Heap — makes both insert and extract ; Example 8's fast column.
  • Binary Tree — the shape a heap secretly is.
  • Queue (FIFO) — the stable, arrival-ordered contrast to Example 3/9.
  • Stack (LIFO) — the other ordering discipline.
  • Dijkstra's Algorithm — the interleaved insert/extract of Example 7 at scale.
  • Huffman Coding — repeatedly extract two minima.
  • Abstract Data Type — PQ promises behaviour, not implementation.