Back to All Algorithms
Min-Heap
Category: Tree / Graph | Time: O(log n) Insert/Delete | Space: O(1)
Visualization
Visual representation of the data structure
1
30
40
20
5
50
15
10
About Min-Heap

A Min-Heap is a specialized tree-based data structure that satisfies the heap property: the value of each node is less than or equal to the value of its children. This means the smallest element is always at the root. It is commonly used to implement priority queues.

Code Editor
The code is for reference. Editing it won't affect the visualization.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
Variable State
{
  "status": "Starting to build Min-Heap..."
}
Min-Heap: How It Works
Last updated October 28, 2026

Overview

A min-heap is a complete binary tree in which every parent is smaller than or equal to its children, so the smallest value is always at the root. 'Complete' means every level is full except possibly the last, which fills from left to right. That shape lets the tree live in a plain array: the children of index i are at 2i + 1 and 2i + 2, and its parent is at floor((i - 1) / 2).

Building a heap from an arbitrary array is done bottom-up: call heapify (sift-down) on every non-leaf node, from the last one back to the root. Surprisingly, this takes only O(n) time, because most nodes are near the bottom and sink only a short distance.

With the default input 10, 20, 15, 30, 40, 5, 50, 1, building the heap pulls 1 up to the root through swaps with 30, 20 and 10, and 5 rises above 15. The visualizer then demonstrates extractMin: it removes 1, moves the last element (30) to the root, and sinks it back down until the heap property holds again.

Step by step

  1. To build: for i from floor(n / 2) - 1 down to 0, call heapify(arr, n, i).
  2. heapify compares a node with its children and swaps it with the smaller child if that child is smaller, then repeats from the child's position.
  3. To extract the minimum: save arr[0], move the last element to index 0, shrink the heap, and heapify from the root.
  4. To insert (not shown): append the value and sift it up while it is smaller than its parent.

Complexity

CaseCostWhy
Build heapO(n)Sum of node heights is linear, even though each heapify can be O(log n).
Get minimumO(1)It is always at index 0.
Extract min / insertO(log n)One path from root to leaf or leaf to root.
SpaceO(n)The array itself; no pointers needed.

When to use it

  • Priority queues: task schedulers, event simulation, and Dijkstra's and Prim's algorithms.
  • Streaming 'top k' problems — keep a min-heap of size k to track the k largest items seen so far.
  • Merging k sorted lists by repeatedly taking the smallest head.

Common mistakes

  • A heap is not sorted: only the root is guaranteed to be the minimum. Searching for an arbitrary value is still O(n).
  • Building a heap by inserting elements one at a time is O(n log n); bottom-up heapify is O(n).
  • Using 1-based child formulas (2i, 2i + 1) on a 0-based array.
Worked Example: Min-Heap Dry Run
Generated from the same trace that drives the visualizer above, using the default input.

A dry run is the best way to check your understanding of Min-Heap: follow each row, predict the next one, and compare. The run below takes 103 steps in total. Steps that only re-print loop counters are left out so the decisions stand out. The Line column refers to the line numbers in the code listing above.

Input
10,20,15,30,40,5,50,1
Result
status = Extraction complete, returned_min = 1, final_heap = [5, 10, 15, 20, 40, 30, 50]
StepLineWhat happens
119status = Starting to build Min-Heap...
22status = Heapifying at index 3, node_value = 30
67condition = 7 < 8 && 1 < 30
810condition = 8 < 8 && undefined < 1
913condition = 7 !== 3
1014action = Swapping 30 and 1
112status = Heapifying at index 7, node_value = 30
157condition = 15 < 8 && undefined < 30
1610condition = 16 < 8 && undefined < 30
1713condition = 7 !== 7
1815action = Recursively heapify on new subtree
192status = Heapifying at index 2, node_value = 15
237condition = 5 < 8 && 5 < 15
2510condition = 6 < 8 && 50 < 5
2613condition = 5 !== 2
2714action = Swapping 15 and 5
282status = Heapifying at index 5, node_value = 15
327condition = 11 < 8 && undefined < 15
3310condition = 12 < 8 && undefined < 15
3413condition = 5 !== 5
3515action = Recursively heapify on new subtree
362status = Heapifying at index 1, node_value = 20
407condition = 3 < 8 && 1 < 20
4210condition = 4 < 8 && 40 < 1
4313condition = 3 !== 1
4414action = Swapping 20 and 1
452status = Heapifying at index 3, node_value = 20
497condition = 7 < 8 && 30 < 20
5010condition = 8 < 8 && undefined < 20
5113condition = 3 !== 3
5215action = Recursively heapify on new subtree
532status = Heapifying at index 0, node_value = 10
577condition = 1 < 8 && 1 < 10
5910condition = 2 < 8 && 5 < 1
6013condition = 1 !== 0
6114action = Swapping 10 and 1
622status = Heapifying at index 1, node_value = 10
667condition = 3 < 8 && 20 < 10
6710condition = 4 < 8 && 40 < 10
6813condition = 1 !== 1

The remaining 22 key steps follow the same pattern. Press play in the visualizer to watch every step through to the result.

Frequently Asked Questions
About Min-Heap

What is a Heap?

A Heap is a specialized tree-based data structure that satisfies the 'heap property'. In a Min-Heap, for any given node, its value is less than or equal to the values of its children. This means the smallest element is always at the root. In a Max-Heap, the root contains the largest element.

How is a Heap represented in memory?

Although it's a tree conceptually, a heap is typically implemented as an array. The parent-child relationship is maintained mathematically: for a node at index `i`, its left child is at `2*i + 1` and its right child is at `2*i + 2`.

What is 'heapify'?

Heapify (or sift-down) is the process of rearranging the nodes to maintain the heap property. When an element is placed at the root that violates the property (e.g., after extracting the min element), heapify 'sinks' the new root down the tree by swapping it with its smallest child until the heap property is restored.

What is the time complexity for heap operations?

Building a heap from an array of n elements can be done in O(n) time. The time complexity for `insert` and `delete` (extract-min/max) operations is O(log n), because the height of a complete binary tree with n nodes is log n.

What are the main applications of a Heap?

Heaps are most famously used to implement Priority Queues, which are essential in many algorithms like Dijkstra's shortest path and Prim's minimum spanning tree. They are also the core of the Heap Sort algorithm.

What is the difference between a Min-Heap and a Max-Heap?

In a Min-Heap, the parent node is always smaller than or equal to its children, so the root is the minimum element. In a Max-Heap, the parent is always larger than or equal to its children, so the root is the maximum element.

How do you insert a new element into a Min-Heap?

To insert a new element, you add it to the end of the array (the bottom-most, right-most position in the tree). Then, you 'bubble up' the new element by repeatedly swapping it with its parent as long as it is smaller than its parent, until the heap property is restored.

Is a Heap a Binary Search Tree?

No. A BST has a strict ordering property (left < root < right). A heap only requires that parents are smaller (or larger) than their children; there is no required ordering between the left and right children.

Where can I learn more?

You can find a more detailed explanation on GeeksforGeeks.