Heap Insert Operation Bubble Up in DSA Go - Time & Space Complexity
When we add a new item to a heap, it may need to move up to keep the heap order. We want to know how long this moving up takes as the heap grows.
How does the time to insert change when the heap gets bigger?
Analyze the time complexity of the following code snippet.
func bubbleUp(heap []int, index int) {
for index > 0 {
parent := (index - 1) / 2
if heap[index] <= heap[parent] {
break
}
heap[index], heap[parent] = heap[parent], heap[index]
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: The loop that swaps the new element with its parent while it is larger.
- How many times: At most, once per level of the heap from the inserted node up to the root.
Each swap moves the element one level up. The heap height grows slowly as the heap size grows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 4 swaps |
| 100 | About 7 swaps |
| 1000 | About 10 swaps |
Pattern observation: The number of swaps grows slowly, roughly proportional to the height of the heap, which is much smaller than the total number of elements.
Time Complexity: O(log n)
This means inserting an element takes time proportional to the height of the heap, which grows slowly as the heap gets bigger.
[X] Wrong: "Insertion always takes constant time because we just add at the end."
[OK] Correct: After adding, the element may need to move up many levels to keep order, so the time depends on the heap height, not just adding at the end.
Understanding how insertion time grows helps you explain heap efficiency clearly. This skill shows you know how data structures behave as they scale, which is key in many coding challenges.
"What if the heap was a min-heap instead of a max-heap? How would the bubble up time complexity change?"
Practice
Solution
Step 1: Understand the heap property
In a min-heap, every parent node must be smaller than its children.Step 2: Purpose of bubble up
When a new element is added at the end, bubble up moves it up to restore the heap property.Final Answer:
To move the new element up until the heap property is restored -> Option CQuick Check:
Bubble up fixes heap by moving new element up [OK]
- Thinking bubble up moves element down
- Confusing bubble up with removing elements
- Assuming bubble up sorts the entire heap
Solution
Step 1: Understand swapping in Go slices
To swap two elements in a slice, assign them simultaneously.Step 2: Check each option
heap[i], heap[parent] = heap[parent], heap[i] uses simultaneous assignment to swap correctly; others do not swap both elements.Final Answer:
heap[i], heap[parent] = heap[parent], heap[i] -> Option AQuick Check:
Simultaneous assignment swaps elements [OK]
- Assigning one element without swapping
- Using undefined swap function
- Confusing assignment direction
[2, 5, 3, 10], what will be the heap after inserting 1 and performing bubble up?Solution
Step 1: Insert 1 at the end
Heap becomes [2, 5, 3, 10, 1].Step 2: Bubble up 1
Compare 1 with parent 5 (index 1), swap: [2, 1, 3, 10, 5]. Then compare 1 with parent 2 (index 0), swap: [1, 2, 3, 10, 5].Final Answer:
[1, 2, 3, 10, 5] -> Option BQuick Check:
Bubble up swaps until heap property restored [OK]
- Not swapping with grandparent
- Leaving new element at the end
- Swapping with wrong parent index
for i > 0 && heap[i] < heap[(i-1)/2] {
heap[i], heap[(i-1)/2] = heap[(i-1)/2], heap[i]
i = (i-1)/2
}
What is the bug in this code?Solution
Step 1: Check parent index calculation
Parent index in a binary heap is (i-1)/2, which is correctly used.Step 2: Verify loop condition and index update
Loop continues while current element is less than parent; index updates to parent correctly.Final Answer:
No bug; the code correctly bubbles up the element -> Option AQuick Check:
Parent index and swaps are correct [OK]
- Miscomputing parent index
- Using wrong comparison operator
- Incorrect index update after swap
[90, 70, 80, 40, 50, 60]. You insert 85 at the end and perform bubble up. What is the final heap after bubble up?Solution
Step 1: Insert 85 at the end
Heap becomes [90, 70, 80, 40, 50, 60, 85].Step 2: Bubble up 85 in max-heap
Parent of index 6 is (6-1)/2 = 2 with value 80. Since 85 > 80, swap: [90, 70, 85, 40, 50, 60, 80]. Now index 2's parent is index 0 (90), 85 < 90, stop.Final Answer:
[90, 70, 85, 40, 50, 60, 80] -> Option DQuick Check:
Bubble up swaps while child > parent in max-heap [OK]
- Not swapping with parent when child is larger
- Stopping bubble up too early
- Confusing max-heap with min-heap rules
Ready to prove it?
Pass the test and this pattern moves unknown
