Data Structures & AlgorithmsHardGATE CS · Codeforces Div2 C

A 0-indexed array holds these values: 29 38 92 5 26 65 27 7 75 51 50. A max-heap is built in place with Floyd's method: for i from n/2 − 1 down to 0, sift the element at index i down (swap with the larger child while it is smaller than that child). What is the array after building?

  1. A. 38 29 92 7 51 65 27 5 75 26 50
  2. B. 92 38 65 75 51 29 27 7 5 26 50
  3. C. 92 75 65 38 51 29 27 7 5 26 50
  4. D. 92 75 65 29 51 38 27 5 7 26 50

Correct answer: C. 92 75 65 38 51 29 27 7 5 26 50

Floyd's method sifts down from the last internal node to the root, so each subtree is already a heap when its root is processed. Always choosing the left child, going top-down, or inserting one by one give other arrays.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic