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 HeapA 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
- Store elements in an array where index 0 is the root.
- 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.
- Maintain the rule that every node's value is <= both of its children's values.
- 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
- Array: [10, 20, 15, 30, 25]
- Root (index 0) = 10, its children are index 1 (20) and index 2 (15) — 10 <= both
- Node 20 (index 1) has child index 3 (30) — 20 <= 30
Output: 10 / \ 20 15 / \ 30 25
Press ▶ Run to watch it step by step.
# 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
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
Ready for the next step?
See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.