Log(N+1) works only for binary tree(lg(N+1). For N-ary tree the formula is a little different.
Height of node K = floor( Log N ( (N-1)*K ) ) For binary tree it translates to floor(lgN) Note: I've assumed node index begin from 1 rather than 0 Regards, Sandeep Jain On Mon, Aug 1, 2011 at 5:43 PM, Ram Shankar <[email protected]> wrote: > If you want to find out the depth of node number N then it would be log (N > +1 ).. > > Explanation: > Number of terms at each level follows GP i.e., > 1 2 4 8 16 32 ..... and so on. > > > Number of nodes in tree till depth 'i': Take the sum of GP till 'ith' > term. > > So your task is to find the 'i' such that sum of first 'i' terms of GP is > greater than your N. > > > On Mon, Aug 1, 2011 at 4:56 PM, Douglas <[email protected]> wrote: > >> Thanks Ankit, but I am not looking for an iterative solution. I am >> looking for a formula, if possible, rather than a funtion. If I can't >> find an analytical formula I will revert to an iterative/recursive >> function. >> >> On Aug 1, 11:55 am, ankit sambyal <[email protected]> wrote: >> > @Douglas: Here is my approach: >> > >> > Suppose index is the index given and we have to find its depth .Also the >> > tree is N ary >> > >> > int i=1; >> > while(index > 0) >> > { >> > index=index-N^i; >> > i++;} >> > >> > return index; >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Algorithm Geeks" group. >> To post to this group, send email to [email protected]. >> To unsubscribe from this group, send email to >> [email protected]. >> For more options, visit this group at >> http://groups.google.com/group/algogeeks?hl=en. >> >> > > > -- > Cheers, > Ram > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
