| 1 | Extract root (10) | [10, 15, 30, 20, 50, 100, 40] | root points to index 0 | Heap before extraction:
Array: [10, 15, 30, 20, 50, 100, 40]
Tree:
10
/ \
15 30
/ \ / \
20 50 100 40 |
| 2 | Replace root with last element (40) | [40, 15, 30, 20, 50, 100, 40] | root = 40 (from last element at index 6) | After replacement:
Array: [40, 15, 30, 20, 50, 100, 40]
Tree:
40
/ \
15 30
/ \ / \
20 50 100 40 |
| 3 | Remove last element (index 6) | [40, 15, 30, 20, 50, 100] | heap size reduced to 6 | After removal:
Array: [40, 15, 30, 20, 50, 100]
Tree:
40
/ \
15 30
/ \ /
20 50 100 |
| 4 | Bubble down from root (index 0) | [40, 15, 30, 20, 50, 100] | current = 0 | Start bubble down:
Current node = 40 at index 0 |
| 5 | Compare children 15 (index 1) and 30 (index 2) | [40, 15, 30, 20, 50, 100] | smaller child = 15 at index 1 | Children:
Left = 15
Right = 30
Smaller child = 15 |
| 6 | Swap 40 with 15 | [15, 40, 30, 20, 50, 100] | current moves to index 1 | After swap:
Array: [15, 40, 30, 20, 50, 100]
Tree:
15
/ \
40 30
/ \ /
20 50 100 |
| 7 | Compare children 20 (index 3) and 50 (index 4) | [15, 40, 30, 20, 50, 100] | smaller child = 20 at index 3 | Children:
Left = 20
Right = 50
Smaller child = 20 |
| 8 | Swap 40 with 20 | [15, 20, 30, 40, 50, 100] | current moves to index 3 | After swap:
Array: [15, 20, 30, 40, 50, 100]
Tree:
15
/ \
20 30
/ \ /
40 50 100 |
| 9 | No children (index 7 and 8 out of range) | [15, 20, 30, 40, 50, 100] | bubble down ends | Reached leaf node, bubble down complete |