Heap Insert Operation Bubble Up in DSA Python - 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 grow as the heap gets bigger?
Analyze the time complexity of the following code snippet.
def bubble_up(heap, index):
while index > 0:
parent = (index - 1) // 2
if heap[index] <= heap[parent]:
break
heap[index], heap[parent] = heap[parent], heap[index]
index = parent
# Assume heap is a max-heap and index is the last element inserted
This code moves the newly added element up the heap until the heap property is restored.
- Primary operation: Comparing and swapping the new element with its parent.
- How many times: At most once per level of the heap, moving up from the inserted node to the root.
Each step moves the element one level up in the heap tree.
| 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, proportional to the height of the heap.
[X] Wrong: "The insert operation always takes constant time because we just add at the end."
[OK] Correct: Adding at the end is quick, but fixing the heap by moving the element up can take multiple steps depending on heap size.
Understanding this helps you explain how heaps keep their order efficiently, a key skill for many coding challenges.
"What if the heap was a min-heap instead of a max-heap? How would the time complexity of bubble up change?"
Practice
Solution
Step 1: Understand heap property
In a min-heap, every parent node is smaller than its children.Step 2: Purpose of bubble up
When a new element is added at the bottom, bubble up moves it up if it is smaller than its parent to restore the heap property.Final Answer:
To restore the heap property by moving the new element up if it is smaller than its parent -> Option AQuick Check:
Bubble up fixes heap order by moving smaller elements up [OK]
- Thinking bubble up sorts the entire heap
- Confusing bubble up with removing elements
- Assuming bubble up adds element without changes
heap?Solution
Step 1: Identify bubble up condition
Bubble up swaps while current element is smaller than its parent.Step 2: Check code correctness
while i > 0 and heap[i] < heap[(i - 1) // 2]: heap[i], heap[(i - 1) // 2] = heap[(i - 1) // 2], heap[i]; i = (i - 1) // 2 uses a while loop with condition heap[i] < heap[parent], swapping and updating i correctly.Final Answer:
while i > 0 and heap[i] < heap[(i - 1) // 2]: heap[i], heap[(i - 1) // 2] = heap[(i - 1) // 2], heap[i]; i = (i - 1) // 2 -> Option AQuick Check:
Bubble up swaps smaller child with parent [OK]
- Using > instead of < in condition
- Using for loop instead of while
- Swapping with wrong indices
heap = [2, 5, 8, 10, 15], what will be the heap list after inserting 1 and performing bubble up?Solution
Step 1: Insert 1 at the end
Heap becomes [2, 5, 8, 10, 15, 1].Step 2: Bubble up 1
Compare 1 with parent at index 2 (value 8). Swap: [2, 5, 1, 10, 15, 8]. Then compare 1 with new parent at index 0 (value 2). Swap: [1, 5, 2, 10, 15, 8].Final Answer:
[1, 5, 2, 10, 15, 8] -> Option DQuick Check:
Inserted 1 bubbled up to root [OK]
- Not swapping enough times
- Swapping with wrong parent index
- Leaving new element at the end
def insert(heap, val):
heap.append(val)
i = len(heap) - 1
while i > 0 and heap[i] > heap[(i - 1) // 2]:
heap[i], heap[(i - 1) // 2] = heap[(i - 1) // 2], heap[i]
i = (i - 1) // 2Solution
Step 1: Analyze bubble up condition
For 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
Code uses heap[i] > heap[parent], which is wrong and breaks min-heap property.Final Answer:
The comparison operator should be '<' instead of '>' to maintain min-heap property -> Option CQuick Check:
Bubble up swaps when child < parent [OK]
- Using '>' instead of '<' in condition
- Incorrect index updates
- Appending at wrong position
[90, 70, 80, 40, 50, 60]. You insert 85 at the end and perform bubble up. What will be the final heap list?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
Compare 85 with parent at index 2 (80). Since 85 > 80, swap: [90, 70, 85, 40, 50, 60, 80]. Then compare 85 with new parent at index 0 (90). 85 < 90, stop bubbling up.Final Answer:
[90, 70, 85, 40, 50, 60, 80] -> Option BQuick Check:
Bubble up swaps larger child with parent in max-heap [OK]
- Not swapping enough times
- Confusing min-heap and max-heap conditions
- Leaving new element at the end
Ready to prove it?
Pass the test and this pattern moves unknown
