Subtract n-1,(n^2) - 1,(n^3)-1 until index is <= 0 and so on from the given
index.
i think following code should work. correct me if m wrong

n=no. of childs in tree.
depth=0;
while(index>0)
{
    index=index - (n-1);
    pow=pow*n;
    depth++;
}



On Mon, Aug 1, 2011 at 3:01 PM, Douglas <douglas.han...@gmail.com> wrote:

> I have a full n-ary tree with N nodes.  The top node is indexed 0 with
> all other nodes indexed incrementally from left to right as we decend
> the tree.  The last node has index N-1.  For example, a 15 node binary
> tree would be indexed as follows:
>
> 0
> 1,2
> 3,4,5,6
> 7,8,9,10,11,12,13,14
>
> So, my question is, for any full n-ary tree with total nodes N, given
> an arbitrary node index based on this structure, can I determine the
> nodes depth in the tree?  I don't want to have to create a tree
> structure and traverse it to determine its depth as the tree may
> potentially be huge.  I am hoping to model this structure on the fly
> with the availability of an analytical formula, but I am not convinved
> it is possible?
>
> Thanks,
>
> Douglas.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to algogeeks@googlegroups.com.
> To unsubscribe from this group, send email to
> algogeeks+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>
>


-- 
Abhishek Gupta
MCA
NIT Calicut
Kerela

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to