Learning path

Full curriculum

Full curriculum

Unit content

Binary heaps

A binary heap represents a priority queue using a complete binary tree that satisfies a heap-order property.

In a min-heap, every node has priority no greater than its children. The smallest element is therefore at the root.

A complete binary tree can be stored compactly in an array. For a node at index $i$, simple index formulas locate its parent and children without explicit pointers.

Insertion places a new element at the end and moves it upward while heap order is violated. Removing the minimum moves the last element to the root and pushes it downward.

Both operations take

$$O(\log n)$$

time, while reading the minimum is $O(1)$.

The heap property is weaker than full sorting: it guarantees only the parent-child ordering needed for priority-queue operations.