Question
Java MinHeap class has a heapifyDown method.
public class MinHeap {   private ArrayList heap;   public MinHeap() {     heap = new ArrayList();   }   // ... other methods like insert, getMin ...   private void heapifyDown(int i) {     int left = 2 * i + 1;     int right = 2 * i + 2;     int smallest = i;     if (left < heap.size() && heap.get(left) < heap.get(smallest)) {       smallest = left;     }     if (right < heap.size() && heap.get(right) < heap.get(smallest)) {       smallest = right;     }     if (smallest != i) {       // Swap heap.get(i) and heap.get(smallest)       int temp = heap.get(i);       heap.set(i, heap.get(smallest));       heap.set(smallest, temp);       // Bug: Missing recursive call     }   } } If heapifyDown is called on a node i where its children are not in heap order, but its grandchild is also out of order, what will be the consequence of the missing recursive call?Solution
• Dry Run: o Consider a min-heap structure where i is the root, left is its left child, right is its right child. o Let heap[i] = 10, heap[left] = 5, heap[right] = 12. o Let heap[2*left + 1] (a grandchild of i) be 2. o Call heapifyDown(i):  smallest is initially i.  heap.get(left) (5) is less than heap.get(smallest) (10), so smallest becomes left.  heap.get(right) (12) is not less than heap.get(smallest) (5).  smallest is left (which is not i), so the swap occurs. heap[i] becomes 5, heap[left] becomes 10.  The method then terminates.  Now, heap[left] is 10, but its child heap[2*left + 1] is 2. The heap property is violated at index left (10 is not less than or equal to 2). The heapifyDown operation is incomplete. • Why Correct Answer (B): Only the immediate children will be correctly placed, but the heap property might still be violated further down the tree. o The element at i will be correctly placed relative to its direct children (left and right). However, the element that was swapped down to smallest might still be larger than its children, meaning the heap property is not fully restored throughout the subtree.
Simplify the following expression: (5x + 7) 2  + (5x – 7) 2 .
- The sum of the present age of 'T' and 'O' is 60 years. The ratio of the age of 'T' and 'O' before 10 years is 3:5 respectively. Find the age of 'O' after 1...
A boat covers a distance of 45 km in still water in 2.5 hours. The ratio of its speed while traveling upstream to its speed while traveling downstream i...
- If x = 6 and y = -2, then find the value of (4x 2  – 3y 3  + 2y 4  – x 3 )
Three bells ring at intervals of 28 seconds, 60 seconds, and 84 seconds, respectively. How many times will all three bells ring together in a span of 10...
Vikas invested Rs. 12,000 at an annual compound interest rate of 30%, with the interest being compounded every 10 months. How much interest did Vikas ea...
- Ram has Rs. 1000 in denomination of Rs. 2, Rs. 5 and Rs. 10 only. If the ratio of number of coins of Rs. 2, Rs. 5 and Rs. 10 is 5:3:10 respectively. Find t...
If in the number 86548246, 3 is subtracted to first four digit of the number and 1 is added to rest of the digits then how many digits are repeating in ...
'A' and 'B' together build a wall in 48 days. If 'A' builds the wall in 120 days, then find the time taken by 'B' to build the wall.
A boat has a speed of 30 km/h in still water. In a particular stream, the boat's downstream speed is 50% faster than its speed upstream. What is the spe...