A priority queue is implemented as a Max-Heap. Initially, it has elements. The level-order traversal of the heap is: . Two new elements and are inserted into the heap in that order. The level-order traversal of the heap after the insertion of the elements is:
View worked solution
Answer: D
To find the level-order traversal after inserting the elements, we perform the standard binary heap insertion operations step-by-step.
Step 1: Initial Max-Heap
The initial level-order traversal is 10, 8, 5, 3, 2. Represented as a complete binary tree, it looks like this:
10
/ \
8 5
/ \
3 2
Step 2: Insert 1
- Insert
1at the next available position in the complete binary tree (as the left child of5):10 / \ 8 5 / \ / 3 2 1 - Compare
1with its parent5. Since , the max-heap property is satisfied, and no swap is required.
Step 3: Insert 7
- Insert
7at the next available position (as the right child of5):10 / \ 8 5 / \ / \ 3 2 1 7 - Compare
7with its parent5. Since , we swap them to restore the max-heap property:10 / \ 8 7 / \ / \ 3 2 1 5 - Compare
7with its new parent10. Since , the heap property is satisfied, and no further swaps are needed.
Conclusion
The final level-order traversal of the heap is obtained by reading the tree level-by-level from left to right:
This matches Option D.
Free practice · Your answers on this page are not saved.
