+1 vote
549 views in DS
edited | 549 views
+1
HINT: Read question One more time, It is saying P-Q<=2  where P and Q are no. of nodes in the left and Right Subtree and Not height of left and right subtree!

## 1 Answer

+13 votes
Best answer

the original image https://drive.google.com/open?id=1-Erqg7Y2xqXJL5YFyDZNh_GJ2Bxto7pT But now i am interested to find it in general height.

we know that the no.of nodes of right side should be less than 2 at maximum only

===> for minimum no.of nodes, i consider  no.of right subtree nodes = no.of left subtree nodes - 2

===> Total nodes in that tree = no.of left subtree nodes + no.of right subtree nodes + 1 ( root node)

= no.of left subtree nodes + ( no.of left subtree nodes - 2 ) + 1

= no.of left subtree nodes + no.of left subtree nodes - 2  + 1

= 2 * no.of left subtree nodes - 1

what is no.of nodes in left subtree ?

it should be equal to no.of nodes in h-1, where h is height of your tree

Total nodes in that tree = 2 * no.of left subtree nodes - 1

N(H) = 2 * ( N(H-1) ) - 1

for solving this recurrence relation, the base condition is N(H=3) = 5

using Back Substitution, we an solve it, the final value we can get

N(H) = 4 . 2(H-3) + 1 = 22 . 2(H-3) + 1 = 2(H-3)+2 + 1 = 2(H-1) + 1.

by Veteran (61.8k points)
selected by
0

@Shaik Masthan

let H-K = 3 ===> K = H-3 ===> the eqn turned into

why upto 3 have u taken?

0
mam, that is base case, i assumed my base case is 3 due to i can manually calculate it.

you can take base case as 4, then you have to substitute the value of 4
0

@Shaik Masthan sir , condition of P and Q is given for h>0 , but root is at height 0,

why we are applying this condition at root.

+1

condition of P and Q is given for h>0

that means when h=0, no need to check, but it doesn't mean don't check the root if h>1

0
someone please explain how the root doesn't satisfy the condition,I didn't get it
0
Solving recurrences is easier if we solve them as given in Rosen(Solving non homogeneous equation).
0

@Shaik Masthan-In extension to solve the recurrence you have derived.

$N(h)=2N(h-1)-1$......(1)

The associated homogenous recurrence relation is

$N(h)=2N(h-1)$

So, solution to this is $C2^h.$

Particular solution will be of the form

$d$

Substituting this in 1 to get $d=1$.

So, $N(h)=C2^h+1$

Base case is $N(0)=1$

$1=C+1$

$C=0.$

But that makes final solution as $N(h)=1$

Where I am going wrong?

@tusharp-I think you were talking about this.Please help.

0
It is like T(n) = 2 T(n-1) - 1

equation becomes x-2 = 0

Solution : An= alpha (2)^n + particular solution

As RHS is constant term its soln will be simply A(constant)

substituting this in eqn we get A=2A -1 => A =1

Sol becomes An= alpha (2)^n +1

We know base condition A3 =5 => alpha = 1/2

final solution = 1/2 (2)^n -1 => 2^n-1 +1
0

@tusharp-Why are you taking base condition only $A(3)=5$. $A(0)=1$ is also valid because for height 0, minimum 1 node is required.

0
However $A(1)=2$ also gives the correct constant value for A.But why not $A(0)=1$?

+1 vote
0 answers
1
+1 vote
2 answers
2
0 votes
2 answers
3
0 votes
1 answer
4
0 votes
0 answers
5
0 votes
0 answers
7