| 1 | Start with initial heap | [10, 15, 30, 40, 50, 100, 40] | head=0 | 10
/ \
15 30
/ \ / \
40 50 100 40 |
| 2 | Insert 8 at end | [10, 15, 30, 40, 50, 100, 40, 8] | new_node=7 | 10
/ \
15 30
/ \ / \
40 50 100 40
/
8 |
| 3 | Compare 8 with parent 40 (index 3) | [10, 15, 30, 40, 50, 100, 40, 8] | compare indices 7 and 3 | 8 < 40? Yes |
| 4 | Swap 8 and 40 | [10, 15, 30, 8, 50, 100, 40, 40] | swap indices 7 and 3 | 10
/ \
15 30
/ \ / \
8 50 100 40
/
40 |
| 5 | Compare 8 with new parent 15 (index 1) | [10, 15, 30, 8, 50, 100, 40, 40] | compare indices 3 and 1 | 8 < 15? Yes |
| 6 | Swap 8 and 15 | [10, 8, 30, 15, 50, 100, 40, 40] | swap indices 3 and 1 | 10
/ \
8 30
/ \ / \
15 50 100 40
/
40 |
| 7 | Compare 8 with new parent 10 (index 0) | [10, 8, 30, 15, 50, 100, 40, 40] | compare indices 1 and 0 | 8 < 10? Yes |
| 8 | Swap 8 and 10 | [8, 10, 30, 15, 50, 100, 40, 40] | swap indices 1 and 0 | 8
/ \
10 30
/ \ / \
15 50 100 40
/
40 |
| 9 | Compare 8 with parent (none, root) | [8, 10, 30, 15, 50, 100, 40, 40] | at root, stop | Heap property restored |
| 10 | End | [8, 10, 30, 15, 50, 100, 40, 40] | no pointer changes | Final heap state |