We have a binary heap on elements and wish to insert more elements (not necessarily one after another) into this heap. The total time required for this is
View worked solution
Answer: B
To insert additional elements into an existing binary heap of size , we have two main approaches:
- One-by-one Insertion: If we insert the elements one after another, each insertion takes time, where is the heap size. For insertions, the total time complexity would be:
-
Batch Insertion (Build-Heap):
Since we do not need to maintain the heap property after each individual insertion (as indicated by "not necessarily one after another"), we can:
- Append the new elements to the end of the existing heap array in time.
- Run the standard bottom-up
Build-Heapalgorithm on the combined array of size .
Since we seek the most efficient method to achieve the final heap, the optimal total time required is .
Free practice · Your answers on this page are not saved.
