01What a priority queue needs
A priority queue stores items with priorities and supports three operations: insert an item, peek at the smallest (or largest) one, and extract it. The obvious structures each fail somewhere:
- a sorted array: cheap peek, but insertion shifts elements, O(n);
- an unsorted array: O(1) insertion, but finding the minimum is O(n);
- a balanced BST: O(log n) for everything, but heavy machinery for a job that only ever looks at one end.
The binary heap is the lightweight answer: peek in O(1), insert and extract in O(log n), no pointers, just an array. It powers std::priority_queue, Python’s heapq and Java’s PriorityQueue, and through them Dijkstra’s and Prim’s algorithms, event simulations, “k largest” queries and heapsort.