Complete the code to find the parent index of a node in a heap.
parent := (index - 1) [1] 2
The parent index in a heap is calculated by subtracting 1 from the current index and dividing by 2.
Complete the code to swap two elements in the heap array.
heap[i], heap[j] = heap[[1]], heap[i]To swap elements at positions i and j, assign heap[i] to heap[j] and heap[j] to heap[i].
Fix the error in the condition to check if left child exists in heapify.
if left < [1] {
The left child index must be less than the length of the heap array to exist.
Fill both blanks to complete the heapify function's recursive call and comparison.
if right < len(heap) && heap[right] [1] heap[largest] { largest = [2] }
We check if the right child is greater than the current largest, then update largest to right.
Complete the code to complete the buildHeap function that heapifies from bottom up.
for i := len(heap)/2 - 1; i >= 0; i[1] { heapify(heap, i, len(heap)) } func heapify(heap []int, i, n int) { largest := i left := 2*i + 1 right := 2*i + 2 if left < n && heap[left] > heap[largest] { largest = left } if right < n && heap[right] > heap[largest] { largest = right } if largest != i { heap[i], heap[largest] = heap[largest], heap[i] heapify(heap, largest, n) } }
The loop decreases i by 1 each time (--). The heapify function needs the size n, so len(heap) + 0 is just len(heap). The closing parenthesis is needed to close the function call.