GATE Computer ScienceModerateGATE CS (PYQ pattern)

In a min-heap stored 1-indexed in an array with children of i at 2i and 2i+1, a node at index 77 is replaced by a smaller key and sifted up to the root. How many comparisons does the sift-up use at most, and which indices does the key pass through?

  1. A. 7 comparisons: indices 77, 38, 19, 9, 4, 2, 1
  2. B. 6 comparisons: indices 77, 39, 19, 9, 4, 2, 1
  3. C. 6 comparisons: indices 77, 38, 19, 9, 4, 2, 1
  4. D. 5 comparisons: indices 77, 38, 19, 9, 4, 2

Correct answer: C. 6 comparisons: indices 77, 38, 19, 9, 4, 2, 1

Parents are found by integer division by 2: 77, 38, 19, 9, 4, 2, 1. That is 6 moves up, hence at most 6 comparisons with parents.

More Programming and Data Structures (GATE) questions

Exams that ask this topic