A priority queue is implemented as a Max-Heap. Initially, it has 5 elements. The level-order traversal of the heap is: 10, 8, 5, 3, 2. Two new elements 1 and 7 are inserted into the heap in that order. The level-order traversal of the heap after the insertion of the elements is:
(A)
10, 8, 7, 3, 2, 1, 5
(B)
10, 8, 7, 2, 3, 1, 5
(C)
10, 8, 7, 1, 2, 3, 5
(D)
10, 8, 7, 5, 3, 2, 1
Answer: (A)
Explanation:
Initially heap has 10, 8, 5, 3, 2 10 / \\ 8 5 / \\ 3 2 After insertion of 1 10 / \\ 8 5 / \\ / 3 2 1 No need to heapify as 5 is greater than 1. After insertion of 7 10 / \\ 8 5 / \\ / \\ 3 2 1 7 Heapify 5 as 7 is greater than 5 10 / \\ 8 7 / \\ / \\ 3 2 1 5 No need to heapify any further as 10 is greater than 7
Hence Option(A) is the correct answer.
Quiz of this Question
Please comment below if you find anything wrong in the above post