3.2.9 · D1Linear Data Structures

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

3,049 words14 min readBack to topic

This is the foundations page. The parent note throws around a pile of vocabulary — priority, insert, extract-min, peek, growth symbols like , and the words FIFO, LIFO, plus a few we will define in their own sections below. If any of those made you pause, start here. We build each one from zero, anchor it to a picture, and say why the topic needs it — in an order where each idea rests on the one before, so nothing is used before it is explained.


0. A collection: the most basic picture

Before "priority", before "queue", there is just the idea of a collection — a bag that holds items.

Figure — Priority Queue — concept (heap-based implementation covered later)
Figure — a rectangular container holds four labelled boxes A, B, C, D floating in no particular order. The caption underneath stresses that we can add and remove, but the rule for "who leaves next" is still undecided.

Why the topic needs it: a priority queue is a collection plus one rule ("most important leaves next"). You can't understand the rule until you can see the bag it governs.


1. Order-out rules: FIFO vs LIFO vs Priority

A collection becomes useful the moment we fix a rule for who leaves next. There are exactly three rules you must be able to tell apart.

Figure — Priority Queue — concept (heap-based implementation covered later)
Figure — three panels share the same items A, B, C. Left (cyan): a FIFO queue with an amber arrow showing A leaving first (earliest arrival). Middle: a LIFO stack with C leaving first (latest arrival, taken off the top). Right: a min-priority queue where the badges read p=5, p=2, p=8 and the amber arrow pulls out B (p=2, the smallest) first, ignoring arrival order entirely.

Why the topic needs it: the word "queue" in "priority queue" is misleading. Look at the right-hand panel — the priority queue does not obey the arrival order; it picks B because its priority is smallest. Knowing all three rules is the only way to see clearly what a priority queue is not.


2. Priority: a label with an order

Most often is just a number, and numbers are easy to compare. But the only thing a priority really needs is a total order — a rule that, given any two labels, tells you which comes first (with no ties left undecided).

Why the topic needs it: priority is the whole point. Without a comparable label there is nothing to sort by, and "most important next" is meaningless. Real programs use strings, timestamps and custom objects as priorities all the time — numbers are just the easiest example to draw.


3. The three operations: insert, extract, peek

A collection is only defined by what you can do to it. For a priority queue there are exactly three actions.

Figure — Priority Queue — concept (heap-based implementation covered later)
Figure — a central box labelled "priority queue" holds three items stacked with B (p=2) at the top in amber marked "top = winner", then A (p=5) and C (p=8) below. A cyan arrow on the left labelled insert(x, p) feeds an item in. An amber solid arrow on the right pulls B out (extract-min → B: remove AND return). A white dashed arrow below it labelled peek → B only looks — the item stays inside.

Why "and return"? extract does two jobs at once: it removes the winner from the collection and hands it back to you. peek does only the "look" half — the item stays inside.

Why the topic needs it: these three verbs are the priority queue's contract — and a contract must say what happens at its boundary. An algorithm like Dijkstra loops while queue not empty, so knowing the empty behaviour is what makes the loop terminate safely.


4. Abstract Data Type (ADT): a promise, not a machine

The parent note calls a priority queue an ADT. This word trips people up, so we build it slowly.

Why "abstract"? Because we promise behaviour only. This is powerful: you can write Dijkstra's algorithm using only the PQ contract, then later swap in a fast Heap without changing a line of the algorithm.

Why the topic needs it: the parent note keeps saying "a heap is just one way to build it". That sentence only makes sense once you accept that the priority queue is the contract and the heap is the machine fulfilling it.


5. Speed language: Big-O, , ,

The parent note compares implementations using , , . Here is that language from zero.

  • constant: the work does not depend on at all. Doubling changes nothing. Picture: grabbing the top plate — same effort with 5 plates or 5000.
  • linear: work grows in step with . Doubling doubles the work. Picture: scanning every item once to find the smallest.
  • logarithmic: work grows very slowly. Doubling adds just one extra step. Picture: the number of times you can halve before reaching 1.

