| 1 | Insert 8 at end | 7 | N/A | [10, 15, 30, 40, 50, 100, 40, 8] | New element index = 7 | Array: [10, 15, 30, 40, 50, 100, 40, 8]
Tree:
10
/ \
15 30
/ \ / \
40 50 100 40
\
8 (new) |
| 2 | Compare with parent | 7 | 3 | [10, 15, 30, 40, 50, 100, 40, 8] | Compare heap[7]=8 with heap[3]=40 | 8 < 40 → swap |
| 3 | Swap with parent | 7 → 3 | 1 | [10, 15, 30, 8, 50, 100, 40, 40] | Swap indices 7 and 3, index now 3 | Array: [10, 15, 30, 8, 50, 100, 40, 40]
Tree:
10
/ \
15 30
/ \ / \
8 50 100 40
(40 moved down) |
| 4 | Compare with parent | 3 | 1 | [10, 15, 30, 8, 50, 100, 40, 40] | Compare heap[3]=8 with heap[1]=15 | 8 < 15 → swap |
| 5 | Swap with parent | 3 → 1 | 0 | [10, 8, 30, 15, 50, 100, 40, 40] | Swap indices 3 and 1, index now 1 | Array: [10, 8, 30, 15, 50, 100, 40, 40]
Tree:
10
/ \
8 30
/ \ / \
15 50 100 40
|
| 6 | Compare with parent | 1 | 0 | [10, 8, 30, 15, 50, 100, 40, 40] | Compare heap[1]=8 with heap[0]=10 | 8 < 10 → swap |
| 7 | Swap with parent | 1 → 0 | N/A | [8, 10, 30, 15, 50, 100, 40, 40] | Swap indices 1 and 0, index now 0 | Array: [8, 10, 30, 15, 50, 100, 40, 40]
Tree:
8
/ \
10 30
/ \ / \
15 50 100 40
|
| 8 | Compare with parent | 0 | N/A | [8, 10, 30, 15, 50, 100, 40, 40] | Index 0 is root, stop bubble up | Heap property restored |