Data Structures19 sections · 729 units
Open in Course

Extract Operation

Bubble down

To extract the minimum (in a min-heap):

1.1. Save the root value.

2.2. Move the last element to the root.

3.3. "Bubble down": while larger than a child, swap with the smaller child.

4.4. Return the saved value.

function extractMin(heap)
    minVal := heap[0]
    heap[0] := last element of heap
    remove last element
    bubbleDown(heap, 0)
    return minVal

Bubbling down takes at most O(logn)O(\log n) swaps.