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?

A The heap will become a max-heap instead of a min-heap.
B Only the immediate children will be correctly placed, but the heap property might still be violated further down the tree.
C The method will cause an IndexOutOfBoundsException.
D The smallest variable will always remain i.
E The heap will always remain sorted.
Practice Next

Hey! Ask a query