Definition
Min-Heap
A min-heap is a heap that satisfies the min-heap property: for every node
other than the root, the value of the parent is at most the value of the node itself: Consequently, the smallest element in a min-heap is always stored at the root, and the values in any path from the root to a leaf are in non-decreasing order.
Operations
Min-Heapify
The
Extract Min
To extract the minimum element:
- Copy the root value (the minimum).
- Replace the root with the last element in the array.
- Decrease the heap size.
- Call
on the root to restore the heap property.
Insert
To insert a new element
- Add
at the end of the heap. - “Bubble up” (or
): compare with its parent and swap if is smaller, repeating until the min-heap property is satisfied.
Complexity
The time complexity for min-heap operations is identical to max-heaps:
- Min-Heapify:
- Insert:
- Extract-Min:
- Build-Min-Heap: