0

Ok i am trying to write a program for binary search tree. Everything looks good except my program keeps printing this instead of just my inorder traversal of integers.I tried to just just println in the main method and got the same thing?

This is my code:

public class bst {
    Node root;

    public Node getRoot(){
        return root;
    }

    public bst(){
        root=null;
    }

    //method addNode
    public void insert(int key){

        Node newNode= new Node(key);//initialize Node

        if(root==null){

            root=newNode;

        }else{
        Node focusNode=root;        
        Node insNode=root;

        while(insNode!=null){
            focusNode=insNode;

            if(key<focusNode.getKey()){
                insNode=insNode.getLeft();
            }
            else{
                insNode=insNode.getRight();
            }
        }

        if(key<focusNode.getKey()){
            focusNode.setLeft(newNode);
        }
        else{
            focusNode.setRight(newNode);
            }       
        }
    }


    public void inOrder(Node focusNode){

        if (focusNode !=null){

            inOrder(focusNode.leftChild);

            System.out.println(focusNode);

            inOrder(focusNode.rightChild);

        }
    }


//Node class
    class Node{

        int key;        
        Node leftChild;
        Node rightChild;

        //Node constructor
        Node(int key){
            this.key=key;
            leftChild=null;
            rightChild=null;
            }
        public void setLeft(Node left){
            this.leftChild=left;            
        }
        public void setRight(Node right){
            this.rightChild=right;      
        }
        public Node getLeft(){return leftChild;}
        public Node getRight(){return rightChild;}
        public void setKey(int k){this.key=k;}
        public int getKey(){return key;}
        public void print(){
            System.out.println(getKey());
        }
        }


    public static void main(String[] args){

        bst theTree= new bst();

        theTree.insert(30);
        theTree.insert(60);
        theTree.insert(50);
        theTree.insert(70);

        theTree.inOrder(theTree.getRoot());


    }

}
duplode
  • 33,731
  • 7
  • 79
  • 150
bluerubez
  • 300
  • 2
  • 14

2 Answers2

0

In the inOrder method, you do:

System.out.println(focusNode);

You are printing focusNode directly, so unless your Node class overrides the default toString method you will just see a hash code of your object (see this question for details if you are interested). You probably wanted something like

System.out.println(focusNode.getKey());

or simply using the print method you wrote instead.

Community
  • 1
  • 1
duplode
  • 33,731
  • 7
  • 79
  • 150
0

It looks like you are trying to print the actual Node, which will just be the memory address that of that node. If you want to print the the integers, you should print the key to the node.

print(node.getKey());