Question

Let T1 and T2 be AVL trees such that the largest key in T1 is less...

Let T1 and T2 be AVL trees such that the largest key in T1 is less than

the smallest key in T2. Give an algorithm (in psuedocode) for procedure JOIN-AVL-
TREES(T1, T2) that joins these two AVL trees. The runtime should be O(log n),

where n is the size of the resulting AVL tree. Can you explain how can we achieve this and why is the complexity such that?

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

We will begin by computing the heights ,H1 of T1 and H2 of T2. this will be taking time as O(H1+H2): we have to simply traverse the path begin from root , if balance factor is -1 than go towards left child , if balance factor is positive than go towards right child and if the balance factor is 0 than can go to any of the children until you reach leaf node.

Assume that H1>=H2 and the other cases are symmetric.

Next Step, DELETE the smallest element (let X) from T2 leaving T2' of Height H , this will take O(H2) time complexity.

find a node V on the rightmost path from the root of T1 , whose height is either H or H+1 , this can be illustrated as follows:

V<--root(T1)

H'<--H1

while H'>H+1 do

if balance factor (V) = -1

then H'<--H'-2

else

H'<--H'-1

V<--rightchild(V)

This will take O(H1) time.

Let U denotes the parent of V , from a new tree whose root contains the key X , whose left subtree is the subtree which is rooted at V and whose right subtree will be T2'.

NOTE: this is a valid binary search tree, since all the key of the subtree which are rooted at V in T1 and hence it is Smaller than X and by construction , X is smaller than or equal to all elements in T2'. The balance factor of the root of this new tree is H-H'. which will be valued to be either -1 or it will be 0. hence the new generated tree is a valid AVL TREE.. the height of this new tree is H'+1, which is one bigger than V's height.

Let the root of this new tree is the right child of U in place of V again, since all the keys in this new tree are at least as big as U, this will results in a valid binary search tree. this part of construction takes constant time.

Now when we will be in the INSERT Algorithm, we will go up to the tree, starting point will be U fixing balance factorand perhaps doing a rotation. this will take O(H1) time. NOTE that the correctness and efficiency will be follows from the condition which is at U before this process is begun is a condition that can arise during INSERT algorithm.

Since H1,H2 belongs to O(log n), hence the total time taken by this algorithm is in O(log n).

Add a comment
Answer #2

FTrst, compute the heig hts ot T, and T, Then, traverse siom te root, oing to let child om -he root , Qoing to left child the-Hen balance factor C)=-1 if 4. 5 else 8 7% algorithm takes O(h) fine. Let denote theborent e v Νοω , rm a neω-tree ωhose ro

The he ight new tree i-e., 1 bigger than new ree be the siht child let the aoor ain, becau se all keys n rew tree are at leat

Add a comment
Know the answer?
Add Answer to:
Let T1 and T2 be AVL trees such that the largest key in T1 is less...
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
  • just need to answer the second question 3 AVL Trees Assume the following notation/operations on AVL trees....

    just need to answer the second question 3 AVL Trees Assume the following notation/operations on AVL trees. An empty AVL tree is denoted E. A non-empty AVL tree T has three attributes: . The key T.key is the root node's key. The left child T.left is T's left subtree, which is an AVL tree (possibly E). The right child T.right is T's right subtree, which is an AVL tree (possibly E) [3 marks] Describe an alternative version of the RANGECOUNT(T,...

  • Assume the following notation/operations on AVL trees. An empty AVL tree is denoted E. A non-empty AVL tree T has three...

    Assume the following notation/operations on AVL trees. An empty AVL tree is denoted E. A non-empty AVL tree T has three attributes The key T.key is the root node's key. The left child T.left is Ts left subtree, which is an AVL tree (possibly E). The right child T.right is T's right subtree, which is an AVL tree (possibly E). (a) 5 marsl Write a function RANGECOUNT(T, lo, hi) to count the number of nodes in an AVL tree with...

  • Assume the following notation/operations on AVL trees. An empty AVL tree is denoted E. A non-empty AVL tree T has three...

    Assume the following notation/operations on AVL trees. An empty AVL tree is denoted E. A non-empty AVL tree T has three attributes: • The key T.key is the root node’s key. • The left child T.left is T’s left subtree, which is an AVL tree (possibly E). • The right child T.right is T’s right subtree, which is an AVL tree (possibly E). (a) [5 marks] Write a function RangeCount(T, lo, hi) to count the number of nodes in an...

  • Problem 2 [35 points (155 15)]: Let Ti and T2 be two binary search trees containing the same elem...

    Problem 2 [35 points (155 15)]: Let Ti and T2 be two binary search trees containing the same elements. In this problem, you will show how to transform Ti into T2 (i.e. Ti is altered to now have the same structure as T2) through a series of rotation operations. (a) Define a binary tree to be a right-going chain if no node in the tree has a left child (in other words, the tree is a linked list formed through...

  • Suppose you have two binary search trees P and Q. Let P and Q be the...

    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...

  • C++ Question 1 5 pts A binary heap's structure is an AVL tree a complete binary...

    C++ Question 1 5 pts A binary heap's structure is an AVL tree a complete binary tree a particular case of binary search tree a sparse tree Question 2 5 pts When using a hash table with quadratic probing, and the table size is prime, then a new element can always be inserted if the table is at least half empty the table is full the table is at least half full the table is larger than any data value...

  • I also don't quite understand the meaning of N.size, is N.size (the sum of nodes) (a...

    I also don't quite understand the meaning of N.size, is N.size (the sum of nodes) (a node and its sub trees) has? please also explain that, thank you. modify the standard definition of a binary search tree to add a field N.size at each Suppose that we node, which records the size of the subtree under N'Tincluding N itself). A. Explain how to modify the procedure for adding both the case where X is not yet in the tree and...

  • We need to write a method that, given a key as an argument, returns the next...

    We need to write a method that, given a key as an argument, returns the next in order key found in the binary search tree. If the key given as an argument is not found, the method should still return the next in order key. If the binary tree is empty or all the stored keys are smaller than the argument, then the return value should be empty. For example, using a collection of {10,13,52,67,68,83} stored in the binary search...

  • Scenario We need to write a method that, given a key as an argument, returns the...

    Scenario We need to write a method that, given a key as an argument, returns the next in order key found in the binary search tree. If the key given as an argument is not found, the method should still return the next in order key. If the binary tree is empty or all the stored keys are smaller than the argument, then the return value should be empty. For example, using a collection of {10,13,52,67,68,83} stored in the binary...

  • k-d tree Background One generalization of binary trees is the k-d tree, which stores k-dimensional data....

    k-d tree Background One generalization of binary trees is the k-d tree, which stores k-dimensional data. Every internal node of a k-d tree indicates the dimension d and the value v in that dimension that it discriminates by. An internal node has exactly two children, containing data that is less-than-or-equal and data that is greater than v in dimension d. For example, if the node distinguishes on dimension 1, value 107, then the left child is for data with y...

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
Active Questions
ADVERTISEMENT