Heaps
A heap is a complete binary tree that enforces one rule: every parent is always greater (max-heap) or smaller (min-heap) than its children. This makes finding the minimum or maximum an O(1) operation — always.
What is a heap data structure?
A heap is a complete binary tree where every parent is smaller than its children (min-heap) or larger (max-heap), so the minimum or maximum is always at the root. It's stored compactly in an array: the children of index i are at 2i+1 and 2i+2. Peeking at the top is O(1), and inserting or removing the top is O(log n). Heaps implement priority queues and heap sort.
// array view of a min-heap: 1
// [1, 3, 2, 7, 4] / \
// 3 2
// / \
// 7 4
const parent = (i) => (i - 1) >> 1;
const left = (i) => 2 * i + 1, right = (i) => 2 * i + 2;Max-heap structure
Max-heap: every parent is greater than both its children
Every parent is greater than both children. 100 > 50, 75. 50 > 30, 40. 75 > 60, 70. Heap property: satisfied everywhere.
The array trick
A heap is stored as a flat array — no pointers needed. The parent-child relationship is computed arithmetically by index.
parent(i) → floor((i - 1) / 2)leftChild(i) → 2*i + 1rightChild(i) → 2*i + 2
Insert operation
Step 1: Append to end
Insert the new element at the last position in the array (bottom-left of the tree). The heap shape property (complete binary tree) is preserved.
Live demo — min-heap
Insert values and watch heapify-up restore the heap property. Extract-min removes the root and heapify-down sifts the replacement into place.
Min-Heap — parent ≤ children always
min = 1 · size = 6Core operations pseudocode
1insert(heap, value):2 heap.append(value) // add at end3 i <- heap.size - 14 siftUp(heap, i)56siftUp(heap, i): // bubble up7 while i > 0:8 parent <- floor((i - 1) / 2)9 if heap[i] > heap[parent]: // max-heap condition10 swap(heap[i], heap[parent])11 i <- parent12 else: break1314extractMax(heap):15 max <- heap[0] // root is always maximum16 heap[0] <- heap[last] // move last to root17 heap.removeLast()18 siftDown(heap, 0) // restore heap property19 return max2021siftDown(heap, i): // bubble down22 largest <- i23 left <- 2*i + 124 right <- 2*i + 225 if left < size and heap[left] > heap[largest]: largest <- left26 if right < size and heap[right] > heap[largest]: largest <- right27 if largest != i:28 swap(heap[i], heap[largest])29 siftDown(heap, largest)
Time complexity
Constant time
Logarithmic time
Logarithmic time
Linear time
Linearithmic time
🎯 Quick Check
In a max-heap stored as an array, where is the maximum element always located?
Heaps: summary
Key points
- Insert: add at the end, then sift up. Remove top: move the last item to the root, then sift down.
- A heap is not sorted: only the parent–child relationship is ordered.
- Build a heap from n items in O(n) with heapify.
Frequently asked questions
›Heap vs binary search tree?
A heap only guarantees fast access to the minimum or maximum. A BST keeps everything ordered, so it supports fast search for any value and sorted traversal.