Heap Extract Min or Max Bubble Down in DSA Java - Time & Space Complexity
We want to understand how long it takes to remove the smallest or largest item from a heap and fix the heap order.
How does the work grow as the heap gets bigger?
Analyze the time complexity of the following code snippet.
public int extractMin() {
int min = heap[0];
heap[0] = heap[size - 1];
size--;
bubbleDown(0);
return min;
}
private void bubbleDown(int index) {
int left = 2 * index + 1;
int right = 2 * index + 2;
int smallest = index;
if (left < size && heap[left] < heap[smallest]) smallest = left;
if (right < size && heap[right] < heap[smallest]) smallest = right;
if (smallest != index) {
swap(index, smallest);
bubbleDown(smallest);
}
}
This code removes the smallest item from a min-heap and restores heap order by moving the new root down.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The recursive
bubbleDownmethod that compares and swaps nodes down the heap. - How many times: It runs once per level of the heap, moving down from the root to a leaf.
Each step moves down one level in the heap tree. The heap height grows slowly as the heap gets bigger.
| Input Size (n) | Approx. Operations (levels) |
|---|---|
| 10 | About 4 steps |
| 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 logarithmically.
Time Complexity: O(log n)
This means the time to fix the heap grows slowly as the heap size grows, only increasing by a small amount for big heaps.
[X] Wrong: "Extracting min takes linear time because we might check every element."
[OK] Correct: The heap structure lets us only check a path down the tree, not all elements, so it's much faster than checking everything.
Knowing this time complexity helps you explain why heaps are great for priority tasks and efficient removal, a key skill in many coding challenges.
"What if the heap was a max-heap instead of a min-heap? How would the time complexity of extract and bubble down change?"
Practice
bubble down operation in a heap after extracting the min or max element?Solution
Step 1: Understand the extract operation
When we remove the min or max element (usually the root), the heap may lose its order property.Step 2: Purpose of bubble down
Bubble down moves the new root element down the tree to restore the heap property by swapping it with the smaller (min-heap) or larger (max-heap) child.Final Answer:
To restore the heap property by moving the root element down to its correct position -> Option BQuick Check:
Bubble down fixes heap order after extraction [OK]
- Thinking bubble down inserts new elements
- Assuming bubble down removes leaf nodes
- Believing bubble down only swaps root and last element
Solution
Step 1: Identify parameters needed for bubble down
We need the heap array, the current index to bubble down from, and the size of the heap to avoid out-of-bounds.Step 2: Match method signature
void bubbleDown(int[] heap, int index, int size) has all parameters in correct order and returns void as bubble down modifies the heap in place.Final Answer:
void bubbleDown(int[] heap, int index, int size) -> Option AQuick Check:
Correct parameters and void return for bubble down [OK]
- Missing size parameter causing index errors
- Wrong return type expecting a value
- Incorrect parameter order
[2, 5, 3, 10, 7], after extracting the min element and performing bubble down, what will be the resulting heap array?Solution
Step 1: Remove min and replace root
Remove 2 (min), replace root with last element 7: [7, 5, 3, 10]Step 2: Bubble down from root
Compare 7 with children 5 and 3; smallest is 3. Swap 7 and 3: [3, 5, 7, 10]. Now 7 has children 10 only, 7 < 10, so stop.Final Answer:
[3, 5, 7, 10] -> Option AQuick Check:
Bubble down restores min-heap order [OK]
- Not replacing root with last element before bubble down
- Swapping with wrong child (larger instead of smaller)
- Stopping bubble down too early
void bubbleDown(int[] heap, int index, int size) {
int largest = index;
int left = 2 * index + 1;
int right = 2 * index + 2;
if (left < size && heap[left] > heap[largest]) {
largest = left;
}
if (right < size && heap[right] > heap[largest]) {
largest = right;
}
if (largest != index) {
int temp = heap[index];
heap[index] = heap[largest];
heap[largest] = temp;
bubbleDown(heap, largest, size);
}
}Solution
Step 1: Check child index calculations
Left = 2*index+1 and right = 2*index+2 are correct for binary heap stored in array.Step 2: Verify comparisons and recursion
Comparisons use > for max-heap to find largest child. Recursive call uses largest index to continue bubbling down correctly.Final Answer:
There is no bug; the code correctly performs bubble down -> Option DQuick Check:
Child indices and recursion are correct [OK]
- Confusing child index formulas
- Wrong comparison operators for max-heap
- Incorrect recursive call index
[20, 18, 15, 10, 12, 9, 8]. After extracting the max element and performing bubble down, what is the resulting heap array?Solution
Step 1: Extract max and replace root
Remove 20, replace root with last element 8: [8, 18, 15, 10, 12, 9]Step 2: Bubble down from root
Compare 8 with children 18 and 15; largest is 18. Swap 8 and 18: [18, 8, 15, 10, 12, 9]. Now bubble down at index 1.Step 3: Continue bubble down
At index 1, children are 10 and 12; largest is 12. Swap 8 and 12: [18, 12, 15, 10, 8, 9]. At index 4, children do not exist, stop.Final Answer:
[18, 12, 15, 10, 8, 9] -> Option CQuick Check:
Bubble down swaps with largest child until heap restored [OK]
- Not continuing bubble down after first swap
- Swapping with smaller child by mistake
- Forgetting to update heap size after extraction
Ready to prove it?
Pass the test and this pattern moves unknown
