AlgoViz

Heap operations

medium 18 min

A binary heap supports insert (add at the end, sift up), extract (move the last value to the root, sift down) and peek (read the root).

Where it shows up

The same pattern appears across everyday software.

  • Operating system schedulers

    Run queues and timer queues pick the next task or the earliest expiring timer with extract-min and add new ones with insert; Linux keeps its high-resolution timers in a tree for the same reason.

  • Shortest paths and maps

    Dijkstra's algorithm, used in routing and navigation, repeatedly extracts the closest unvisited node and calls decrease-key (or pushes a new entry) when it finds a shorter route.

  • Event-driven simulation

    Network and physics simulators keep future events in a min-heap ordered by timestamp, so the next event is always a peek away.

  • Leaderboards and monitoring

    Showing the top 10 scores or the 5 slowest requests uses a size-k heap over a stream, as in Python's heapq.nlargest.

  • Merging sorted files

    Databases and external sort merge k sorted runs with a heap of their current heads; Python's heapq.merge does the same lazily.

  • Bandwidth and job queues

    Message brokers and network routers serve higher-priority packets or jobs first, using a priority queue built on a heap.