The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+4 votes
635 views

Consider the height-balanced tree $T_{t}$ with values stored at only the leaf nodes, shown in Fig.4.

(i) Show how to merge to the tree, $T_{1}$ elements from tree $T_{2}$ shown in Fig.5 using node D of tree $T_{1}$.

(ii) What is the time complexity of a merge operation of balanced trees $T_{1}$ and $T_{2}$ where $T_{1}$ and $T_{2}$ are of height $h_{1}$ and $h_{2}$ respectively, assuming that rotation schemes are given. Give reasons.

in DS by Boss (29.7k points)
edited by | 635 views

1 Answer

+3 votes

A height balanced tree is a binary tree in which the difference between the left subtree and right subtree is not greater than 1.

Since the values are stored only at the leaf nodes and the merging is done using node D, we add extra nodes as shown below  :-

Complexity will be $O(n \log n)$

As here all nodes of second tree need to be inserted in tree and rotation of nodes are needed.

by Veteran (115k points)
edited by
0
How did you merge t1 and t2.. I m not getting it.. Can't we just put all elements and from t1 and t2 and perform insertion with rotation..
0
like BST

F can be right child of B and 5 will be in left subtree leaf of F
0

@srestha , I think, you have added some extra nodes..here T1 contains 9 nodes and T2 contains 5 nodes. So, after merging , total nodes should be 14..but in your 1st image , no. of nodes = 15 and in 3rd image , no. of nodes = 16...

could you please explain why time complexity is O(nlogn).. I think here we should have to write time complexity in terms of h1 and h2. and could you please explain what is meaning of merge using node D.

0
@ankit

I havenot added 1 extra node, I added 2 extra node

See in 1st pic after E I added 1 extra node, otherwise 5 couldnot be there in resultant tree

and yes again I added 1 extra node in 3rd pic

Because  it holds 6,7 as leaf

Basically according to question, we need to accommodate 8 nodes as leaf
0
@srestha , instead of adding one extra node , can't we add 6 to the  left of E and 5 to the left of 6 ?
+1
then 6 will not be leaf
0
@srestha , is it necessary that all given leaf nodes should also be leaf nodes in the merged tree ?  it is not mentioned in the question..
0
yes, required

As this is pattern given in question

some visualization required to ans some ques
0
@srestha In First Image Left Child of E is 4 and Right Child of C is 4 can u explain please?
0
how Complexity will be O(n log n) ??
0
O(n) tc if the use of extra space is permitted
0

yes @Bhupendra 

Related questions

+5 votes
1 answer
2
Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,092 questions
55,239 answers
190,755 comments
85,992 users