Data Structures & AlgorithmsHardInfosys SP · TCS Digital

The array [3, 9, 2, 1, 4, 5, 8] is converted to a max-heap by Floyd's bottom-up method, sifting down from the last parent to the root. What is the final array?

  1. A. 9 4 8 1 3 2 5
  2. B. 9 4 8 1 3 5 2
  3. C. 9 8 5 4 3 2 1
  4. D. 9 3 8 1 4 5 2

Correct answer: B. 9 4 8 1 3 5 2

Sift down index 2 (2 vs 5, 8): swap with 8; index 1 (9) stays; root 3 goes below 9, then swaps with 4 to give 9 4 8 1 3 5 2. Inserting the keys one by one instead would give 9 4 8 1 3 2 5.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic