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: A
To find the level-order traversal after inserting the elements, we can trace the operations step-by-step on the Max-Heap.
Step 1: Initial Max-Heap
The initial level-order traversal is . Represented as a complete binary tree:
10
/ \
8 5
/ \
3 2
Step 2: Insert
We insert at the next available position (the left child of ):
10
/ \
8 5
/ \ /
3 2 1
Since the parent node , the max-heap property is satisfied. No swaps are needed.
Step 3: Insert
We insert at the next available position (the right child of ):
10
/ \
8 5
/ \ / \
3 2 1 7
Since the parent node , we swap with (heapify-up):
10
/ \
8 7
/ \ / \
3 2 1 5
Now, we compare with its new parent . Since , the heapify-up process terminates.
Conclusion
The final level-order traversal of the heap is obtained by reading the nodes level-by-level from left to right:
This matches Option A.
Free practice · Your answers on this page are not saved.
