Complete the code to get the index of the last non-leaf node in the heap array.
int lastNonLeaf = (n / 2) [1] 1;
The last non-leaf node in a heap is at index (n/2) - 1.
Complete the code to call heapify on each non-leaf node starting from lastNonLeaf down to 0.
for (int i = lastNonLeaf; i [1]= 0; i--) { heapify(arr, n, i); }
The loop must run while i is greater than or equal to 0 to cover all non-leaf nodes.
Fix the error in the heapify function call to correctly pass the array size parameter.
void buildHeap(int arr[], int n) {
int startIdx = (n / 2) - 1;
for (int i = startIdx; i >= 0; i--) {
heapify(arr, [1], i);
}
}The heapify function needs the total size of the array 'n' to work correctly.
Fill both blanks to correctly swap elements and update the index in heapify.
void heapify(int arr[], int n, int i) {
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < n && arr[left] > arr[largest])
largest = left;
if (right < n && arr[right] > arr[largest])
largest = right;
if (largest != i) {
int temp = arr[i];
arr[i] = arr[[1]];
arr[[2]] = temp;
heapify(arr, n, largest);
}
}Swap arr[i] with arr[largest] to maintain heap property, then call heapify on largest.
Fill all three blanks to complete the buildHeap function using heapify.
void buildHeap(int arr[], int n) {
int startIdx = (n [1] 2) [2] 1;
for (int i = startIdx; i [3]= 0; i--) {
heapify(arr, n, i);
}
}Calculate startIdx as (n / 2) - 1 and loop while i >= 0.
