Data Structures & AlgorithmsVery hardGATE CS · Codeforces Div2 C

A 0-indexed array holds these values: 33 28 86 90 92 95 23 32 9 8 26 73. 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. 90 33 95 32 92 86 23 28 9 8 26 73
  2. B. 86 92 95 90 28 73 23 32 9 8 26 33
  3. C. 95 90 92 32 86 73 23 28 9 8 26 33
  4. D. 95 92 86 90 28 73 23 32 9 8 26 33

Correct answer: D. 95 92 86 90 28 73 23 32 9 8 26 33

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