Data Structures & AlgorithmsVery hardGATE CS · Codeforces Div2 C

A 0-indexed array holds these values: 19 76 92 63 7 44 54 81 90 11 74 93 20 82 31. 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. 81 76 93 63 11 92 82 19 90 7 74 44 20 54 31
  2. B. 92 76 82 90 74 93 54 81 63 11 7 44 20 19 31
  3. C. 93 90 92 81 74 44 82 76 63 11 7 19 20 54 31
  4. D. 93 90 92 81 74 76 82 19 63 7 11 44 20 54 31

Correct answer: C. 93 90 92 81 74 44 82 76 63 11 7 19 20 54 31

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