Data Structures · Visualization

Min-Heap: Introduction

Min Heap

A min-heap is a complete binary tree where every parent's value is less than or equal to both of its children's values — the mirror image of a max-heap. Time: O(1) to peek the min, O(log n) to insert or remove the min. Space: O(n).

Big-O: Insert/extract O(log n); build O(n).

Min-Heap: Introduction

Min Heap

A min-heap is a complete binary tree where every parent's value is less than or equal to both of its children's values — the mirror image of a max-heap. This guarantees the smallest value in the whole structure always sits at the root. Like a max-heap, it is typically stored as a plain array, using index math to find any node's parent and children.

How Min-Heap: Introduction Works

  1. Store elements in an array where index 0 is the root.
  2. For any node at index i, its children live at indices 2i+1 and 2i+2, and its parent lives at index (i-1)/2.
  3. Maintain the rule that every node's value is <= both of its children's values.
  4. Use this shape to always find the minimum instantly at index 0.

Min-Heap: Introduction Worked Example

Input: A valid min-heap stored as an array

  1. Array: [10, 20, 15, 30, 25]
  2. Root (index 0) = 10, its children are index 1 (20) and index 2 (15) — 10 <= both
  3. Node 20 (index 1) has child index 3 (30) — 20 <= 30

Output: 10 / \ 20 15 / \ 30 25

📖 Introduction: this is an overview of the topic, so there's nothing to run here. Pick an operation from the side menu to see a live, step-by-step animation.
Introduction
A complete binary tree stored in an array, where every parent is bigger than its children — so the largest value always sits at the root.
90
0
60
1
70
2
20
3
40
4
55
5
66
6
10
7
Bubbling / sifting
Max-Heap — Introduction
Press ▶ Run to watch it step by step.
Python · Heaps in Code
# Max-heap inside a plain list
# children of i: 2i+1, 2i+2; parent: (i-1)//2
heap = [10, 20, 15, 30, 25]

# root heap[0] is always the maximum
print(heap[0])
# example: intro on [10, 20, 15, 30, 25]

Min-Heap: Introduction Time & Space Complexity

TimeO(1) to peek the min, O(log n) to insert or remove the min
SpaceO(n)

When to Use Min-Heap: Introduction

  • Implementing priority queues where the lowest value/cost is served first
  • Dijkstra's shortest path algorithm (always expanding the closest node)
  • Finding the k smallest elements efficiently
  • Event simulation systems that always process the earliest event next

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Ready for the next step?

See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.