Inserting elements one by one into a min-heap and restoring heap property by bubbling up.
Execution Table
Step
Operation
Nodes in Heap (Array)
Pointer Changes
Visual State (Tree)
1
Insert 30
[30]
head=0
30
2
Insert 20
[20, 30]
20 at index 1, parent index 0 (30 > 20), swap
20
/
30
3
Insert 10
[10, 30, 20]
10 at index 2, parent index 0 (20 > 10), swap to root
10
/ \
30 20
4
Insert 40
[10, 30, 20, 40]
40 at index 3, parent index 1 (30 < 40), no swap
10
/ \
30 20
/
40
5
Insert 5
[5, 10, 20, 40, 30]
5 at index 4, parent index 1 (30 > 5) swap; parent index 0 (10 > 5) swap to root
5
/ \
10 20
/ \
40 30
6
Insert 25
[5, 10, 20, 40, 30, 25]
25 at index 5, parent index 2 (20 < 25), no swap
5
/ \
10 20
/ \ \
40 30 25
7
End
Heap ready
No changes
Final heap structure
💡 All elements inserted and heap property maintained after each insertion, with bubbling up demonstrated in steps 2, 3, and 5.
Variable Tracker
Variable
Start
After 1
After 2
After 3
After 4
After 5
After 6
Final
Heap Array
[]
[30]
[20, 30]
[10, 30, 20]
[10, 30, 20, 40]
[5, 10, 20, 40, 30]
[5, 10, 20, 40, 30, 25]
[5, 10, 20, 40, 30, 25]
Size
0
1
2
3
4
5
6
6
Key Moments - 3 Insights
Why do we insert the new element at the end of the array?
Because the heap is a complete binary tree, new elements must fill the tree level by level from left to right, which corresponds to adding at the end of the array (see execution_table steps 1-6).
Why do we compare the inserted element with its parent?
To maintain the heap property (min-heap here), the inserted element must not be smaller than its parent. If it is smaller, we swap and continue up (see execution_table steps 2, 3, 5).
What happens if the inserted element is larger than its parent?
No swap is needed and the heap property is already maintained, so insertion stops (see execution_table steps 4, 6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the heap array after inserting 40?
A[40, 10, 30, 20]
B[10, 20, 30, 40]
C[10, 30, 20, 40]
D[10, 30, 40, 20]
💡 Hint
Check the 'Nodes in Heap (Array)' column at step 4 in execution_table.
At which step does the insertion require two swaps (bubbling up to root)?
AStep 5
BStep 3
CStep 2
DStep 6
💡 Hint
Look for the row describing two swaps in the 'Pointer Changes' column.
If we insert a new element smaller than the root, how would the heap change?
ANew element added at root directly
BNew element added at end and bubbled up to root
CNew element added at end with no swaps
DHeap array size decreases
💡 Hint
Refer to concept_flow and execution_table step 5 showing bubbling up after insertion.
Concept Snapshot
Heap is a complete binary tree stored as an array.
Insertions add element at array end.
Compare inserted element with parent.
If smaller, swap and repeat (bubble up).
Maintains min-heap property.
No swaps if heap property holds.
Full Transcript
A heap is a special tree structure where every parent node is smaller than its children in a min-heap. We store it as an array to keep it compact and complete. When we insert a new element, we add it at the end of the array to keep the tree complete. Then we compare it with its parent. If the new element is smaller, we swap it with the parent and continue this process until the heap property is restored or we reach the root. This process is called bubbling up. If the new element is larger or equal to the parent, no swaps are needed and the heap is ready for the next operation.