Heap Insert Operation Bubble Up in DSA C++ - Time & Space Complexity
We want to understand how long it takes to add a new item to a heap and fix its position.
How does the time needed grow as the heap gets bigger?
Analyze the time complexity of the following code snippet.
void bubbleUp(std::vector<int>& heap, int index) {
while (index > 0) {
int parent = (index - 1) / 2;
if (heap[index] <= heap[parent]) break;
std::swap(heap[index], heap[parent]);
index = parent;
}
}
This code moves the newly inserted element up the heap until the heap property is restored.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Comparing and swapping the inserted element with its parent.
- How many times: At most once per level up the heap, from the inserted node to the root.
Each step moves the element one level up the heap tree, which has a height related to the number of elements.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 4 steps (levels) |
| 100 | About 7 steps |
| 1000 | About 10 steps |
Pattern observation: The number of steps grows slowly, roughly with the height of the heap, which increases as the logarithm of n.
Time Complexity: O(log n)
This means the time to fix the heap after insertion grows slowly as the heap gets bigger, only about one step per level.
[X] Wrong: "The insert operation takes constant time because we just add at the end."
[OK] Correct: Adding at the end is quick, but fixing the heap order by moving the element up can take multiple steps depending on heap height.
Knowing this time complexity helps you explain why heaps are efficient for priority queues and sorting, showing you understand how data structure operations scale.
"What if the heap was a ternary heap (each node has 3 children) instead of binary? How would the time complexity change?"
Practice
Solution
Step 1: Understand heap property
In a min-heap, every parent node is smaller than its children. When inserting, this property might be violated.Step 2: Purpose of bubble up
The bubble up operation moves the newly inserted element up the tree if it is smaller than its parent, restoring the heap property.Final Answer:
To restore the heap property by moving the new element up if it is smaller than its parent -> Option CQuick Check:
Bubble up fixes heap by moving smaller element up [OK]
- Thinking bubble up moves element down
- Confusing bubble up with deletion
- Assuming bubble up sorts entire heap
heap?Solution
Step 1: Identify bubble up logic
Bubble up compares the inserted element with its parent at index (i-1)/2 and swaps if smaller.Step 2: Check each option
int i = heap.size() - 1; while (i > 0 && heap[i] < heap[(i - 1) / 2]) { std::swap(heap[i], heap[(i - 1) / 2]); i = (i - 1) / 2; } correctly uses the parent index and swaps while the child is smaller. Others use wrong indices or wrong comparison.Final Answer:
int i = heap.size() - 1; while (i > 0 && heap[i] < heap[(i - 1) / 2]) { std::swap(heap[i], heap[(i - 1) / 2]); i = (i - 1) / 2; } -> Option AQuick Check:
Bubble up uses parent index (i-1)/2 and swaps if smaller [OK]
- Using child indices instead of parent
- Swapping when child is larger
- Starting index at 0 instead of last element
heap = {2, 5, 8, 10, 15}, what will be the heap vector after inserting 1 and performing bubble up?Solution
Step 1: Insert 1 at the end
Heap becomes [2, 5, 8, 10, 15, 1]. Index of 1 is 5.Step 2: Bubble up 1
Parent index = (5-1)/2 = 2. Compare heap[5]=1 with heap[2]=8. Since 1 < 8, swap. Heap: [2, 5, 1, 10, 15, 8]. Now i=2.Step 3: Continue bubble up
Parent index = (2-1)/2=0. Compare heap[2]=1 with heap[0]=2. Since 1 < 2, swap. Heap: [1, 5, 2, 10, 15, 8]. Now i=0, stop.Final Answer:
[1, 5, 2, 10, 15, 8] -> Option BQuick Check:
Bubble up swaps until heap property restored [OK]
- Not swapping enough times
- Swapping with wrong parent index
- Leaving new element at the end
heap. Identify the error:
int i = heap.size() - 1;
while (i > 0 && heap[i] > heap[(i - 1) / 2]) {
std::swap(heap[i], heap[(i - 1) / 2]);
i = (i - 1) / 2;
}Solution
Step 1: Understand bubble up condition
In a min-heap, bubble up swaps when child is smaller than parent, so condition should be heap[i] < heap[parent].Step 2: Identify error in code
The code uses heap[i] > heap[parent], which is incorrect and will not restore min-heap property.Final Answer:
The comparison operator should be '<' instead of '>' -> Option AQuick Check:
Bubble up swaps when child < parent [OK]
- Using wrong comparison direction
- Starting loop at wrong index
- Miscalculating parent index
heap = {20, 15, 18, 10, 12, 17}. You insert 19 at the end and perform bubble up. What is the resulting heap vector?Solution
Step 1: Insert 19 at the end
Heap becomes [20, 15, 18, 10, 12, 17, 19]. Index of 19 is 6.Step 2: Bubble up 19 in max-heap
Parent index = (6-1)/2 = 2. Compare heap[6]=19 with heap[2]=18. Since 19 > 18, swap. Heap: [20, 15, 19, 10, 12, 17, 18]. Now i=2.Step 3: Continue bubble up
Parent index = (2-1)/2=0. Compare heap[2]=19 with heap[0]=20. Since 19 < 20, stop.Final Answer:
[20, 15, 19, 10, 12, 17, 18] -> Option DQuick Check:
Bubble up swaps if child > parent in max-heap [OK]
- Using min-heap logic for max-heap
- Not swapping enough times
- Incorrect parent index calculation
Ready to prove it?
Pass the test and this pattern moves unknown
