GATE Computer ScienceVery hardGATE CS (PYQ pattern)

With the Sethi–Ullman labelling (a left leaf is 1, a right leaf is 0 because it can be a memory operand; an interior node is max(l1, l2) if they differ, else l1 + 1), how many registers are needed to evaluate the sum of 16 operands written as a perfectly balanced tree ((a1 + a2) + (a3 + a4)) + …, and as the left-deep chain (((a1 + a2) + a3) + …) + a16?

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

Correct answer: C. 4 and 1

Balanced: a pair (left leaf 1, right leaf 0) is 1; two subtrees labelled k combine to k + 1, so 2-leaf → 1, 4 → 2, 8 → 3, 16 → 4. Left-deep chain: every right child is a leaf (0), so each node is max(1, 0) = 1: one register suffices. (Labelling every leaf 1 gives 5 for the balanced tree.)

More Compiler Design questions

Exams that ask this topic