heap = [2, 5, 3, 9, 7, 6] # Extract min (2) and bubble down heap[0] = heap[-1] heap.pop() # Bubble down process index = 0 size = len(heap) while True: left = 2 * index + 1 right = 2 * index + 2 smallest = index if left < size and heap[left] < heap[smallest]: smallest = left if right < size and heap[right] < heap[smallest]: smallest = right if smallest == index: break heap[index], heap[smallest] = heap[smallest], heap[index] index = smallest print(heap)
After removing the root (2), we replace it with the last element (6). Then we bubble down:
- Compare 6 with children 5 and 3; 3 is smallest, swap 6 and 3.
- Now 6 is at index 2, children are 9 and 7, no smaller child, stop.
Final heap: [3, 5, 6, 9, 7]
heap = [20, 18, 15, 10, 12, 9, 8] # Extract max (20) and bubble down heap[0] = heap[-1] heap.pop() index = 0 size = len(heap) while True: left = 2 * index + 1 right = 2 * index + 2 largest = index if left < size and heap[left] > heap[largest]: largest = left if right < size and heap[right] > heap[largest]: largest = right if largest == index: break heap[index], heap[largest] = heap[largest], heap[index] index = largest print(heap)
After removing 20, replace root with 8. Bubble down:
- Compare 8 with children 18 and 15; 18 is largest, swap.
- Now 8 at index 1, children 10 and 12; 12 is largest, swap.
- Now 8 at index 4, no children, stop.
Final heap: [18, 12, 15, 10, 8, 9]
heap = [4, 7, 6, 9, 10] heap[0] = heap.pop() index = 0 size = len(heap) while True: left = 2 * index + 1 right = 2 * index + 2 smallest = index if left < size and heap[left] < heap[smallest]: smallest = left if right < size and heap[right] < heap[smallest]: smallest = right if smallest == index: break heap[index], heap[smallest] = heap[smallest], heap[index] index = smallest print(heap)
The code executes without any error. heap[0] = heap.pop() pops 10 and assigns it to heap[0], resulting in heap = [10, 7, 6, 9]. Then bubble down:
- At index 0 (10), left=7 <10, right=6 <7, so smallest=2, swap with 6: [6, 7, 10, 9].
- At index 2 (10), no children, stop.
Final output: [6, 7, 10, 9]. No runtime error occurs.
heap = [30, 20, 20, 15, 10, 10, 5] heap[0] = heap[-1] heap.pop() index = 0 size = len(heap) while True: left = 2 * index + 1 right = 2 * index + 2 largest = index if left < size and heap[left] > heap[largest]: largest = left if right < size and heap[right] > heap[largest]: largest = right if largest == index: break heap[index], heap[largest] = heap[largest], heap[index] index = largest print(heap)
After removing 30, replace root with 5. Bubble down:
- Compare 5 with children 20 and 20; left 20 > 5 so largest=1, right 20 not >20, swap with left 20.
- Now 5 at index 1, children 15 and 10; 15 largest, swap with index 3.
- Now 5 at index 3, no children, stop.
Final heap: [20, 15, 20, 5, 10, 10]
When the root is removed, the last element replaces it. This element may be smaller or larger than its children, violating the heap property. Bubble down moves it down the tree by swapping with the smaller (min-heap) or larger (max-heap) child until the heap property is restored.
