Beginner Topic 9 of 52 Data Structures
Priority Queue
Collection always yielding the element with highest priority first.
How it works
A priority queue always removes the element with the highest priority, or the lowest key in a min-queue, regardless of insertion order. It is usually implemented with a binary heap giving O(log n) insert and extract and O(1) peek. Ties can be broken by insertion order if stability matters, and decrease-key operations are supported by some implementations.
Where it is used
Dijkstra's shortest-path algorithm, A* search, event-driven simulation, operating-system job scheduling, Huffman coding and finding the k largest items in a stream use priority queues.
Common pitfall
A priority queue is not fully sorted, so iterating it does not give sorted order. Mutating an item's priority in place without updating the heap breaks it. Comparison functions must be consistent, and equal priorities are not served in FIFO order unless you add a tiebreaker.
Where it fits in the course
Priority Queue is topic 9 of 52 in Data Structures, rated Beginner. It follows Deque. The next topic is Hash Tables.
Practice and explore
- Open the interactive lesson, with a mini quiz.
- Take the Data Structures quiz.
- Explore the Stack, Queue & Linked List visualizer: Push, pop, enqueue and dequeue interactively.
How to study this topic
- Read the explanation above and restate the definition in your own words.
- Open the interactive lesson and answer its mini quiz.
- Read the common pitfall, then write a short example that avoids it.
- Check your understanding in the Data Structures quiz, and revisit the topic if you miss a question.
Frequently asked questions
What is Priority Queue?
Where do people use Priority Queue?
What is the most common mistake with Priority Queue?
What should I learn before this topic?
How can I practice it?
Is Priority Queue hard to learn?
How long does it take to learn Priority Queue?
Which topics are related to Priority Queue?
What should I study after Priority Queue?
Related topics
- Deque (Data Structures)
- Hash Tables (Data Structures)
- Queues (Data Structures)
- Hash Collisions (Data Structures)
- Priority Scheduling (Operating Systems)
- Priority Inversion (Concurrency & Parallelism)