Machine-translated by Google. Questions are authored in English.
Top

Discussion

The minimum number of interchanges needed to convert the array 89,19,40,17,12,10,2,5,7,11,6,9,70 into a heap with maximum element at the root is

  • A.1
  • B.2
  • C.4
  • D.None of these

Answer: B

Correct answer: 2

Build the max-heap bottom-up. Numbering the array from 1:

1:89  2:19  3:40  4:17  5:12  6:10  7:2  8:5  9:7  10:11  11:6  12:9  13:70

  • Node 6 holds 10 with children 9 and 70 - swap with 70.
  • Nodes 5 and 4 are already correct (12 over 11 and 6; 17 over 5 and 7).
  • Node 3 holds 40 with children 70 and 2 - swap. The 40 that sinks to node 6 still beats its children, 9 and 10.
  • Node 2 (19 over 17 and 12) and the root (89 over 19 and 70) need nothing.

Two interchanges suffice, so the answer is 2.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.