Skip to content
DeepForgeby svx
All problems
EasyData Structuresds-176

D-ary Heap Parent and Child Indices

Problem

Return the parent and children indices of a node in a d-ary heap stored as an array.

Every node has exactly d children. For a zero-based index i the parent is (i - 1) // d (or -1 for the root), the first child is d*i + 1, and the last child is d*i + d.

Return [parent, first_child, last_child].

Hint

The children of i occupy the contiguous block from d*i + 1 through d*i + d.

Test cases

case 1
in: [2,0]
expected: [-1,1,2]
case 2
in: [2,3]
expected: [1,7,8]
case 3
in: [3,4]
expected: [1,13,15]
case 4
in: [4,2]
expected: [0,9,12]
Starter codePython
def dary_heap_indices(d, i):
    # Your code here
    pass

4 test cases · run your code against them in the editor