Data Structures & AlgorithmsHardGATE CS · Codeforces Div 2

Array [22, 29, 23, 18, 14, 20, 10] is turned into a min-heap in place with bottom-up heapify: for i from ⌊n/2⌋−1 down to 0, sift the element at index i down (swap with its smaller child while a child is smaller). What is the resulting array?

  1. A. [10, 14, 20, 18, 29, 22, 23]
  2. B. [22, 29, 23, 18, 14, 20, 10]
  3. C. [10, 14, 18, 20, 22, 23, 29]
  4. D. [10, 18, 14, 29, 22, 23, 20]

Correct answer: A. [10, 14, 20, 18, 29, 22, 23]

Bottom-up heapify on [22, 29, 23, 18, 14, 20, 10] produces [10, 14, 20, 18, 29, 22, 23]. Inserting one element at a time gives a different heap, and a single sift-down from the root does not fix the internal nodes.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic