Remove the minimum value, move the last item to the root, and sift downward.

Algorithm

Steps

  1. Store the heap in an array.
  2. Compare parent and child indexes instead of building explicit tree nodes.
  3. Swap only when the heap order is violated.
  4. Print the deterministic final heap state for replay comparison.

Complexity

  • Time: O(log n)
  • Space: O(1) extra
sift down After removing the root, the last value moves to the root and swaps with the smaller child until order is restored.

Visual walkthrough

R DSA Implementation

basic.R
list_string <- function(values) paste0("[", paste(values, collapse = ", "), "]")
heap_insert <- function(heap, value) {
  heap <- c(heap, value)
  child <- length(heap)
  while (child > 1) {
    parent <- floor(child / 2)
    if (heap[parent] <= heap[child]) break
    tmp <- heap[parent]; heap[parent] <- heap[child]; heap[child] <- tmp
    child <- parent
  }
  heap
}
heap_pop <- function(heap) {
  smallest <- heap[1]
  heap[1] <- heap[length(heap)]
  heap <- heap[-length(heap)]
  parent <- 1
  while (TRUE) {
    left <- parent * 2
    right <- left + 1
    if (left > length(heap)) break
    child <- left
    if (right <= length(heap) && heap[right] < heap[left]) child <- right
    if (heap[parent] <= heap[child]) break
    tmp <- heap[parent]; heap[parent] <- heap[child]; heap[child] <- tmp
    parent <- child
  }
  list(value = smallest, heap = heap)
}
heap <- c(1, 4, 2, 9, 6, 7)
result <- heap_pop(heap)
cat(result$value, " -> ", list_string(result$heap), "\n", sep = "")

After popping the minimum, the last value moves to the root and sifts down by swapping with the smaller child.

Step 1 - Replace root

The saved minimum is 1; the last value 7 moves to the root before sifting down.

Replacement state [7, 4, 2, 9, 6] with removed value 1.1removed7root4left2smaller9i36i4

Step 2 - Swap with the smaller child

7 swaps with 2, producing the final heap [2, 4, 7, 9, 6].

Final heap after pop and one sift-down swap.2root4i17i29i36i4

Implementation notes

  • heap <- c(1, 4, 2, 9, 6, 7) is the starting min-heap vector. This lesson uses R's 1-based indexes.
  • The output path calls result <- heap_pop(heap). heap_insert() is present in the shared file, but it is not used for this lesson's printed result.
  • heap_pop() stores smallest <- heap[1], moves the last value to the root with heap[1] <- heap[length(heap)], then removes the old last slot with heap <- heap[-length(heap)].
  • The trace shows that as popped value 1 and heap [7, 4, 2, 9, 6].
  • Sift-down starts at parent <- 1. left <- parent * 2 and right <- left + 1 are the 1-based child indexes.
  • The code starts with child <- left, then switches to the right child when right <= length(heap) && heap[right] < heap[left].
  • At the root, 7 has left child 4 at index 2 and right child 2 at index 3, so the smaller child is 2.
  • if (heap[parent] <= heap[child]) break stops when min-heap order is valid; otherwise the tmp line swaps parent and child, then parent <- child.

Replay steps

start:       [1, 4, 2, 9, 6, 7]
pop root:    1, move 7 to root -> [7, 4, 2, 9, 6]
swap 7/2:    [2, 4, 7, 9, 6]
  • heap_pop() returns list(value = smallest, heap = heap), so the output reads result$value and result$heap.
  • cat(result$value, " -> ", list_string(result$heap), "\n", sep = "") prints exactly 1 -> [2, 4, 7, 9, 6].

Output

1 -> [2, 4, 7, 9, 6]