Heap extraction (bubble down) in Data Structures Theory - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we remove the top item from a heap, we need to restore its order. This process is called bubble down.
We want to know how the time to do this grows as the heap gets bigger.
Analyze the time complexity of the following heap extraction code.
function heapExtract(heap) {
if (heap.size === 0) return null;
const top = heap[0];
heap[0] = heap[heap.size - 1];
heap.size--;
bubbleDown(heap, 0);
return top;
}
function bubbleDown(heap, index) {
let left = 2 * index + 1;
let right = 2 * index + 2;
let smallest = index;
if (left < heap.size && heap[left] < heap[smallest]) smallest = left;
if (right < heap.size && heap[right] < heap[smallest]) smallest = right;
if (smallest !== index) {
[heap[index], heap[smallest]] = [heap[smallest], heap[index]];
bubbleDown(heap, smallest);
}
}
This code removes the top element from a min-heap and restores heap order by moving the new root down.
Look at what repeats during bubble down.
- Primary operation: Comparing and swapping the current node with its smaller child.
- How many times: At most once per level of the heap, moving down from root to leaf.
Each bubble down step moves one level down the heap tree.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 3 to 4 steps |
| 100 | About 6 to 7 steps |
| 1000 | About 9 to 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 restore heap order grows slowly as the heap size grows, increasing by about one step each time the heap size doubles.
[X] Wrong: "Bubble down takes the same time no matter the heap size because it just swaps a few elements."
[OK] Correct: The number of swaps depends on the heap height, which grows with the size of the heap, so bigger heaps can take more steps.
Understanding how bubble down works and its time cost helps you explain heap operations clearly and shows you know how data structures behave as they grow.
"What if the heap was a max-heap instead of a min-heap? How would the time complexity of bubble down change?"
Practice
bubble down operation during heap extraction?Solution
Step 1: Understand heap extraction
Heap extraction removes the root element, which may break the heap property.Step 2: Role of bubble down
Bubble down swaps the root with its smaller child repeatedly to restore the heap order.Final Answer:
To restore the heap property after removing the root element -> Option AQuick Check:
Bubble down fixes heap after root removal = D [OK]
- Confusing bubble down with insertion
- Thinking bubble down sorts the entire heap
- Believing bubble down finds max element
Solution
Step 1: Understand min-heap property
In a min-heap, parent nodes must be smaller than their children.Step 2: Bubble down condition
Bubble down continues while the current node is larger than at least one child, to swap with the smaller child.Final Answer:
While the current node is larger than at least one child -> Option BQuick Check:
Bubble down if parent > any child = C [OK]
- Stopping bubble down too early
- Swapping with larger child instead of smaller
- Confusing min-heap with max-heap conditions
[2, 5, 3, 10, 7], what is the array after extracting the root and performing bubble down?Solution
Step 1: Remove root and replace with last element
Remove 2, replace root with 7: [7, 5, 3, 10]Step 2: Bubble down to restore heap
Compare 7 with children 5 and 3; swap with smaller child 3: [3, 5, 7, 10]Final Answer:
[3, 5, 7, 10] -> Option CQuick Check:
Bubble down swaps root with smaller child = A [OK]
- Not replacing root with last element
- Swapping with larger child
- Forgetting to bubble down fully
function bubbleDown(heap, index):
left = 2 * index + 1
right = 2 * index + 2
smallest = index
if left < heap.size and heap[left] > heap[smallest]:
smallest = left
if right < heap.size and heap[right] > heap[smallest]:
smallest = right
if smallest != index:
swap(heap[index], heap[smallest])
bubbleDown(heap, smallest)Solution
Step 1: Check comparison logic
In a min-heap, bubble down swaps with the smaller child, so comparisons must find the smallest value.Step 2: Identify incorrect operator
The code uses '>' which finds the largest child; it should use '<' to find the smallest child.Final Answer:
The comparisons should use '<' instead of '>' to find the smallest child -> Option DQuick Check:
Use < to find smallest child in min-heap bubble down = A [OK]
- Using '>' instead of '<' in comparisons
- Mixing up left/right child indices
- Calling bubbleDown outside swap condition
[50, 30, 40, 10, 20, 35]. After extracting the root and performing bubble down, what is the resulting array?Solution
Step 1: Remove root and replace with last element
Remove 50, replace root with 35: [35, 30, 40, 10, 20]Step 2: Bubble down to restore max-heap
Compare 35 with children 30 and 40; swap with larger child 40: [40, 30, 35, 10, 20]Step 3: Continue bubbling down
Now 35 at index 2 has children indices 5 and 6 which don't exist, so stop.Final Answer:
[40, 30, 35, 10, 20] -> Option AQuick Check:
Bubble down swaps with larger child in max-heap = C [OK]
- Swapping with smaller child in max-heap
- Not replacing root with last element
- Stopping bubble down too early
