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