Data Structures & AlgorithmsVery hardGATE CS · Codeforces Div2 C

A 0-indexed array holds these values: 56 58 94 18 5 33 71 35 90 26 61 85 97 62. 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. 58 56 94 35 26 85 71 18 90 5 61 33 97 62
  2. B. 94 58 71 90 61 97 62 35 18 26 5 85 33 56
  3. C. 97 90 94 56 61 85 62 18 35 5 26 33 71 58
  4. D. 97 90 94 58 61 85 71 35 18 26 5 56 33 62

Correct answer: D. 97 90 94 58 61 85 71 35 18 26 5 56 33 62

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