Heap insertion (bubble up) in Data Structures Theory - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we add a new item to a heap, we need to keep the heap rules intact. This process is called "bubble up."
We want to know how the time to do this grows as the heap gets bigger.
Analyze the time complexity of the following code snippet.
function heapInsert(heap, value) {
heap.push(value);
let index = heap.length - 1;
while (index > 0) {
let parentIndex = Math.floor((index - 1) / 2);
if (heap[parentIndex] >= heap[index]) break;
[heap[parentIndex], heap[index]] = [heap[index], heap[parentIndex]];
index = parentIndex;
}
}
This code adds a new value to the heap and moves it up 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 value with its parent.
- How many times: At most, this loop runs once per level of the heap, moving up from the inserted node to the root.
Each time we add a new item, it might move up several levels. The number of levels grows slowly as the heap grows.
| 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 heap size grows.
Time Complexity: O(log n)
This means the time to insert grows slowly, proportional to the height of the heap, not the total number of items.
[X] Wrong: "Insertion takes the same time no matter how big the heap is because we just add at the end."
[OK] Correct: While adding at the end is quick, restoring the heap order by moving the new item up can take more steps as the heap grows.
Understanding heap insertion time helps you explain how priority queues work efficiently. It shows you can analyze how data structure operations scale with size.
"What if the heap was a min-heap instead of a max-heap? How would the time complexity of insertion change?"
Practice
Solution
Step 1: Add new element at the end
The new element is always added at the end of the heap to maintain the complete tree property.Step 2: Prepare for bubble up
After adding, the element will be compared with its parent to restore heap order.Final Answer:
Add the new element at the end of the heap -> Option AQuick Check:
Insertion starts by adding at the end [OK]
- Starting at the root instead of the end
- Removing elements before insertion
- Sorting the entire heap immediately
Solution
Step 1: Understand min-heap property
In a min-heap, parents must be less than or equal to their children.Step 2: Bubble up condition
If the new element is less than its parent, it violates the heap property and must bubble up.Final Answer:
Continue bubbling up if the new element is less than its parent -> Option DQuick Check:
Bubble up when child < parent in min-heap [OK]
- Bubbling up when new element is greater
- Ignoring equality cases
- Not bubbling up at all
[2, 5, 8, 10, 15], what will be the array after inserting 1 and performing bubble up?Solution
Step 1: Insert 1 at the end
Array becomes [2, 5, 8, 10, 15, 1].Step 2: Bubble up 1
Compare 1 with parent 8 (index 2). Since 1 < 8, swap: [2, 5, 1, 10, 15, 8]. Then compare 1 with parent 5 (index 1). Since 1 < 5, swap: [2, 1, 8, 10, 15, 5]. Then compare 1 with parent 2 (index 0). Since 1 < 2, swap: [1, 2, 8, 10, 15, 5].Final Answer:
[1, 2, 5, 10, 15, 8] -> Option BQuick Check:
Bubble up swaps until heap property restored [OK]
- Not swapping enough times
- Swapping with wrong parent
- Leaving new element at the end
def bubble_up(heap, index):
while index > 0:
parent = (index - 1) // 2
if heap[index] > heap[parent]:
heap[index], heap[parent] = heap[parent], heap[index]
index = parent
else:
break
Solution
Step 1: Analyze comparison logic
For a min-heap, bubble up should swap when child is less than parent, not greater.Step 2: Identify correct condition
The code uses '>' which is wrong; it should be '<' to maintain min-heap property.Final Answer:
The comparison should be heap[index] < heap[parent] for min-heap -> Option AQuick Check:
Bubble up swaps when child < parent in min-heap [OK]
- Using > instead of <
- Wrong parent index formula
- Incorrect loop condition
[20, 15, 18, 8, 10, 17]. You insert 19. After bubble up, what is the correct heap array?Solution
Step 1: Insert 19 at the end
Array becomes [20, 15, 18, 8, 10, 17, 19].Step 2: Bubble up 19 in max-heap
Parent of index 6 is index 2 (value 18). Since 19 > 18, swap: [20, 15, 19, 8, 10, 17, 18]. Next parent is index 0 (value 20). Since 19 < 20, stop bubbling up.Step 3: Final heap array
The final array is [20, 15, 19, 8, 10, 17, 18].Final Answer:
[20, 15, 19, 8, 10, 17, 18] -> Option CQuick Check:
Bubble up swaps child > parent in max-heap [OK]
- Not swapping enough times
- Swapping with wrong parent
- Leaving new element at the end
