How to use Priority Queue in Python
The Priority Queue is implemented with binary heap. To understand how it’s implemented, refer to this post. Python provides a built-in implementation of the Priority Queue data structure.
For insertion, it uses the put function. The get function dequeues the highest priority item from the queue.
To use the Priority Queue class object:
1 | from queue import PriorityQueue |
To insert a tuple pair which has a priority associated with it:
1 | q.put((2, "Joe")) |
To simply insert values as priority:
1 | q.put(12) |