Complete the code to get the root element of a max heap stored in a slice.
func getRoot(heap []int) int {
return heap[[1]]
}The root of a heap stored in a slice is always at index 0.
Complete the code to find the left child index of a node at index i in a heap slice.
func leftChild(i int) int {
return [1]
}In a heap stored as a slice, the left child of node at index i is at 2*i + 1.
Fix the error in the code to correctly find the parent index of a node at index i in a heap slice.
func parent(i int) int {
return (i - [1]) / 2
}The parent index of node i is (i - 1) / 2 in zero-indexed heap arrays.
Fill both blanks to complete the code that checks if a node at index i has a right child in the heap slice.
func hasRightChild(heap []int, i int) bool {
return [1] < len(heap) && heap[[2]] != 0
}The right child index is 2*i + 2. We check if this index is within bounds and the element is not zero.
Fill all three blanks to complete the code that builds a max heap property check for a node at index i.
func isMaxHeapNode(heap []int, i int) bool {
left := [1]
right := [2]
return (left >= len(heap) || heap[i] >= heap[left]) && (right >= len(heap) || heap[i] >= heap[[3]])
}Left child index is 2*i + 1, right child index is 2*i + 2. The last blank uses the variable 'right' to compare values.