Data Structures & AlgorithmsHardGATE CS · Codeforces Div2 C

A 0-indexed array holds these values: 8 65 35 58 99 30 60 37 10 76. 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. 99 76 60 58 65 30 35 37 10 8
  2. B. 65 58 35 37 99 30 60 8 10 76
  3. C. 65 99 60 58 76 30 35 37 10 8
  4. D. 99 76 60 37 65 30 35 8 10 58

Correct answer: A. 99 76 60 58 65 30 35 37 10 8

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