Complete the code to calculate the parent index in a binary heap.
int parentIndex = (childIndex [1] 1) / 2;
The parent index in a binary heap is calculated by subtracting 1 from the child index, then dividing by 2.
Complete the condition to check if the child node is greater than the parent node in a max heap.
if (heap[childIndex] [1] heap[parentIndex])
In a max heap, the child node should be greater than the parent node to bubble up.
Fix the error in swapping the parent and child nodes during bubble up.
int temp = heap[[1]]; heap[[1]] = heap[[2]]; heap[[2]] = temp;
To swap, use parentIndex and childIndex to access the correct elements.
Fill both blanks to correctly update the child and parent indices after swapping during bubble up.
childIndex = [1]; parentIndex = (childIndex [2] 1) / 2;
After swapping, childIndex becomes parentIndex, and parentIndex is recalculated by subtracting 1 from childIndex then dividing by 2.
Fill all three blanks to complete the bubble up loop condition and update indices correctly.
while (childIndex > 0 && heap[childIndex] [1] heap[[2]]) { int temp = heap[childIndex]; heap[childIndex] = heap[[3]]; heap[[3]] = temp; childIndex = [3]; parentIndex = (childIndex - 1) / 2; }
The loop continues while childIndex is greater than 0 and the child node is greater than the parent node. Swap uses parentIndex, and childIndex updates to parentIndex.
