| 1 | Initial heap | [10, 15, 30, 40, 50, 100, 40] | head = 0, size = 7 | Array: [10, 15, 30, 40, 50, 100, 40]
Tree:
10
/ \
15 30
/ \ / \
40 50 100 40 |
| 2 | Extract min: remove root 10 | [40, 15, 30, 40, 50, 100, 40] | head replaced with last element (index 6) | Array after replace root with last element:
[40, 15, 30, 40, 50, 100, 40]
Tree:
40
/ \
15 30
/ \ / \
40 50 100 40 |
| 3 | Remove last element (index 6) | [40, 15, 30, 40, 50, 100] | size decremented to 6 | Array size reduced:
[40, 15, 30, 40, 50, 100]
Tree:
40
/ \
15 30
/ \ /
40 50 100 |
| 4 | Bubble down from root (index 0) | [40, 15, 30, 40, 50, 100] | current = 0 | Start bubble down at root 40 |
| 5 | Compare children 15 (index 1) and 30 (index 2) | [40, 15, 30, 40, 50, 100] | smaller child = 15 at index 1 | Children: 15 and 30, smaller is 15 |
| 6 | 40 > 15? Yes, swap | [15, 40, 30, 40, 50, 100] | swap indices 0 and 1, current = 1 | After swap:
[15, 40, 30, 40, 50, 100]
Tree:
15
/ \
40 30
/ \ /
40 50 100 |
| 7 | Compare children of index 1: 40 (index 3) and 50 (index 4) | [15, 40, 30, 40, 50, 100] | smaller child = 40 at index 3 | Children: 40 and 50, smaller is 40 |
| 8 | 40 > 40? No, stop bubble down | [15, 40, 30, 40, 50, 100] | bubble down ends | Heap property restored |