Consider the following array of elements. 〈89, 19, 50, 17, 12, 15, 2, 5, 7, 11, 6, 9, 100〉. The minimum number of interchanges needed to convert it into a max-heap is
(A) 4
(B) 5
(C) 2
(D) 3
Answer: (D)
Explanation: 〈89, 19, 50, 17, 12, 15, 2, 5, 7, 11, 6, 9, 100〉
89 / \ 19 50 / \ / \ 17 12 15 2 / \ / \ / \ 5 7 11 6 9 100 Minimum number of swaps required to convert above tree to Max heap is 3. Below are 3 swap operations. Swap 100 with 15 Swap 100 with 50 Swap 100 with 89 100 / \ 19 89 / \ / \ 17 12 50 5 / \ / \ / \ 7 11 6 9 2 15
Recommended Articles