) 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.
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.
) True or false: Any two (possibly unbalanced) binary search trees containing n elements each can...
Suppose you have two binary search trees P and Q. Let P and Q be the number of elements inP and Q, and let hp and ho be the heights of P and Q. Assume that that is, hp ho < P IQ and A. Give a destructive algorithm for creating a binary search tree containing the union PUQ that runs in time O(|P2) in the worst case. B. Assume now that it is known that the largest element of...
a. How can I show that any node of a binary search tree of n nodes can be made the root in at most n − 1 rotations? b. using a, how can I show that any binary search tree can be balanced with at most O(n log n) rotations (“balanced” here means that the lengths of any two paths from root to leaf differ by at most 1)?
C++ Vectors and Binary Search Trees • Write a program that takes from the user n integers and stores them a vector of int. Then, create a function insert After that takes first Value and second Value. This function searches for each occurrence of first Value in the vector and insert the second Value after it in the same vector. The first and second values are taken from the user. • Create another function that creates a Binary Search Tree...
I need question 9-10 answered. Thank you
Question 1 iShow the resulting binary search tree if we are to insert following elements into the tree in given order, [34, 12, 23, 27,31,9,11,45, 20, 37. i) Show the resulting balanced binary search tree if we are to insert following sorted elements into the tree, [9,12,21, 23, 29, 31, 34, 45, 48, 52, 55] iii What is the pre-order traversal of the balanced binary search tree? v) What is the post-order traversal...
why are RB and AVL trees considered balanced binary search trees? give concrete examples for each tree type
Show that any binary search tree with n nodes can be transformed into any other search tree using O(n) rotations. Also show that you need at most n - 1 right rotations to transform a tree into a chain.
Question 4 (1 point) How many binary search trees can you make from the three elements e1, e2 and e3 assuming each tree maintains the ordering el 〈 e2 〈 e3 ? 14
For the set of keys [37, 24, 29, 66, 17, 82, 43], draw binary search trees of height 2, 4, and 6. Argue that since sorting n elements takes Ω(n log n) time in the worst case in the comparison model, any comparison‐based algorithm for constructing a binary search tree from an arbitrary list of n elements takes Ω(n log n) time in the worst case. When node z in TREE‐DELETE has two children, we could choose node y as...
Binary Search Trees can be used to implement Maps. Discuss the pros and cons of this in terms of performance. Are there any unique benefits of using a Binary Search Tree to implement a map?
Draw a maximally balanced binary search tree that can be produced from the elements: 1, 2, 3, 4, 5, 6, 7, 8, 9. Hint: a maximally balanced binary search tree minimises the average depth of its elements.