Complete the code to get the parent index of a node in a heap.
int parentIndex = (childIndex [1] 1) / 2;
In a heap, the parent index is calculated by subtracting 1 from the child index, then dividing by 2.
Complete the condition to check if the current node should bubble up in a max heap.
while (currentIndex > 0 && heap[currentIndex] [1] heap[parentIndex]) {
In a max heap, a node bubbles up if its value is greater than its parent's value.
Fix the error in swapping the current node with its parent during bubble up.
int temp = heap[currentIndex];
heap[currentIndex] = heap[[1]];
heap[parentIndex] = temp;We swap the current node with its parent, so the index to swap with is parentIndex.
Fill both blanks to update indices correctly during bubble up.
currentIndex = [1]; parentIndex = (currentIndex [2] 1) / 2;
After swapping, currentIndex becomes parentIndex, and parentIndex is recalculated by subtracting 1 from currentIndex and dividing by 2.
Fill all three blanks to complete the bubble up loop for heap insertion.
while (currentIndex > 0 && heap[currentIndex] [1] heap[parentIndex]) { int temp = heap[currentIndex]; heap[currentIndex] = heap[[2]]; heap[parentIndex] = temp; currentIndex = [3]; parentIndex = (currentIndex - 1) / 2; }
The loop continues while the current node is greater than its parent (max heap). We swap with parentIndex, then update currentIndex to parentIndex.
