GATE | GATE-CS-2005 | Question 34
A Priority-Queue is implemented as a Max-Heap. Initially, it has 5 elements. The level-order traversal of the heap is given below: 10, 8, 5, 3, 2 Two new elements ”1‘ and ”7‘ are inserted in the heap in that order. The level-order traversal of the heap after the insertion of the elements is:
(A)
10, 8, 7, 5, 3, 2, 1
(B)
10, 8, 7, 2, 3, 1, 5
(C)
10, 8, 7, 1, 2, 3, 5
(D)
10, 8, 7, 3, 2, 1, 5
Answer: (D)
Explanation:
The correct answer is (D), 10, 8, 7, 3, 2, 1, 5.
The level-order traversal of a max-heap is a traversal of the heap where the elements are visited in level order, from left to right. In a max-heap, the parent node must always be greater than or equal to its children.
The initial level-order traversal of the heap is 10, 8, 5, 3, 2. When the elements 1 and 7 are inserted, they are placed at the bottom of the heap, in the rightmost positions. The heap is then re-heapified, which means that the elements are swapped with their parents if necessary to maintain the max-heap property.
After the heap is re-heapified, the level-order traversal of the heap is 10, 8, 7, 3, 2, 1, 5.
Here is a diagram of the heap after the elements are inserted:
10
/ \
8 7
/ \ / \
5 3 2 1
https://www.geeksforgeeks.org/data-structures-and-algorithms-set-22/
See question 4 of
Quiz of this Question
Quiz of this Question
Please comment below if you find anything wrong in the above post
Last Updated :
28 Jun, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...