GATE Computer ScienceVery hardGATE CS (PYQ pattern)

Build-heap (bottom-up, max-heap) is run on [3, 9, 2, 1, 4, 5, 8, 7, 6, 10, 12, 11]. In each sift-down step the larger of the element and its existing children is found with one comparison per child present (against the current maximum). How many key comparisons are made in total during the build?

  1. A. 22
  2. B. 13
  3. C. 29
  4. D. 18

Correct answer: D. 18

Process indices 5 down to 0; each sift-down level costs one comparison per existing child. Counting every level visited gives 18.

More Programming and Data Structures (GATE) questions

Exams that ask this topic