Data Structures & AlgorithmsHardGATE CS · Amazon

Floyd's bottom-up build-heap (sifting down from the last internal node to the root) is applied to the array [7, 2, 9, 4, 6, 1, 8, 3, 5] to form a max-heap. The resulting array is:

  1. A. [9, 6, 8, 5, 2, 1, 7, 3, 4]
  2. B. [9, 6, 8, 5, 4, 1, 7, 2, 3]
  3. C. [9, 7, 8, 4, 6, 1, 2, 3, 5]
  4. D. [9, 6, 7, 5, 4, 1, 8, 2, 3]

Correct answer: A. [9, 6, 8, 5, 2, 1, 7, 3, 4]

Sift down indices 3, 2, 1, 0 in turn: 4 swaps with 5; 9 at index 2 stays; 2 swaps with 6; finally 7 swaps with 9 and then with 8, ending at index 6. The one-by-one insertion method gives a different heap, [9, 6, 8, 5, 4, 1, 7, 2, 3].

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic