Heaps & Priority Queues

Always grab the best one, cheaply

A priority queue answers one question fast: what's the smallest (or largest) item right now? You keep pushing items in any order, and each pop hands you the current min (or max).

The structure underneath is a heap β€” a tree kept partly ordered so the min (or max) is always at the root. Push and pop are O(log n); peeking at the min is O(1).

Real uses: dispatch the nearest available walker first; run the highest-priority job before the rest.