Home | Projects | Notes > Data Structures & Algorithms > Priority Queues
A priority queue is an abstract data structure where each element has a priority, and elements are served based on their priority, not their insertion order.
By default, the highest priority (usually the largest number) is dequeued first.
Internally, it’s typically implemented using a binary heap for efficient performance.

Efficient operations: push, pop, and top all run in O(log n) time with a binary heap.
Dynamic ordering: Automatically keeps the highest (or lowest) priority item at the top.
Flexible: Can be implemented as min-heap or max-heap, or customized using comparators.
Widely used: Great for scheduling, graph algorithms (e.g., Dijkstra’s), load balancing, etc.
Limited access: You can only access or remove the top-priority element — no direct access to arbitrary items.
Not suitable for sorted traversal: Unlike balanced BSTs, you can’t iterate in order.
Re-prioritizing an item (i.e., decreasing key) is not efficient unless custom logic is added.
Task scheduling
Shortest path algorithms
Simulations or real-time systems
Event-driven systems
minpq.hpp)26123
45
6class minpq7{8public:9 void push(const int val);10 void pop();11 int top() const;12 bool empty() const;13 int size() const;14 void print() const;15
16private:17 std::vector<int> heap; 18
19 int parent(const int i) const;20 int left(const int i) const;21 int right(const int i) const;22 void heapify_up(int idx);23 void heapify_down(int idx);24};25
26minpq.cpp)1041234
5void minpq::push(const int val)6{7 heap.push_back(val);8 heapify_up(heap.size() - 1);9}10
11void minpq::pop()12{13 if (heap.empty())14 {15 throw std::underflow_error("MinHeap is empty");16 }17
18 heap[0] = heap.back();19 heap.pop_back();20
21 if (!heap.empty())22 {23 heapify_down(0);24 }25}26
27int minpq::top() const28{29 if (heap.empty())30 {31 throw std::underflow_error("MinHeap is empty");32 }33
34 return heap[0];35}36
37bool minpq::empty() const38{39 return heap.empty();40}41
42int minpq::size() const43{44 return heap.size();45}46
47void minpq::print() const48{49 for (int val : heap)50 {51 std::cout << val << " ";52 }53
54 std::cout << std::endl;55}56
57// private functions58
59int minpq::parent(const int i) const60{61 return (i - 1) / 2;62}63
64int minpq::left(const int i) const65{66 return 2 * i + 1;67}68
69int minpq::right(const int i) const70{71 return 2 * i + 2;72}73
74void minpq::heapify_up(int idx)75{76 while (idx > 0 && heap[parent(idx)] > heap[idx])77 {78 std::swap(heap[parent(idx)], heap[idx]);79 idx = parent(idx);80 }81}82
83void minpq::heapify_down(int idx)84{85 int smallest = idx;86 int l = left(idx);87 int r = right(idx);88
89 if (l < heap.size() && heap[l] < heap[smallest])90 {91 smallest = l;92 }93
94 if (r < heap.size() && heap[r] < heap[smallest])95 {96 smallest = r;97 }98
99 if (smallest != idx)100 {101 std::swap(heap[idx], heap[smallest]);102 heapify_down(smallest);103 }104}32123
4int main(int argc, char *argv[])5{6 minpq pq;7
8 pq.push(30);9 pq.push(10);10 pq.push(40);11 pq.push(50);12 pq.push(20);13 pq.push(60);14
15 std::cout << pq.top() << std::endl; // 1016
17 pq.pop();18
19 std::cout << pq.top() << std::endl; // 2020
21 pq.print(); // 20 30 40 50 6022
23 pq.pop();24
25 pq.print(); // 30 50 40 6026
27 std::cout << pq.empty() << std::endl; // 028 std::cout << pq.size() << std::endl; // 429
30 return 0;31}32
6110220320 30 40 50 60430 50 40 605064