AlgoViz

Heap properties

easy 16 min

A binary heap is a complete binary tree in which every parent is greater than or equal to its children (max-heap) or less than or equal to them (min-heap).

Where it shows up

The same pattern appears across everyday software.

  • Priority queues in standard libraries

    Python's heapq, Java's PriorityQueue and C++'s std::priority_queue are binary heaps stored in an array. The heap properties are the whole contract: the next item is at index 0.

  • Shortest paths and maps

    Dijkstra's algorithm and A* keep the frontier in a min-heap keyed by distance, so the closest unsettled node is always one pop away. Routing engines and game path-finding rely on this.

  • Schedulers and timers

    Job schedulers and timer systems keep pending tasks in a heap by priority or deadline, so finding the next task to run is O(1) and adding one is O(log n). Go's runtime, for example, keeps its timers in a heap.

  • Event-driven simulation

    Simulations of networks, queues or physics process events in time order. A min-heap keyed by timestamp always yields the earliest pending event, even as new events are scheduled out of order.

  • Top-k and streaming statistics

    Leaderboards, trending lists and monitoring dashboards keep a small heap of size k to track the top values of a stream in O(log k) per item, and two heaps to track a running median.

  • Merging sorted data

    Databases and external sorts merge many sorted runs with a min-heap holding the head of each run, pulling the overall smallest next value in O(log k).