What is the purpose of the 'bubble down' operation in a heap after extracting the min or max element?
The 'bubble down' operation restores the heap property by moving the new root element down the tree until it is in the correct position, ensuring the heap remains a valid min-heap or max-heap.
Click to reveal answer
beginner
In a min-heap, after extracting the minimum element, which element replaces the root before bubbling down?
The last element in the heap replaces the root before the bubble down process begins.
Click to reveal answer
intermediate
Describe the condition to continue bubbling down in a max-heap.
In a max-heap, bubbling down continues as long as the current node is smaller than at least one of its children. The node swaps with the larger child to maintain the max-heap property.
Click to reveal answer
intermediate
What is the time complexity of the extract min or max operation including bubble down in a heap of size n?
The time complexity is O(log n) because the bubble down operation moves down the height of the heap, which is proportional to log n.
Click to reveal answer
beginner
Why do we use the last element of the heap to replace the root during extraction?
Using the last element to replace the root keeps the heap complete (all levels fully filled except possibly the last), which is essential for the heap structure.
Click to reveal answer
After extracting the root in a min-heap, what is the first step before bubbling down?
ASwap the root with its left child
BReplace the root with the last element
CRemove the last element
DInsert a new element at the root
✗ Incorrect
The last element replaces the root to maintain the complete tree structure before bubbling down.
In a max-heap, bubbling down swaps the current node with which child?
AThe larger child
BThe right child only
CThe left child only
DThe smaller child
✗ Incorrect
To maintain max-heap property, swap with the larger child if the current node is smaller.
What is the main goal of the bubble down operation?
ATo delete the last element
BTo insert a new element
CTo restore heap property after extraction
DTo sort the heap
✗ Incorrect
Bubble down restores the heap property after the root is replaced during extraction.
What is the height of a heap with n elements?
An
Bn/2
Csqrt(n)
Dlog n
✗ Incorrect
A heap is a complete binary tree, so its height is proportional to log n.
Which element is removed during the extract min or max operation?
AThe root element
BThe last element
CThe leftmost leaf
DThe rightmost leaf
✗ Incorrect
The root element (min or max) is removed during extraction.
Explain step-by-step how the bubble down operation works after extracting the root from a min-heap.
Think about how to keep the smallest element at the root.
You got /4 concepts.
Describe why the heap remains a complete binary tree after extracting the min or max element and bubbling down.
Focus on the shape and order of the heap.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of the bubble down operation in a min-heap after extracting the minimum element?
easy
A. To restore the min-heap property by moving the root element down to its correct position
B. To insert a new element at the bottom of the heap
C. To swap the last element with the root without any further changes
D. To remove the maximum element from the heap
Solution
Step 1: Understand extract-min operation
When the minimum element (root) is removed, the last element is moved to the root position.
Step 2: Apply bubble down to restore heap
The bubble down operation moves this element down the tree to restore the min-heap property by swapping with the smaller child.
Final Answer:
To restore the min-heap property by moving the root element down to its correct position -> Option A
Quick Check:
Bubble down fixes heap after extract-min [OK]
Hint: Bubble down fixes heap after removing root [OK]
Common Mistakes:
Thinking bubble down inserts new elements
Confusing bubble down with bubble up
Assuming bubble down removes max element
2. Which of the following is the correct C++ syntax to swap two elements heap[i] and heap[j] during bubble down?
easy
A. swap(heap[i], heap[j]){}
B. heap[i] = heap[j]; heap[j] = heap[i];
C. swap(heap[i], heap[j]) = true;
D. swap(heap[i], heap[j]);
Solution
Step 1: Recall C++ swap function usage
The standard way to swap two variables in C++ is using swap(a, b); from <algorithm>.
Step 2: Check each option
swap(heap[i], heap[j]); uses correct syntax. heap[i] = heap[j]; heap[j] = heap[i]; wrongly assigns values without temporary storage. Options C and D have invalid syntax.
Hint: Use standard swap function with two variables [OK]
Common Mistakes:
Assigning without temporary variable causes errors
Adding extra symbols after swap call
Using braces {} after swap incorrectly
3. Given the min-heap array [2, 5, 3, 10, 7], after extracting the minimum element and performing bubble down, what is the resulting heap array?
medium
A. [3, 5, 7, 10]
B. [3, 7, 5, 10]
C. [5, 7, 3, 10]
D. [5, 3, 7, 10]
Solution
Step 1: Remove minimum and replace root
Remove 2 (root). Move last element 7 to root: [7, 5, 3, 10]
Step 2: Bubble down 7 to restore min-heap
Compare 7 with children 5 and 3. Smallest child is 3. Swap 7 and 3: [3, 5, 7, 10]. Now 7 has children 10 only, no swap needed.
Final Answer:
[3, 5, 7, 10] -> Option A
Quick Check:
Bubble down swaps root with smallest child [OK]
Hint: Replace root with last, then swap down with smaller child [OK]
Common Mistakes:
Not swapping with smallest child first
Forgetting to replace root with last element
Swapping with larger child by mistake
4. Consider this bubble down code snippet for a max-heap extract operation:
void bubbleDown(vector<int>& heap, int i) {
int left = 2*i + 1;
int right = 2*i + 2;
int largest = i;
if (left < heap.size() && heap[left] > heap[largest])
largest = left;
if (right < heap.size() && heap[right] > heap[largest])
largest = right;
if (largest != i) {
swap(heap[i], heap[largest]);
bubbleDown(heap, largest);
}
}
What is the bug if the heap size is 1 and bubbleDown(heap, 0) is called?
medium
A. Infinite recursion because largest never changes
B. No bug; function handles size 1 correctly
C. Accessing left child index 1 causes out-of-range error
D. Swap called even when largest equals i
Solution
Step 1: Check conditions for size 1 heap
For size 1, left = 1 and right = 2, both >= heap.size(), so conditions fail and largest stays i.
Step 2: Confirm no recursion or swap
Since largest == i, no swap or recursive call happens. Function returns safely.
Final Answer:
No bug; function handles size 1 correctly -> Option B
Quick Check:
Bounds checked before access prevents errors [OK]
Hint: Check index bounds before accessing children [OK]
Common Mistakes:
Assuming out-of-range access without checking
Thinking recursion always happens
Swapping when largest equals current index
5. You have a max-heap represented as [50, 30, 40, 10, 20, 35]. After extracting the maximum element and performing bubble down, what is the final heap array?
hard
A. [40, 35, 30, 10, 20]
B. [40, 30, 20, 10, 35]
C. [40, 30, 35, 10, 20]
D. [35, 30, 40, 10, 20]
Solution
Step 1: Remove max and replace root
Remove 50 (root). Move last element 35 to root: [35, 30, 40, 10, 20]
Step 2: Bubble down 35 to restore max-heap
Compare 35 with children 30 and 40. Largest child is 40. Swap 35 and 40: [40, 30, 35, 10, 20]. Now 35 has children 10 and 20, both smaller, so stop.
Final Answer:
[40, 30, 35, 10, 20] -> Option C
Quick Check:
Bubble down swaps with largest child in max-heap [OK]
Hint: Swap root with largest child until heap property restored [OK]
Common Mistakes:
Swapping with smaller child instead of largest
Not replacing root with last element before bubble down
Stopping bubble down too early
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.