GeekInterview.com
   Home |  Tech FAQ  |   Interview Questions |  Placement Papers |  Tech Articles |  Learn |  Freelance Projects |  Online Testing |  Geeks Talk |  Job Postings |  Knowledge Base | Site Search |  Add/Ask Question

  GeekInterview.com  >  Placement Papers  >  Adobe

 Print  |  
Question:  An N-ary tree has N sub-nodes for each node, it has M non-leaf nodes
Find the no of leaf nodes?




September 09, 2006 00:36:19 #7
 Nav   Member Since: Visitor    Total Comments: N/A 

RE: An N-ary tree has N sub-nodes for each node, it ha...
 

Yep, the answer is ((1 - N^(h-1)) / ( 1 - N)) + M

where,
N: N-ary tree
h: Height of N-ary tree
M: No of leaf nodes

Example:

Suppose we are having 3-nary tree as shown below:


(h:1)                                                                                 (1)


(h:2)                                         ((2)                          (3)                    (4))


(h:3)                ((5)                   (6)            (7))
   ((8)  (9) (10))  ((11)  (12)  (13))


(h:4)    ((14)  (15)  (16))  ((17))     <-------- M (i.e. 4) Leaf Nodes

Here,
N: 3-ary tree
h: 4, Height of 3-ary tree
M: 4, No of leaf nodes

Now, Apply the formaula:

= ((1 - 3^(4-1)) / ( 1 - 3)) + 4

= 17
Hence Proved.

Solution by:

The Revolutions (Comp Sci RnD)
A Orkut's Community
http://www.orkut.com/Community.aspx?cmm=13552733

Yahoo Group
http://www.yahoogroups.com/group/TheRevolutions

     

 

Back To Question