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 6 of 6

Thread: Java binary Tree code help

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java binary Tree code help

    I need help with binary trees, I have written a code to get all the nodes below a current Node and it is working but I need to get it to look better if possible. To get the number of nodes below I have created an Array List, which I then go to the first Node below and add all the people to the Array list on the same level till I get to the end of the level, then I go down again until I cannot go down any more. Is there a way I can have my code without having to use an array List? I have put my code below. The brieff about the tree is that you have a parent and below the Parent are children who can have brothers and sisters next to them and after that they also have children below that. The method is trying to find the number of children below any given child
    private LinkedList<Node> TempQueue = new LinkedList<Node>();
     
    public int noOfYoungerChildren(Member p1){
        Node tmp = find(p1);
        return countYoungerChildren(tmp);
        }
     
      private int countYoungerChildren(Node m){
        Node tmp = m.getFirstYoungerChildren();
        if(tmp != null){
          TempQueue.add(tmp);
          while(tmp.getNextChild() != null){
            TempQueue.add(tmp.getNextChild());
            tmp = tmp.getNextChild();
     
          }
     
        }
        if(queue.isEmpty()){
          return 0;
        }
        else {
          int number = 1 + countYoungerChildren(TempQueue.poll());
          return number;
        }
     
      }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,854
    My Mood
    Mellow
    Thanks
    239
    Thanked 721 Times in 707 Posts

    Default Re: Java binary Tree code help

    . . . it is working but I need to get it to look better . . .
    What's your question? What do you mean by "look better?"
    Is there a way I can have my code without having to use an array List?
    Why? What other collection would you prefer?

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java binary Tree code help

    Thank you for the reply. What I mean is there a better way to write this recursive code rather than have to create another private method thats called by the main method like what I am currently doing.

    I am trying to do away with the array list because it is only created and used for the purpose of looping through the binary tree and then polled to see the number of elements in the tree. I am new to Java programming but I know there has to be a way I could loop through my Binary Tree and add 1 every time my loop finds a Node.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,854
    My Mood
    Mellow
    Thanks
    239
    Thanked 721 Times in 707 Posts

    Default Re: Java binary Tree code help

    Moving code from the main() method to other methods is good practice and enables reuse.

    As for determining the number of children a node in a binary tree has, yes there may be a better way that doesn't require an ArrayList. The solution is typically recursive. Here is some example code from Stanford, including a size() method.

  5. #5
    Junior Member
    Join Date
    Nov 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java binary Tree code help

    Thank you, I had seen the example from Stanford but it would have involved me having to write another method for the left and right which would mean more code so I will carry on tinkering with my current solution.

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,854
    My Mood
    Mellow
    Thanks
    239
    Thanked 721 Times in 707 Posts

    Default Re: Java binary Tree code help

    You're welcome. Good luck, and let us know what you come up with.

Similar Threads

  1. (Binary Tree) Family tree - help with my addChild method
    By Pip_Squeak in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 26th, 2014, 07:52 AM
  2. Replies: 1
    Last Post: February 8th, 2014, 04:20 PM
  3. Java Binary Tree
    By comwizzz in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 28th, 2011, 02:15 PM
  4. Need help fixing Binary Search Tree code
    By fistpunch in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 6th, 2010, 10:22 AM
  5. 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