| 1 | Initial heap | [10, 15, 30, 40, 50, 100, 40] | root → index 0 (10) | 10
/ \
15 30
/ \ / \
40 50 100 40 |
| 2 | Remove root (10) | [10, 15, 30, 40, 50, 100, 40] | root extracted | Root 10 extracted |
| 3 | Replace root with last element (40) | [40, 15, 30, 40, 50, 100] | root → index 0 now 40 (was last) | 40
/ \
15 30
/ \ /
40 50 100 |
| 4 | Bubble down from index 0 (40) | [40, 15, 30, 40, 50, 100] | Compare 40 with children 15 (index 1) and 30 (index 2) | Current:40, Left child:15, Right child:30 |
| 5 | Swap with smaller child (15) | [15, 40, 30, 40, 50, 100] | Swap index 0 and 1 | 15
/ \
40 30
/ \ /
40 50 100 |
| 6 | Continue bubble down at index 1 (40) | [15, 40, 30, 40, 50, 100] | Compare 40 with children 40 (index 3) and 50 (index 4) | Current:40, Left child:40, Right child:50 |
| 7 | No swap needed (40 ≤ 40 and 50) | [15, 40, 30, 40, 50, 100] | Bubble down ends | 15
/ \
40 30
/ \ /
40 50 100 |
| 8 | Return extracted root | [15, 40, 30, 40, 50, 100] | Returned 10 | 15
/ \
40 30
/ \ /
40 50 100 |