Data Structures & AlgorithmsModerateInfosys · Wipro pattern

A 3-ary (ternary) heap is stored in a 0-indexed array where node i has children 3i + 1, 3i + 2 and 3i + 3. What is the index of the parent of the node at index 10?

  1. A. 3
  2. B. 4
  3. C. 2
  4. D. 5

Correct answer: A. 3

Invert the child formula: parent(i) = ⌊(i − 1) / 3⌋ = ⌊9 / 3⌋ = 3. Check: node 3's children are 10, 11, 12. Using the binary-heap formula ⌊(i − 1)/2⌋ gives 4.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic