Sitemap

Heap vs Priority Queues vs Queues

2 min readJan 12, 2019

You must have come across these data structures- Heap/Queues/Priority Queues. Why do we need Priority Queues when we have Queues. And also heaps are referred to as priority queues, then are they same? What's the difference between queues/Priority Queues and Heaps/Priority Queues?
Let's discuss these.

Queues:

These are very easy to understand. Like the queues in real, the first entry into the queue is the first one to be taken out i.e. FIFO- first In first Out.
e.g. if we entered these numbers in the following order: 6 → 3 → 8 → 2 into the queue. Then while popping the values from it: the order of their pop will be: 6 → 3 → 8 → 2, i.e. the same order as of insertion. 6 was entered first, so it will be taken out before the rest.

Priority Queue:

As the name suggests, it is a queue but has something to do with priority.
While taking out the values from PQ, the entry with the highest priority is removed first(not following the FIFO order as in normal Queues).
Take, for example, the insertion order:
A(P= 4) → B(P=3)→ C(P=5) → D(p=9)

So while taking out from PQ, the removal order will be:
D(p=9) → C(P=5) → A(P= 4) → B(P=3)

--

--

Anmol Sehgal
Anmol Sehgal

Written by Anmol Sehgal

Senior Software Developer @ Amazon | ex Oracle OCI, Goldman Sachs, Expedia | Writing real-world lessons on Java, system design & production-scale architecture.

Responses (1)