in DS edited by
31,298 views
103 votes
103 votes

A program takes as input a balanced binary search tree with $n$ leaf nodes and computes the value of a function $g(x)$ for each node $x$. If the cost of computing $g(x)$ is: $$\Large \min \left ( \substack{\text{number of leaf-nodes}\\\text{in left-subtree of $x$}}\;,\; \substack{\text{number of leaf-nodes}\\\text{in right-subtree of $x$}}\right )$$

Then the worst-case time complexity of the program is?

  1. $\Theta (n)$
  2. $\Theta (n \log n)$
  3. $\Theta(n^2)$
  4. $\Theta (n^2\log n)$
in DS edited by
31.3k views

4 Comments

poor basics in a subject often give headaches.

This question follows basic equation

 

N(h)>=N(h-1) + N(h-2)

N(h)>= 2N(h-2)

height is always log(n)

for each node u do this

for n leaf nodes, max time u will need it nlogn
4
4

@

“we first need to count no. of leaf in left sub-tree, then no. of leaf in right sub-tree + compare them to find minimum”


Here when we see the recurrence relation that you proposed, how are we sure that there will be exactly n/2 leaf nodes in both left sub-tree and right sub-tree? like there can be n/3 leaf nodes in left sub-tree and 2n/3 leaf nodes in right sub-tree.

I hope you got my doubt!

0
0
edited by

https://gateoverflow.in/1079/gate-cse-2004-question-85?show=379216#a379216

this answer by User-Jiren is must read, @Sachin Mittal 1 sir gave some really nice insights there

0
0

11 Answers

112 votes
112 votes
Best answer

B. At the root node (first level) the cost would be $\Theta\left(\frac{n}{2}\right)$ as the tree is balanced.

At next level, we have 2 nodes and for each of them cost of computing $g(x)$ will be $\Theta\left(\frac{n}{4}\right)$. So, total cost at second level $= \Theta\left(\frac{n}{2}\right).$ Similarly at each level (total cost per level and not the cost per node in a level) the cost would be $\Theta\left(\frac{n}{2}\right)$ and so for $\log n$ levels it would be $\Theta({n} \log n).$

PS: Even if we change $\min$ to $\max$ in the defintion of $g(x)$ we get the same answer.

selected by

4 Comments

@Pabitra

“min(number of leaf-nodes in left-subtree of x,number of leaf-nodes in right-subtree of x) “ is COST of computing g(x) not the g(x) itself. g(x) is itself not known.

Question is about finding time complexity of program which calculates g(x) for each node (we know the cost of calculating g(x) only).

n is no. of leafs.

By doing recursion we can find no. of leaf nodes at each subtree in O(n) and can be stored at each node.

Later calculate level wise total cost as explained above.

PS: Read answer below by Arjun Sir and all its comment for better understanding.

2
2
Are we considering the number of leaf nodes as in same order as number of nodes, since we have to calculate the asymptotic notation? As the question says ’n’ is the number of leaf nodes.

Would the answer change if the total number of nodes were given as ‘n’?
0
0

@hollow_mind  Asymptotically ans should not change. As n is taken as total no. of nodes (tree being balanced BST)→

height of tree h = logn

max leaf = 2^h = Theta(n)

1
1
49 votes
49 votes

Do a post order traversal and store and return $\min \left( \text{g}(x \rightarrow left), \text{g}(x \rightarrow right ) \right )$ for all non leaf nodes and store $0$ for all leaf nodes and return 1. BST being balanced, with n leaf nodes we can have total 2n nodes and complexity of tree traversal is linear in number of nodes- $\Theta(n)$.

But this is just computing the time complexity of $g(x)$ for each node- not exactly what is asked in question.

Actually the procedure I gave is computing the COST of computing the value of $g(x)$ which would have been correct had the question been defined as 

$g(x) = \Large \min \left ( \substack{\text{number of leaf-nodes}\\ \text{in left-subtree of x}}\;,\; \substack{\text{number of leaf-nodes}\\\text{in right-subtree of x}}\right )$. 

The correct answer for this would be $\Theta(n \log n)$ as at each level, the cost is $\Theta(n)$ and we have $\log n$ levels since the tree is balanced.  

Correct Answer: $B$

edited by
by

4 Comments

@Arjun sir we can use your algorithm provided that we have some extra space to store the

min(left subtree and right subtree) for every node else it not possible right?
0
0

@Arjun

why we are calculating for each level and not each node??

0
0
Sir, if we are taking cost of every level as n then that means we are only considering cost of visiting leaf nodes because n is no of leaf nodes. Shouldn't we also consider internal nodes that we will be visiting.
0
0
24 votes
24 votes

1.if we use simple divide and conquer technique(without using extra space)

we first need to count no. of leaf in left subtree,then no. of leaf in right subtree+compare them to find minimum.

T(n)=2T(n/2)+1----->O(n)

but we need to compute this for every node.

so, cost at 0 th level=O(n)

cost at 1st level=n/2+n/2=O(n)

cost at 2nd level=n/4+n/4+n/4+n/4=O(n)\

we have total of logn levels (because tree is balanced) and cost at every level is O(n).

Hence total cost is=n*logn=O(nlogn)

2.if we use extra memory to store no of leaf at left subtree and at right subtree for every node

we don't need to go to last level for every node.

1.compute for every leaf node.

2.then use those values to compute at all nodes at (h-1) level till root.

so, time complexity of this method is equal to O(n),since we are calculating value for each node and doing only one comparison. 

space complexity is also O(n),since we are using extra memory for every node.

since question is asking about worst case time complexity,so we have O(nlogn) .

1 comment

@jatin is it always true while calculating worst case , we choose algo with less space complexity O(1) in above case ?
1
1
17 votes
17 votes

Let us assume a balanced binary tree containing n leaf nodes. We have to compute g(x) corresponding to every node x and the cost of computing g(x) is 

min(number of leaf-nodes in left-subtree of x,number of leaf-nodes in right-subtree of x).

we start from root and compute g(x) corresponding to every node.g(root) = min(n/2 , n/2) = Θ(n/2) =Θ(n).

Cost at level 1 :Θ(n).

Cost corresponding to second level :

g(left_node)= min(n/4,n/4)=n/4

g(right_node)=min(n/4,n/4)=n/4

Total cost at level 2 :Θ(n/4 + n/4) =Θ(n/2)=Θ(n)

Similarly total cost at level 3 : Θ(n/8 + n/8 +n/8 + n/8)=Θ(n/2)=Θ(n)

...There are totally log n levels since it is a balanced binary search tree and cost at every level is Θ(n).

There fore time complexity  of the program is Θ(n) . log n =Θ(n logn).

edited by

4 Comments

we have total of logn levels (because tree is balanced) and cost at every level is O(n).

Hence total cost is= n*logn =O(nlogn)

8
8
thank you sir . analysed my mistake and got your point.
1
1

balaeinstein  

Edit your answer, please . Put correct result.

1
1
ok sir
1
1
Answer:

Related questions