A meld operation on two instances of a data structure combines them into one single instance of the same data structure. Consider the following data structures:
P. Unsorted doubly linked list with pointers to the head node and tail node of the list.
Q. Min-heap implemented using an array.
R. Binary Search Tree.
Which ONE of the following options gives the worst-case time complexities for meld operation on instances of size of these data structures?
View worked solution
Answer: A
To find the worst-case time complexity for the meld operation on each of the given data structures of size :
P. Unsorted Doubly Linked List (with Head and Tail Pointers)
To meld two unsorted doubly linked lists, and :
- Connect the tail node of to the head node of .
- Update the tail pointer of the combined list to point to the tail of .
Since we have direct pointers to the head and tail of both lists, this operation requires only a constant number of pointer updates, which takes time.
Q. Min-heap (implemented using an array)
To meld two min-heaps of size represented as arrays:
- Concatenate the two arrays of size into a single array of size . This takes time.
- Apply the bottom-up heap construction algorithm (
Build-Heap) on the combined array of size .
The time complexity of Build-Heap on an array of size is linear, i.e., . Here, , so the heapification takes time. Thus, the overall worst-case time complexity is .
R. Binary Search Tree (BST)
To meld two BSTs of size optimally:
- Perform an in-order traversal of both BSTs to produce two sorted arrays (or lists) of size . This takes time.
- Merge the two sorted arrays of size into a single sorted array of size . This takes time.
- Reconstruct a balanced BST from the merged sorted array. This can be done in time.
Thus, the overall worst-case time complexity to meld two BSTs is .
Comparing the results:
This corresponds to Option A.
Free practice · Your answers on this page are not saved.
