GATE Computer ScienceHardGATE CS (PYQ pattern)

A 3-ary max-heap with 100 elements is stored in a 0-indexed array (the children of i are 3i + 1, 3i + 2, 3i + 3). How many leaves does it have, and how many children does the last internal node have?

  1. A. 66 leaves, 2 children
  2. B. 67 leaves, 3 children
  3. C. 67 leaves, 1 child
  4. D. 50 leaves, 3 children

Correct answer: B. 67 leaves, 3 children

The parent of the last index 99 is floor((99 - 1) / 3) = 32, so indices 0 to 32 are internal and the other 100 - 33 = 67 are leaves. Node 32's children are 97, 98 and 99, all present, so it has 3 children (the binary-heap habit of n/2 gives 50).

More Programming and Data Structures (GATE) questions

Exams that ask this topic