Chapter 9 · Trees

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

100507530406070

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.

100
[0]
50
[1]
75
[2]
30
[3]
40
[4]
60
[5]
70
[6]

parent(i)   → floor((i - 1) / 2)leftChild(i) → 2*i + 1rightChild(i) → 2*i + 2

💡No pointers — pure arithmetic
Because heaps are complete binary trees, there are never any gaps in the array. Navigation between parent and children is pure integer arithmetic — no pointer allocation or cache misses. This makes heaps extremely cache-friendly.

Insert operation

1 / 3
1

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.

1 of 3

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 = 6
1531087
root (min)bubble-up pathextracted min
arr:
1
[0]
5
[1]
3
[2]
10
[3]
8
[4]
7
[5]

Core operations pseudocode

max-heap.pseudo
1insert(heap, value):
2 heap.append(value) // add at end
3 i <- heap.size - 1
4 siftUp(heap, i)
5 
6siftUp(heap, i): // bubble up
7 while i > 0:
8 parent <- floor((i - 1) / 2)
9 if heap[i] > heap[parent]: // max-heap condition
10 swap(heap[i], heap[parent])
11 i <- parent
12 else: break
13 
14extractMax(heap):
15 max <- heap[0] // root is always maximum
16 heap[0] <- heap[last] // move last to root
17 heap.removeLast()
18 siftDown(heap, 0) // restore heap property
19 return max
20 
21siftDown(heap, i): // bubble down
22 largest <- i
23 left <- 2*i + 1
24 right <- 2*i + 2
25 if left < size and heap[left] > heap[largest]: largest <- left
26 if right < size and heap[right] > heap[largest]: largest <- right
27 if largest != i:
28 swap(heap[i], heap[largest])
29 siftDown(heap, largest)

Time complexity

peek max/min — always at root (index 0)O(1)

Constant time

insert — sift up at most log n levelsO(log n)

Logarithmic time

extract max/min — sift down at most log n levelsO(log n)

Logarithmic time

build heap from unsorted array (Floyd's algorithm)O(n)

Linear time

heap sort — n extractions each O(log n)O(n log n)

Linearithmic time

🎯 Quick Check

In a max-heap stored as an array, where is the maximum element always located?

🧠Heap is not a sorted array
A common mistake: confusing a heap with a sorted array. In a max-heap, index 0 is the maximum, but the rest are only partially ordered. You can NOT binary search a heap. Its only guarantee is parent >= children everywhere.

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.