Heap Insert Operation Bubble Up in DSA Typescript - Time & Space Complexity
When we add a new item to a heap, we need to keep the heap rules intact. This involves moving the new item up the heap if it's bigger or smaller than its parent.
We want to know how the time to do this moving up changes as the heap grows.
Analyze the time complexity of the following code snippet.
function bubbleUp(heap: number[], index: number): void {
while (index > 0) {
const parentIndex = Math.floor((index - 1) / 2);
if (heap[index] <= heap[parentIndex]) break;
[heap[index], heap[parentIndex]] = [heap[parentIndex], heap[index]];
index = parentIndex;
}
}
This code moves the newly added element up the heap until the heap property is restored.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that compares and swaps the new element with its parent.
- How many times: At most, this loop runs once per level of the heap, moving from the inserted node up to the root.
Each time we add a new item, it might move up several levels. The heap is a tree with levels growing slowly as the heap grows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 4 moves (levels) |
| 100 | About 7 moves |
| 1000 | About 10 moves |
Pattern observation: The number of moves grows slowly, roughly with the height of the heap, which increases as the logarithm of the number of items.
Time Complexity: O(log n)
This means the time to insert and bubble up grows slowly as the heap gets bigger, only increasing with the height of the heap.
[X] Wrong: "The bubble up always takes as long as the number of items in the heap (O(n))."
[OK] Correct: The heap is a balanced tree, so the bubble up only moves up the height of the tree, which grows much slower than the total number of items.
Understanding how bubble up works and its time cost helps you explain heap insertions clearly and confidently, a skill often valued in interviews.
What if we changed the heap to a linked structure instead of an array? How would the time complexity of bubble up change?
Practice
Solution
Step 1: Understand the heap property
In a min-heap, each parent node must be smaller than its children. In a max-heap, each parent must be larger.Step 2: Purpose of bubble up
When inserting a new element, it is placed at the end. Bubble up moves it up the tree to restore the heap property if violated.Final Answer:
To restore the heap property by moving the new element up if it is smaller (min-heap) or larger (max-heap) than its parent -> Option DQuick Check:
Bubble up fixes heap order after insert [OK]
- Confusing bubble up with removing elements
- Thinking bubble up sorts the entire heap
- Assuming bubble up swaps elements randomly
Solution
Step 1: Identify min-heap bubble up condition
In a min-heap, bubble up swaps while the child is smaller than the parent.Step 2: Check code correctness
while (index > 0 && heap[index] < heap[parentIndex]) { swap(heap, index, parentIndex); index = parentIndex; } uses a while loop with condition heap[index] < heap[parentIndex], correctly swapping and updating index.Final Answer:
while (index > 0 && heap[index] < heap[parentIndex]) { swap(heap, index, parentIndex); index = parentIndex; } -> Option AQuick Check:
Min-heap bubble up swaps smaller child up [OK]
- Using > instead of < for min-heap
- Using for loop instead of while
- Not updating index after swap
[50, 30, 40, 10, 20], what will be the heap array after inserting 45 and performing bubble up?Solution
Step 1: Insert 45 at the end
Array becomes [50, 30, 40, 10, 20, 45]Step 2: Bubble up 45 in max-heap
Parent index of 45 (index 5) is 2 (value 40). Since 45 > 40, swap. New array: [50, 30, 45, 10, 20, 40]. Now index 2's parent is 0 (value 50). 45 < 50, stop bubbling up.Final Answer:
[50, 30, 45, 10, 20, 40] -> Option BQuick Check:
Bubble up swaps 45 with 40 only [OK]
- Not swapping 45 with 40
- Swapping 45 with 30 instead of 40
- Stopping bubble up too early
while (index > 0 && heap[index] > heap[parentIndex]) {
swap(heap, index, parentIndex);
index = parentIndex;
}
What is the error in this code?Solution
Step 1: Check comparison for min-heap bubble up
In a min-heap, bubble up swaps when child is smaller than parent, so condition should be heap[index] < heap[parentIndex].Step 2: Identify error in code
The code uses '>' which is incorrect for min-heap bubble up, causing wrong swaps.Final Answer:
The comparison operator should be '<' to maintain min-heap property -> Option CQuick Check:
Min-heap bubble up uses < comparison [OK]
- Using > instead of < in condition
- Not updating parentIndex after swap
- Loop condition index > 0 is correct, not < 0
[5, 10, 15, 20, 25, 30]. You insert 12 and perform bubble up. After that, you insert 3 and perform bubble up again. What is the final heap array?Solution
Step 1: Insert 12 and bubble up
Insert 12 at index 6: [5,10,15,20,25,30,12]. Parent of index 6 is 2 (value 15). Since 12 < 15, swap: [5,10,12,20,25,30,15]. Parent of index 2 is 0 (value 5). 12 > 5, stop.Step 2: Insert 3 and bubble up
Insert 3 at index 7: [5,10,12,20,25,30,15,3]. Parent of index 7 is 3 (value 20). 3 < 20, swap: [5,10,12,3,25,30,15,20]. Parent of index 3 is 1 (value 10). 3 < 10, swap: [5,3,12,10,25,30,15,20]. Parent of index 1 is 0 (value 5). 3 < 5, swap: [3,5,12,10,25,30,15,20]. Now at root, stop.Final Answer:
[3, 5, 12, 10, 25, 30, 15, 20] -> Option AQuick Check:
Bubble up swaps until heap property restored [OK]
- Not swapping 3 all the way to root
- Incorrect parent index calculation
- Stopping bubble up too early
Ready to prove it?
Pass the test and this pattern moves unknown
