Results 1 to 9 of 9
  1. #1
    oshikoren is offline Member
    Join Date
    Feb 2012
    Posts
    4
    Rep Power
    0

    Default 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??!!

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default Re: find the level of a certin leaf in a binery tree???

    Sure. What do you have, and where are you stuck?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    oshikoren is offline Member
    Join Date
    Feb 2012
    Posts
    4
    Rep Power
    0

    Default 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????

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,734
    Blog Entries
    7
    Rep Power
    21

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    oshikoren is offline Member
    Join Date
    Feb 2012
    Posts
    4
    Rep Power
    0

    Default 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??????!!!!!!

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,734
    Blog Entries
    7
    Rep Power
    21

    Default Re: find the level of a certin leaf in a binery tree???

    Quote Originally Posted by oshikoren View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: find the level of a certin leaf in a binery tree???


  8. #8
    oshikoren is offline Member
    Join Date
    Feb 2012
    Posts
    4
    Rep Power
    0

    Default 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??????

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,734
    Blog Entries
    7
    Rep Power
    21

    Default Re: find the level of a certin leaf in a binery tree???

    Quote Originally Posted by oshikoren View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Binary Tree Help - Find the largest sub-tree
    By joshhazel in forum New To Java
    Replies: 2
    Last Post: 01-30-2012, 03:08 AM
  2. Replies: 0
    Last Post: 09-28-2011, 02:04 PM
  3. Replies: 3
    Last Post: 12-06-2010, 05:42 PM
  4. [SOLVED] Class-level vs Object-level method()
    By mfaizan24 in forum New To Java
    Replies: 7
    Last Post: 06-23-2009, 10:18 AM
  5. Level order binary tree traversal
    By H3rtaherta in forum New To Java
    Replies: 1
    Last Post: 04-20-2009, 08:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •