Complete the code to get the index of the left child in a heap array.
int leftChild(int i) {
return [1];
}2 * i - 1 instead of 2 * i + 1.The left child of a node at index i in a heap is at 2 * i + 1.
Complete the code to swap two elements in the heap array.
void swap(int *a, int *b) {
int temp = *a;
*a = [1];
*b = temp;
}*a = temp which causes no change.To swap, assign *a the value of *b.
Fix the error in the bubble down loop condition to avoid infinite loop.
while (smallest != i && smallest < [1]) { swap(&heap[i], &heap[smallest]); i = smallest; smallest = leftChild(i); if (rightChild(i) < size && heap[rightChild(i)] < heap[smallest]) { smallest = rightChild(i); } }
The loop must check that smallest is less than the heap size to stay within bounds.
Fill both blanks to correctly update the smallest child index during bubble down.
int smallest = [1](i); if ([2](i) < size && heap[rightChild(i)] < heap[smallest]) { smallest = rightChild(i); }
Start with left child index, then check right child to find the smallest.
Fill all three blanks to complete the extract min function with bubble down.
int extractMin(int heap[], int *size) {
if (*size <= 0) return -1;
int root = heap[0];
heap[0] = heap[[1]];
(*size)--;
int i = 0;
int smallest = [2](i);
while (smallest < *size && heap[smallest] < heap[i]) {
swap(&heap[i], &heap[smallest]);
i = smallest;
smallest = [3](i);
}
return root;
}size instead of *size causes wrong indexing.Replace last element at index *size, then bubble down starting from left child.
