A priority queue is implemented as a max-heap. Initially, it has five…

2016

A priority queue is implemented as a max-heap. Initially, it has five elements. The level order traversal of the heap is as follows :

20, 18, 15, 13, 12

Two new elements ‘10’ and ‘17’ are inserted in the heap in that order. The level-order traversal of the heap after the insertion of the element is :

  1. A.

    20, 18, 17, 15, 13, 12, 10

  2. B.

    20, 18, 17, 12, 13, 10, 15

  3. C.

    20, 18, 17, 10, 12, 13, 15

  4. D.

    20, 18, 17, 13, 12, 10, 15

Attempted by 301 students.

Show answer & explanation

Correct answer: D

Final level-order traversal: 20, 18, 17, 13, 12, 10, 15

  1. Start with the given max-heap (level-order): 20, 18, 15, 13, 12.

  2. Insert 10: place it at the next available leaf. Heap becomes 20, 18, 15, 13, 12, 10. No swaps are needed because the parent (15) is greater than 10.

  3. Insert 17: place it at the next leaf, giving 20, 18, 15, 13, 12, 10, 17. Compare 17 with its parent 15; since 17 > 15, swap them.

  4. After the swap the heap is 20, 18, 17, 13, 12, 10, 15. The parent of 17 is now 20, which is larger, so no further swaps are needed.

  5. Therefore the final level-order traversal is 20, 18, 17, 13, 12, 10, 15.

Explore the full course: Coding For Placement