Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: a recorsive method to get level of a certin leaf on a binary tree???

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default a recorsive method to get level of a certin leaf on a binary tree???

    this is a simple recorsive method that print all leaves of a binary tree:

    public static void level(Node n)
    {
    if(n!=null)
    {
    System.out.print(n.getNumber()+"=>"**********);
    level(n.getLeftSon());
    level(n.getRightSon());

    }
    }

    now the thing is that next to each leaf (where all the ************ are) i need to print the level of that certin leaf, how the hell do i do that since a regular counter integer wont do (every time ill call the recorsive method it will set it on zero)

    i also thought about write another method which give u the level of a certin tree and just place it where needed what i came up with:

    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;
    }


    i cant use it cause as a parameter i need both the root Node and the leaf Node.......
    hopefully what i wrote here is understandable.....help would be very very much appreciate......


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: a recorsive method to get level of a certin leaf on a binary tree???

    Cross posted at find the level of a certin leaf in a binery tree???

Similar Threads

  1. Binary Search Tree
    By lex25288 in forum Algorithms & Recursion
    Replies: 3
    Last Post: January 19th, 2011, 09:10 AM
  2. need help with binary tree
    By vash0047 in forum Java Theory & Questions
    Replies: 5
    Last Post: July 12th, 2010, 08:23 AM
  3. Data Structures(Binary Search Tree to AVL Tree)ASAP
    By jfAdik in forum Algorithms & Recursion
    Replies: 2
    Last Post: April 5th, 2010, 03:58 AM
  4. Problem with binary tree
    By Exoskeletor in forum Object Oriented Programming
    Replies: 2
    Last Post: January 8th, 2010, 01:03 PM
  5. Binary Search Tree
    By Koren3 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 12th, 2009, 09:27 AM