# Priority Queue (PQ)

A priority queue is just a list of Queues sorted by priority. Depending on how the priority queue is implemented, you have different time complexities.

You can implement it using Array, Linked List, Vector, but the most natural choice is a Heap (Data Structure).

Links

### C++

By Default, the C++ priority queue is a Max Heap and uses C++ Vector (see reference).

If we want to create a priority queue that supports finding and removing the smallest element, we can do it as follows:

Priority queues **allow** for duplicate values to be stored.

You can also implement a Priority Queue using a Heap.

For a priority queue of key-pairs, you can us the following:

NOTE: This is still different from an Ordered Map.

Why do we implement a priority queue with a heap?

We get insertion time and deletion time on average of $O(1)$, unlike a BST which is $O(gn)$. So then why arenâ€™t heaps used more often? Well, like in an Ordered Map, we need the keys to be ordered, and access every key in an ordered fashion.

Priority queues **allow** for duplicate values to be stored.