Data Structures & AlgorithmsModerateCognizant · Capgemini pattern

The maximum is deleted from the max-heap [50, 30, 40, 10, 20, 35]: the last element replaces the root and is sifted down. What is the heap afterwards?

  1. A. 40 30 20 10 35
  2. B. 35 30 40 10 20
  3. C. 40 35 30 10 20
  4. D. 40 30 35 10 20

Correct answer: D. 40 30 35 10 20

35 moves to the root: 35 30 40 10 20. Its larger child 40 is bigger, so they swap: 40 30 35 10 20, and 35 now has no children. Swapping with the left child by habit gives an invalid heap.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic