Data Structures & AlgorithmsVery hardGATE CS · Codeforces Div2 C

A 0-indexed array holds these values: 94 34 64 65 45 41 73 47 4 24 15 99 6. 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. 94 65 99 47 45 64 73 34 4 24 15 41 6
  2. B. 99 65 94 47 45 64 73 34 4 24 15 41 6
  3. C. 94 65 73 47 45 99 64 34 4 24 15 41 6
  4. D. 99 65 94 47 45 73 64 34 4 24 15 41 6

Correct answer: B. 99 65 94 47 45 64 73 34 4 24 15 41 6

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