What is a Priority Queue?
A priority queue throws out the "first come, first served" rule that a normal queue follows. Every element carries a priority, and whichever element has the most urgent priority gets dequeued next; it doesn't matter how long it's been sitting there.
Key Characteristics
Priority queues have these fundamental properties:
- Priority-based ordering:
- Elements are processed by priority (highest first or lowest first)
- Two core operations:
- insert(item, priority) - Add with priority
- extractMax()/extractMin() - Remove highest/lowest priority item
- Peek operation:
- View highest/lowest priority item without removal
- No FIFO guarantee:
- Equal priority elements may be processed in arbitrary order
How Does It Work?
A priority queue does not keep its elements fully sorted — that would be far more work than the job needs. It only maintains one weaker rule: every parent outranks its children. That is a binary heap, and it is enough to guarantee the most urgent item is always at the root. Below, the tree and the array underneath it are the same six values — a node at index i keeps its children at 2i + 1 and 2i + 2:
- A max-heap holding priorities 9, 7, 8, 3, 5. Every parent outranks its children, so the most urgent item is always at the root.
- insert(10): the new item is appended to the end of the array — index 5, whose parent is index 2 holding 8. 10 outranks 8, so the heap rule is broken.
- Swap them. 10 is now at index 2, and its new parent is the root, 9 — still out of order, so it keeps climbing.
- Swap again and 10 reaches the root. Two swaps for six elements — the climb is bounded by the height of the tree, not its size.
- peek(): read the root. It is the highest priority by construction, so no searching is needed — this is the O(1) operation.
- extractMax(): 10 is returned and the last element, 8, is moved into the empty root. Now the root is too small, so it has to sink instead.
- The larger child, 9, is promoted. The heap rule holds again and the queue is ready to serve the next highest priority.
Both repair routines — climbing after an insert, sinking after an extract — only ever move along one path from root to leaf. That path is the height of the tree, so doubling the number of items adds just one extra step.
Time Complexity
For the usual binary heap implementation:
- insert(): O(log n)
- extractMax()/extractMin(): O(log n)
- peek(): O(1)
- isEmpty(): O(1)
This is the one queue type whose curve is not flat. The lower line is peek, which just reads the root. The upper line is insert and extract, which walk the height of the tree — still shallow, since 1,000 items are only about 10 levels deep:
Implementation Variations
Common implementation approaches:
- Binary Heap:
- Most common implementation
- O(log n) insert and extract
- O(1) peek
- Memory efficient
- Balanced Binary Search Tree:
- O(log n) all operations
- Supports more operations (like delete-by-value)
- Higher memory overhead
- Array (Unsorted):
- O(1) insert, O(n) extract
- Simple but inefficient for large datasets
- Fibonacci Heap:
- Amortized O(1) insert
- O(log n) extract
- Complex implementation
Applications
Priority queues are used in:
- Dijkstra's Algorithm: Finding shortest paths in graphs
- Huffman Coding: Data compression
- Operating Systems: Process scheduling
- Event-driven Simulation: Processing events in time order
- A* Search: Pathfinding in AI
- Bandwidth Management: Prioritizing network packets
Special Cases
Interesting priority queue variations:
- Min-Priority Queue: Extracts minimum priority first
- Max-Priority Queue: Extracts maximum priority first
- Double-Ended Priority Queue: Supports both min and max extraction
- Indexed Priority Queue: Allows priority updates by key
- Bounded Priority Queue: Fixed capacity with eviction policies
What makes it so useful is that it always hands you the most important item on demand, which is exactly what a lot of algorithms need. Under the hood it's usually built on a heap, though a balanced BST works too, and which one you pick depends on how the application balances insertion speed against extraction speed.