Figure — Priority Queue — concept (heap-based implementation covered later)
Figure — three curves on axes "n (number of items)" vs "work (steps)". A flat cyan line at height 1 is O(1). An amber curve rising slowly is O(log n), with an annotation "doubling n adds one step". A straight white diagonal is O(n), annotated "doubling n doubles work". The picture makes visible that the amber log curve stays far below the white linear line as n grows.

Why the topic needs it: the whole reason heaps exist is a speed argument. Without Big-O you cannot see why per operation crushes per operation when you do millions of operations. This is the payoff line of the parent note.


6. Stability and tie-breaking: the pair

The parent note warns that PQs are not stable. One last piece of vocabulary.

Why the topic needs it: real schedulers must break ties predictably. The pair is the standard trick, and you need to understand pairs as compound priorities to read it.


Putting it together: the prerequisite map

Here is how every idea above stacks up into the topic. Read it top-down: the boxes at the top are the raw ingredients (a collection, a comparable priority), the middle row shows the three order-out rules and the operations they enable, and every arrow means "is needed to understand". Follow any path of arrows down to the bottom box Priority Queue — that path is exactly the reading order of this page. If a box feels shaky, jump back to its section before continuing.

Collection: a bag of items

Order-out rule

FIFO Queue

LIFO Stack

Priority order

Priority: a comparable label with total order

Operations: insert extract peek

Empty-queue edge case

ADT: behaviour not machine

Big-O: O1 On Ologn

Why heaps are worth it

Priority Queue

Pair p and seq for ties

Trace one path as a check: Collection → Order-out rule → Priority order → Operations → Priority Queue. Reading those five boxes in order gives you the shortest honest definition of the topic. The side branches (FIFO/LIFO for contrast, Big-O for the speed argument, the empty case and tie-breaking for the boundaries) fill in why each choice matters.


Equipment checklist

Cover the right-hand side and answer aloud. If any stalls, reread that section above.

A "collection" is
a group of stored items you can add to and remove from, with no order rule stated yet.
FIFO means
First In, First Out — the earliest arrival leaves first (a normal queue).
LIFO means
Last In, First Out — the latest arrival leaves first (a stack).
Priority order means
the most important labelled item leaves first, regardless of arrival time.
A priority label only needs to be
totally ordered — comparable so you can always tell which of any two wins (numbers, strings, timestamps, custom objects all qualify).
In a MIN priority queue, the winner is the item with
the smallest priority label.
The three PQ operations are
insert(x,p), extract-min/max, and peek.
The difference between extract and peek is
extract removes AND returns the winner; peek only looks, leaving it inside.
Calling extract-min or peek on an EMPTY queue
has no winner to return — libraries either throw an error, return null/nothing, or require you to check isEmpty() first.
An ADT is
a definition by behaviour/contract only, hiding how it is built inside (like a vending machine's front panel).
A heap is
one machine (implementation) that fulfils the PQ contract quickly — not the contract itself.
means
constant work — independent of the number of items .
means
linear work — doubling doubles the work.
means
logarithmic work — doubling adds just one extra step.
The formal meaning of is
there exist a constant and a size such that for all .
answers the question
"how many times can I halve before reaching 1?" (equivalently, ).
A "stable" PQ would
return equal-priority items in arrival order; standard PQs do not promise this.
To force FIFO tie-breaking, store
the pair with a rising sequence counter, comparing priority first then arrival.

Connections

  • Parent topic (Hinglish) — the concept this page prepares you for.
  • Queue (FIFO) — the FIFO order-out rule.
  • Stack (LIFO) — the LIFO order-out rule.
  • Abstract Data Type — why a PQ is a contract, not a machine.
  • Heap — the fast machine that fulfils the contract (next page).
  • Binary Tree — the shape a heap secretly is.
  • Dijkstra's Algorithm — the algorithm that needs both fast insert and fast extract.
  • Huffman Coding — repeatedly extracts the two minimums.