← Back to the heaps page
Binary Min / Max Heap
A complete binary tree in an array: parent beats children. The root is always the extreme — everything else is only loosely ordered.
What to know
- Array layout: children of i at 2i+1, 2i+2; parent at (i−1)/2. No pointers.
- Sift-up on insert, sift-down on extract; both walk one root-to-leaf path.
- Building from n items with sift-down is O(n), not O(n log n).
- A max-heap is a min-heap on negated values.
In the wild: Every language’s priority queue, OS schedulers, event loops, timer wheels.
Algorithms to reach for
Heapify (bottom-up build)
O(n)Turn an array into a heap in linear time
Insert / extract root
O(log n)Priority queue primitives
Heapsort
O(n log n)Sort by repeated extraction
Practice problems — with full guides
Each problem has its own page: progressive hints, how to approach it, and the full solution.