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

Java DSA Implementation

Basic.java
import java.util.*;
class Basic {
    static String listString(List<Integer> values) { return values.toString(); }
    static void heapInsert(List<Integer> heap, int value) {
        heap.add(value);
        int child = heap.size() - 1;
        while (child > 0) {
            int parent = (child - 1) / 2;
            if (heap.get(parent) <= heap.get(child)) break;
            Collections.swap(heap, parent, child);
            child = parent;
        }
    }
    static int heapPop(List<Integer> heap) {
        int smallest = heap.get(0);
        heap.set(0, heap.remove(heap.size() - 1));
        int parent = 0;
        while (true) {
            int left = parent * 2 + 1;
            int right = left + 1;
            if (left >= heap.size()) break;
            int child = left;
            if (right < heap.size() && heap.get(right) < heap.get(left)) child = right;
            if (heap.get(parent) <= heap.get(child)) break;
            Collections.swap(heap, parent, child);
            parent = child;
        }
        return smallest;
    }
    public static void main(String[] args) { List<Integer> heap = new ArrayList<>(Arrays.asList(1, 4, 2, 9, 6, 7)); int popped = heapPop(heap); System.out.println(popped + " -> " + listString(heap)); }
}

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

Output

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

Implementation notes

  • Java stores the heap as a List<Integer> backed by an ArrayList, seeded with new ArrayList<>(Arrays.asList(1, 4, 2, 9, 6, 7)). Heap entries are boxed through Integer.valueOf, so these small fixture values may be cached Integer instances rather than primitive int array slots.
  • heapPop saves the root with int smallest = heap.get(0), unboxing the root Integer, then replaces index 0 using heap.set(0, heap.remove(heap.size() - 1)). The remove call deletes the last list element and returns it for the root replacement.
  • This is a min-heap sift-down: child indexes are left = parent * 2 + 1 and right = left + 1; the loop stops when left >= heap.size() or heap.get(parent) <= heap.get(child), which unboxes the two Integer values for primitive comparison.
  • When both children exist, right < heap.size() && heap.get(right) < heap.get(left) also unboxes for comparison, chooses the smaller child, and Collections.swap(heap, parent, child) mutates the same list in place.
  • The replay-visible states show popped value 1, root replacement [7, 4, 2, 9, 6], and final heap [2, 4, 7, 9, 6]. Allocation is the ArrayList storage and any uncached boxed integers, all managed by JVM GC.