Data Structures & AlgorithmsHardTCS NQT · Infosys pattern

A complete binary tree is stored in an array with the root at index 1. A node at index 37 – what are its parent, left child and right child indices, and at which depth (root = 0) is it?

  1. A. Parent 18, children 75 and 76, depth 5
  2. B. Parent 19, children 74 and 75, depth 6
  3. C. Parent 18, children 74 and 75, depth 5
  4. D. Parent 18, children 74 and 75, depth 6

Correct answer: C. Parent 18, children 74 and 75, depth 5

With 1-based indexing, parent = ⌊i/2⌋ = 18, children 2i = 74 and 2i + 1 = 75, and depth = ⌊log₂ i⌋ = 5 (indices 32–63 are on depth 5). With 0-based indexing the formulas shift to (i − 1)/2, 2i + 1 and 2i + 2 – mixing the two conventions is the usual error.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic