# find the level of a certin leaf in a binery tree???

• 02-07-2012, 07:32 AM
oshikoren
find the level of a certin leaf in a binery tree???
can anyone please help me with the recorsive code to find the level (height) of a certin leaf in a binery tree??!!
• 02-07-2012, 10:53 AM
DarrylBurke
Re: find the level of a certin leaf in a binery tree???
Sure. What do you have, and where are you stuck?

db
• 02-07-2012, 05:01 PM
oshikoren
Re: find the level of a certin leaf in a binery tree???
public static void level(Node n)
{
if(n!=null)
{
System.out.print(n.getNumber()+"=>"**********);
level(n.getLeftSon());
level(n.getRightSon());

}
}

thats a simple code of a method that print all leaves in a binary tree what i need to do is next to each number "leaf" print the level where the leaf is located on the tree????
• 02-07-2012, 05:08 PM
JosAH
Re: find the level of a certin leaf in a binery tree???
Pass the level as an another parameter and increment it where needed.

kind regards,

Jos
• 02-07-2012, 05:34 PM
oshikoren
Re: find the level of a certin leaf in a binery tree???
it wont work cause its a recorsive method every time ill call the method it will set my counter integer on zero.....
i also write this method:

private static int inWhichLevel(Node root, Node n)
{
if(root!=null)
{
if(isInTree(root,n))
{
if(isInTree(root.getLeftSon(),n))
{
return inWhichLevel(root.getLeftSon(),n)+1;
}
else
{
return inWhichLevel(root.getRightSon(),n)+1;
}
}
else
return 0;
}
return 0;
}

the thing is that i cant use it cause this method needs both the root Node and the Node which i want to get his level??????!!!!!!
• 02-07-2012, 06:16 PM
JosAH
Re: find the level of a certin leaf in a binery tree???
Quote:

Originally Posted by oshikoren
it wont work cause its a recorsive method every time ill call the method it will set my counter integer on zero.....

You didn't do it correctly then; I can't give you any code because it would be spoonfeeding; all I can say is, pass the level as a parameter an adjust it where necessary. Your method will always know the level of the node it is investigating.

kind regards,

Jos
• 02-07-2012, 06:26 PM
Norm
Re: find the level of a certin leaf in a binery tree???
• 02-07-2012, 06:27 PM
oshikoren
Re: find the level of a certin leaf in a binery tree???
i see what u seeing the thing is that im not allowed to change the signture of the method it has to stay as it is "public static void level(Node n)"
so no room for more parameters.... any other idea??????
• 02-07-2012, 06:40 PM
JosAH
Re: find the level of a certin leaf in a binery tree???
Quote:

Originally Posted by oshikoren
i see what u seeing the thing is that im not allowed to change the signture of the method it has to stay as it is "public static void level(Node n)"
so no room for more parameters.... any other idea??????

You should've mentioned that silly restriction in your first post. I hate it when those silly restrictions (who comes up with them anyway?) show up at the end. Good luck with it.

kind regards,

Jos