GATE Computer ScienceVery hardGATE CS

A segment tree over positions 0 to 12 (13 elements) is built recursively: the root is node 1, the node for [lo, hi] splits at mid = (lo + hi)/2 (integer division) and its children are 2i and 2i + 1. What is the largest node index used?

  1. A. 26
  2. B. 25
  3. C. 29
  4. D. 31

Correct answer: C. 29

A tree over 13 leaves has 2·13 − 1 = 25 nodes, but the recursive numbering leaves gaps because the tree is not perfectly balanced. The largest index belongs to position 11, reached by 1 → 3 → 7 → 14 → 29, so an array of size 30 is needed, whereas the textbook bound 2^(⌈log₂13⌉+1) − 1 = 31 is only an upper bound.

More Programming and Data Structures (GATE) questions

Exams that ask this topic