| 1 | Insert 8 at end | 7 | 3 | [10, 15, 30, 40, 50, 100, 40, 8] | Pointer at index 7 | Array: [10, 15, 30, 40, 50, 100, 40, 8]
Tree:
10
/ \
15 30
/ \ / \
40 50 100 40
/
8 ← new node |
| 2 | Compare with parent | 7 | 3 | [10, 15, 30, 40, 50, 100, 40, 8] | 8 < 40? Yes, swap | Swap nodes at index 7 and 3 |
| 3 | Swap with parent | 7 → 3 | 1 | [10, 15, 30, 8, 50, 100, 40, 40] | Pointer moves to index 3 | Array: [10, 15, 30, 8, 50, 100, 40, 40]
Tree:
10
/ \
15 30
/ \ / \
8 50 100 40
/
40 |
| 4 | Compare with parent | 3 | 1 | [10, 15, 30, 8, 50, 100, 40, 40] | 8 < 15? Yes, swap | Swap nodes at index 3 and 1 |
| 5 | Swap with parent | 3 → 1 | 0 | [10, 8, 30, 15, 50, 100, 40, 40] | Pointer moves to index 1 | Array: [10, 8, 30, 15, 50, 100, 40, 40]
Tree:
10
/ \
8 30
/ \ / \
15 50 100 40
/
40 |
| 6 | Compare with parent | 1 | 0 | [10, 8, 30, 15, 50, 100, 40, 40] | 8 < 10? Yes, swap | Swap nodes at index 1 and 0 |
| 7 | Swap with parent | 1 → 0 | - | [8, 10, 30, 15, 50, 100, 40, 40] | Pointer moves to index 0 (root) | Array: [8, 10, 30, 15, 50, 100, 40, 40]
Tree:
8
/ \
10 30
/ \ / \
15 50 100 40
/
40 |
| 8 | Compare with parent | 0 | - | [8, 10, 30, 15, 50, 100, 40, 40] | At root, stop bubble up | Bubble up complete |