Skip to content
IT glossary
AlgorithmsUpdated December 3, 2026

Heap (data structure)

Куча (структура данных)

A data structure that gives quick access to the smallest or largest item, even as items are added and removed. It is useful when you need to pick the next item by priority rather than by arrival time.

Also known as: пирамида, priority queue

What a heap is

Suppose a scheduler needs to run whichever job is due next. It could keep all jobs sorted, but it only needs to know which deadline comes first. A min-heap keeps the earliest deadline at the root, where the scheduler can read it immediately. A max-heap does the reverse: its root holds the largest key.

A heap is not the same thing as a priority queue. A priority queue describes the behavior you want: add items and retrieve the one that comes first by priority. A heap is one data structure you can use to provide that behavior.

How a binary heap works

A binary heap is a complete binary tree: every level is full except possibly the last, which fills from left to right. In a min-heap, each parent's key is no greater than either child's key. That makes the root a global minimum, even though siblings and separate branches need not be in order. A max-heap reverses the comparison.

Because there are no gaps in the tree's level order, an array can represent it without pointers. With zero-based indexing, the children of index i are at 2*i + 1 and 2*i + 2 when those positions exist. For i > 0, the parent is at (i - 1) // 2, using integer division.

  • Peek: Read index 0 in O(1) time.
  • Push: Append an item and sift it up, swapping with its parent while the heap rule is broken. This takes at most O(log n) sift steps; a dynamic array may occasionally need to resize.
  • Pop: Save the root, move the last item to the root, and sift it down if the heap is still nonempty. In a min-heap, swap with the smaller child whenever a swap is needed. This takes O(log n) time in the worst case.

If the items are already in an array, build the heap bottom up by sifting down each internal node, starting with the last one. This takes O(n) time because most nodes are close to the leaves and can move only a short distance.

Example: pull the next deadline

Python's heapq treats a list as a min-heap. Here the integers are minutes until jobs are due:

from heapq import heapify, heappop, heappush

deadlines = [20, 5, 12]
heapify(deadlines)
heappush(deadlines, 3)
next_deadline = heappop(deadlines)  # 3
print(deadlines[0])                 # 5

Reading index 0 leaves the item in the heap. Calling heappop removes it and restores the heap rule for the remaining items.

Uses and interview pitfalls

Heaps are useful in schedulers, in Dijkstra's algorithm, and when merging sorted streams: store one candidate from each stream and refill from whichever stream supplied the last item. A common interview problem asks for the k largest items in a stream. Keep a min-heap of size k so its root tells you which of the current winners to evict.

Don't iterate over the backing array and expect sorted output. Don't search it like a binary search tree either: finding an arbitrary value can take O(n) time. If equal-priority jobs must come out in arrival order, add an insertion counter as a tie-breaker. Finally, the heap used for dynamic memory allocation is unrelated to this data structure.