Question

) True or false: Any two (possibly unbalanced) binary search trees containing n elements each can...

) True or false: Any two (possibly unbalanced) binary search trees containing n elements each can be merged into a single balanced binary search tree in O(n) time.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

False,

Two BST containing n elements in each BST can be merged by the following methods with complexities other that O(n):

1. Insert elements of first tree to second(Method 1):

  In this approach we are required to take elements of 1st BST one by one and insert those into the 2nd BST.

Insertion of these elements take Lon(n) time, where n is the size of BST.

Thus time complexity of this method comes out to be Log(n) + Log(n+1) … Log(m+n-1), implies,

Complexity is between mLogn and mLog(m+n-1).

2. Merge Inorder Traversals (Method 2):

1. Do inorder traversal of the first tree.

2. Store the traversal in one temporary array array1[ ] (This step takes O(m) time).
3. Do inorder traversal of second tree.

4. Store the traversal in another temporary array array2[ ] (This step takes O(n) time).
3. Both the arrays array1 and array2 are sorted array. Now merge these two arrays into one array of size (m+n) (This step takes O(m+n) time).
4) Now create a balanced tree from the merged array thereby taking time of O(m+n).

Result:

If we compare both methods we can see that Inorder traversal takes less time than the first method as O(m+n).

It cannot be solved with O(n) time as two minimum steps are storing and merging thus taking time O(m+n) and O(m+n) respectively.

Add a comment
Know the answer?
Add Answer to:
) True or false: Any two (possibly unbalanced) binary search trees containing n elements each can...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT