GATE Computer ScienceHardGATE CS (PYQ pattern)

A heap with 2000 elements is stored in an array (complete binary tree, last level filled left to right). How many elements are in the left subtree of the root, and how many in the right subtree?

  1. A. 999 and 1000
  2. B. 1023 and 976
  3. C. 1022 and 977
  4. D. 976 and 1023

Correct answer: B. 1023 and 976

Levels 0 to 9 hold 1023 elements and the last level (depth 10) holds 977. The left subtree has 511 elements on depths 1 to 9 plus the first 512 slots of the last level = 1023; the right subtree has 511 plus the remaining 977 - 512 = 465 = 976.

More Programming and Data Structures (GATE) questions

Exams that ask this topic