All problems
Binary Heap Parent and Child Indices
Problem
Return the parent and child indices of a node in an array-based binary heap.
For a zero-based index i, the parent is (i - 1) // 2 (or -1 for the root), the left child is 2*i + 1, and the right child is 2*i + 2.
Return [parent, left, right].
Hint
Children of i live at 2*i + 1 and 2*i + 2; the parent is their inverse.
Test cases
case 1
in: [0]
expected: [-1,1,2]
case 2
in: [1]
expected: [0,3,4]
case 3
in: [3]
expected: [1,7,8]
case 4
in: [6]
expected: [2,13,14]
Starter codePython
def heap_indices(i):
# Your code here
pass4 test cases · run your code against them in the editor