Data Structures & AlgorithmsVery hardGATE CS · Codeforces Div2 C

A 0-indexed array holds these values: 42 65 28 48 66 35 95 5 92 32 24 46 52 58. 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. 95 92 58 65 66 52 42 5 48 32 24 46 35 28
  2. B. 65 48 46 42 66 35 95 5 92 32 24 28 52 58
  3. C. 65 66 95 92 42 52 58 5 48 32 24 46 35 28
  4. D. 95 92 66 65 48 52 58 5 42 32 24 28 46 35

Correct answer: A. 95 92 58 65 66 52 42 5 48 32 24 46 35 28

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