Complete the code to get the index of the left child in a heap array.
int leftChildIndex = 2 * parentIndex [1] 1;
In a heap stored as an array, the left child of a node at index parentIndex is at 2 * parentIndex + 1.
Complete the code to swap two elements in the heap array.
int temp = heapArray[i]; heapArray[i] = heapArray[[1]]; heapArray[[1]] = temp;
To swap elements at indices i and j, use heapArray[j] in the swap.
Fix the error in the bubble down condition to continue while the child index is valid.
while (childIndex [1] heapSize) { // bubble down logic }
The loop should continue while childIndex is less than heapSize to stay within array bounds.
Fill both blanks to correctly find the smaller child index during bubble down.
if (rightChildIndex < heapSize && heapArray[rightChildIndex] [1] heapArray[leftChildIndex]) { smallerChildIndex = [2]; } else { smallerChildIndex = leftChildIndex; }
We compare right and left child values to find the smaller child. If right child is smaller, assign its index.
Fill all three blanks to complete the bubble down swap and update indices.
int temp = heapArray[[1]]; heapArray[[1]] = heapArray[[2]]; heapArray[[2]] = temp; parentIndex = [3];
Swap values at parentIndex and smallerChildIndex, then update parentIndex to smallerChildIndex for next iteration.
