GATE Computer ScienceEasyGATE CS (PYQ pattern)

A 3-ary min-heap stores 1000 keys in an array A[0..999], with the children of A[i] at 3i + 1, 3i + 2 and 3i + 3. How many of the keys are at leaves?

  1. A. 666
  2. B. 667
  3. C. 333
  4. D. 750

Correct answer: B. 667

A[i] is internal iff 3i + 1 ≤ 999, i.e. i ≤ 332, giving 333 internal nodes. The other 1000 − 333 = 667 are leaves. A[332] is the last internal node (children A[997]–A[999]); the off-by-one 666 comes from taking 334 internal nodes.

More Algorithms questions

Exams that ask this topic