0

I believe my inOrderTraverseTree() method puts the tree in order but I'm not sure how to go about actually printing it in order like that. Any suggestions? And why does the method not print them out in order as it is? Thanks, Zach

    public class Tree 
        {
    Node root;
    public static void main(String[] args){
        Tree t = new Tree();
        t.addNode("hey");
        t.addNode("fghjk");
        t.addNode("aaaaaa");
        t.addNode("zzzzzz");
        t.addNode("egh");
        t.addNode("rrrrrr");
        t.inOrderTraverseTree(t.root);



    }

    public void displayTree(){
        Node link = root;

        while(link != null){


        }
    }
    public void addNode(String line){

        //Create a new Node and initialize it
        Node newNode = new Node(line);

        //If there is no root this becomes root

        if(root == null){
            root = newNode;
        } else {

            //Set root as the Node we will start
            //with as we traverse the tree

            Node focusNode = root;

            // Future parent for our new Node

            Node parent;

            while(true){

                // root is the top parent so we start there

                parent = focusNode;

                //Check if the new Node should go on the left 
                //side of the parent node

                if(line.compareTo(focusNode.line) == -1){

                    focusNode = focusNode.leftChild;

                    //If the left child has no children

                    if(focusNode == null){
                        parent.leftChild = newNode;
                        return; //All Done
                    }

                } else { // If we get here put the node on the right

                    focusNode = focusNode.rightChild;

                    //If the right child has no children

                    if(focusNode == null){

                        //then place the node on the right of it

                        parent.rightChild = newNode;
                        return; //All Done
                    }

                }
            }
        }

    }

    public void inOrderTraverseTree(Node focusNode)
    {
        if(focusNode != null){

            //traverse the left node

            inOrderTraverseTree(focusNode.leftChild);

            //Visit the currently focused on node

            System.out.println(focusNode);

            //Traverse the right node 

            inOrderTraverseTree(focusNode.rightChild);

        }
    }
    class Node {

        String line;

        Node leftChild;
        Node rightChild;

        Node(String line){
            this.line = line;
        }

        //this method overrides toString in Object class
        public String toString(){
            return line;
        }

    }


}
2
  • can you provide an example of what do you expect to be printed? Commented May 13, 2014 at 3:01
  • aaaaaa egh fghjk hey rrrrrr zzzzzz Commented May 13, 2014 at 5:51

1 Answer 1

1

The bug is in this line of addNode():

if(line.compareTo(focusNode.line) == -1){
.
.

Change it to:

if(line.compareTo(focusNode.line) < 0){
.
.

See more details on the return value here:

http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#compareTo%28java.lang.String%29

and

String Compareto actual return value

The rest of the code looks good to me.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.