Write pseudocode for the procedures \(\textsc{Heap-Minimum}\), \(\textsc{Heap-Extract-Min}\), \(\textsc{Heap-Decrease-Key}\), and \(\textsc{Min-Heap-Insert}\) that implement a min-priority queue with a min-heap.
A min-priority queue is the mirror image of a max-priority queue. Instead of maintaining the largest element at the root, we maintain the smallest. This is useful for applications like event-driven simulation (where we process events in chronological order) or Dijkstra’s shortest-path algorithm.
The key insight is that min-priority queue operations are analogous to max-priority queue operations, with all comparisons reversed. Where max-heaps use “greater than” comparisons, min-heaps use “less than” comparisons.
Heap-Minimum
The \(\textsc{Heap-Minimum}\) procedure simply returns the root element, which is the minimum in a min-heap. This takes \(\Theta(1)\) time.
Heap-Extract-Minimum
The \(\textsc{Heap-Extract-Min}\) procedure removes and returns the minimum element. It replaces the root with the last element, decreases the heap size, and calls \(\textsc{Min-Heapify}\) to restore the min-heap property. The running time is \(O(\lg n)\) due to the \(\textsc{Min-Heapify}\) call.
Heap-Decrease-Key
The \(\textsc{Heap-Decrease-Key}\) procedure decreases the key of element \(A[i]\) to the new value. After setting the new key, it bubbles the element up toward the root by repeatedly swapping with its parent whenever the parent is larger (violating the min-heap property). The running time is \(O(\lg n)\) since the path to the root has length at most the height of the tree.
Min-Heap-Insert
The \(\textsc{Min-Heap-Insert}\) procedure adds a new element to the min-heap. It first expands the heap and inserts a sentinel value \(\infty\) (larger than any actual key) at the new position. Then it calls \(\textsc{Heap-Decrease-Key}\) to decrease this value to the desired key, which bubbles the new element to its correct position. The running time is \(O(\lg n)\).