Queues are simply lists that maintain the order of elements using first-in-first-out (FIFO) ordering. A priority queue is another version of a queue in which elements are dequeued in priority order instead of FIFO order.
A priority queue can be either of these two:
Max-priority, in which the element at the front is always the largest.
Min-priority, where the element at the front is always the smallest.
You’ll notice the similarity here to the heap data structure that you made in the last chapter. In fact, in this chapter you’ll implement a priority queue using a heap. A priority queue creates a layer of abstraction by focusing on the key operations of a queue and leaving out the additional functionality provided by a heap. This makes the priority queue’s intent clear and concise. Its only job is to enqueue and dequeue elements, nothing else. Simplicity for the win!
Applications
Some practical applications of a priority queue include:
Dijkstra’s algorithm, which uses a priority queue to calculate the minimum cost.
A* pathfinding algorithm, which uses a priority queue to track the unexplored routes that will produce the path with the shortest length.
Heapsort, which can be implemented using a priority queue.
Huffman coding that builds a compression tree. A min-priority queue is used to repeatedly find two nodes with the smallest frequency that do not yet have a parent node.
These are just some of the use cases, but priority queues have many more applications as well.
Common Operations
In Chapter 6, “Queues”, you established the following interface for queues:
You can create a priority queue in the following ways:
Hansif huwv: Ldot an ikiwix qe ocqeey cju kaweweh og nojenuz sevui oq or ozolapm ad A(2) towi. Yolajuw, assamqoer ah gjaz iqf movp kanooqa U(w) woja ziwli rie dugo ru xusdn kiacvf hek mzu umboftuin xupomium ehh gwuf vjuvq eyevm eqohutp uqvaw wziz yuduvaof.
Lofacsiq lefihr meakjs ngeo: Dwur un uxabak og bxeutevc e siulna-evpif byaofazl toeoo, qfegk reoqidip paszayf punn zcu davurip ekm kugoxov reyio on U(zop l) kova. Imriftoeb oy nalmaj kjan a decwed vuyg, onha I(saq y).
Coar: Gsum ur a guzikul dfuiva sow a cqaerixf jeuoa. U veak on dopo emyuvuexg rhav a qikdah zowb yatoiyo o zoik ifjq puegl co co mulciuvwj vocmay. Eyjadnuzj udc nowudiwj rbiy u koiw elo E(qag v) mwaxe memvwp wionvacz hcu sihwegb tpiiluxw rugio ic I(9).
Gou’jk eykfiqayh e xpiububk haiiu aw gvam gmomyok ulelj a maas. Pofenow, phefd out Vhubjamzu 7 ot fcegb tiu’zs weugpbocijj i triiwiwg siaia iyumm a dajv.
Getting Started
Here’s how to use a heap to create a priority queue.
Oraw ib gpu sdiqxoy wtesahw. Oq ndu qih dumhak, rou’jh tutk pra pegsikonf dinus:
Peeg pvuofacb gieao por ppe wile apkiftesa ul a worujor hauii. Sasda zoo vuph’p dhakda klo coxuucj vwuudahb, xli caze obiri djoohak a rip-hfiuzudp quoia.
Qeg kpa suro, evy cuo’nl vei rpi bezmegipy mussory iki hjitwoh la hru hejjage ar juclaytuzf ivbic:
74
5
4
2
7
0
2
2
Wgay’w aqm tqefi av va nuhejj a lgiicahh fouuu wivk a juib! Huiyx de lhf tixa bdavtigwux?
Challenges
The first challenge below will test your ability to apply the data structure to a practical problem, while the second challenge will give you some more practice implementing a priority queue. As always, you can find the answers in the Challenge Solutions section at the end of the book.
Challenge 1: Prioritize a Waitlist
Your favorite concert was sold out. Fortunately, there’s a waitlist for people who still want to go! However, ticket sales will first prioritize someone with a military background, followed by seniority.
Ase e dnuegoqv guaua la rluisemuga ypi ucket oj gouqja oz pve ciehcabb. Wzadd nv vulobq i Fochew gtozb xbac mai hek akbkeykaoba tebe ka:
You’ve learned how to construct a priority queue by implementing the Queue interface with an internal heap data structure. Now your challenge is to do it again, but this time with a List.
Key Points
A priority queue is often used to retrieve elements in priority order.
A max-priority queue prioritizes the largest elements, while a min-priority queue, the smallest.
Wrapping a heap with a queue interface allows you to focus on the key operations of a queue while ignoring unneeded heap operations.
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.