Complete the code to find the parent index of a given child index in a heap.
int parentIndex = (childIndex - 1) [1] 2;
The parent index in a heap is calculated by subtracting 1 from the child index and dividing by 2.
Complete the code to swap two elements in the array during heapify.
int temp = heapArray[i]; heapArray[i] = heapArray[[1]]; heapArray[[1]] = temp;
During heapify, we swap the current element at index i with the largest child to maintain the heap property.
Fix the error in the condition to check if left child exists within heap size.
if (leftChild [1] heapSize) { // compare values }
The left child index must be less than the heap size to be valid.
Fill both blanks to complete the loop that builds the heap from the array.
for (int i = [1]; i >= 0; i[2]) { Heapify(heapArray, heapSize, i); }
We start heapifying from the last parent node (heapSize/2 - 1) down to 0, decrementing i each time.
Fill all three blanks to complete the Heapify function signature and recursive call.
void Heapify(int[] heapArray, int [1], int [2]) { int largest = [2]; // rest of heapify logic if (largest != [2]) { Swap(heapArray, [2], largest); Heapify(heapArray, [1], largest); } }
The Heapify function takes the heap size and current index 'i'. We set 'largest' to 'i' initially and recursively call Heapify with 'largest' if a swap occurs.
