Min-Heap
Data StructuresavgO(log n)worstO(log n)spaceO(n)
8
15
10
21
30
14
A min-heap of 6; the minimum 8 sits at the root
1 / 47
Starting contents6 values
Operations6 / 40
- 1. insert(3)
- 2. insert(12)
- 3. pop()
- 4. insert(1)
- 5. pop()
- 6. pop()
How it works
A complete binary tree stored in an array where every parent is ≤ its children, so the minimum is always at the root. Insert sifts the new value up; extract-min moves the last value to the root and sifts it down. Most priority queues, including the one inside Dijkstra's algorithm, are built this way.
Complexity
- Best
- O(1)
- Average
- O(log n)
- Worst
- O(log n)
- Space
- O(n)