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: Quick binary tree question.

  1. #1
    Junior Member
    Join Date
    Sep 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Quick binary tree question.

    I'm implementing a RNL traversal across this binary search tree

    here's my pseudocode and the loop I'm trying to implement it with:

    1. If there is a node to the right, push current node onto stack, follow right node (by setting it to currentNode).
    2. If there is no right node, print current node.
    3. If there is a left node push it onto stack. follow left node.
    4. If there is no left node pop your current node from the stack.

            Stack nodes = new Stack();
            Node currentNode = root;
     
            do {
                Node right = currentNode.rightChild;
                if (right != null) {
                    nodes.push(right);
                    currentNode = right;
                }//end if
     
                currentNode.displayNode();
     
                Node left = currentNode.leftChild;
                if (left != null) {
                    nodes.push(left);
                    currentNode = left;
                }//end if
                else
                    nodes.pop();
                //end else
            } while (! nodes.isEmpty());  //end do while

    The output is only 3 nodes out of many, so there is something wrong with it, though I can not figure out what. Many thanks to any who can take a quick look.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Quick binary tree question.

    You must traverse all the way left before you can begin pushing onto the stack. This is best done recursively.

    Note: I'm taking advantage of passing by reference here. You'll need to initialize a a stack to push the nodes onto before calling this method.

    public void traverse(Node node, Stack toPushOnto)
    {
         if (node.leftChild != null)
         {
              traverse(node.leftChild,toPushOnto);
         }
         toPushOnto.push(node);
         if (node.rightChild != null)
         {
              traverse(node.rightChild,toPushOnto);
         }
    }

Similar Threads

  1. B+ Tree implementation
    By programmer in forum Java Theory & Questions
    Replies: 2
    Last Post: November 15th, 2009, 04:47 PM
  2. Binary Search Tree
    By Koren3 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 12th, 2009, 08:27 AM
  3. Binary Search Tree implementation
    By Ceasar in forum Java Theory & Questions
    Replies: 3
    Last Post: October 9th, 2009, 12:23 AM
  4. Replies: 6
    Last Post: April 14th, 2009, 08:02 AM
  5. How to convert float and double data types to binary?
    By rosh72851 in forum Java Theory & Questions
    Replies: 1
    Last Post: October 7th, 2008, 10:45 PM