简体   繁体   中英

Why pointers are shown instead of string in my inorder traversal of BST?

Here is the code for printing the inorder traversal of the binary search tree: public class BSTPrint {

public void printInorder(BSTNode root){
    if (root!=null){
        printInorder(root.getLeftNode());
        System.out.println(root.getNodeValue());
        printInorder(root.getRightNode());
    }

}

public static void main(String[] argc){
    BSTPrint bstPrint = new BSTPrint();
    BSTNode<String> root=new BSTNode<String>();
    root.setNodeValue("5");
    BSTNode<String> rootLeft= new BSTNode<String>();
    rootLeft.setNodeValue("3");
    root.setLeftNode(rootLeft);
    BSTNode<String> rootRight= new BSTNode<String>();
    rootRight.setNodeValue("8");
    root.setRightNode(rootRight);
    bstPrint.printInorder(root);
}
}

Here's the BSTNode class:

public class BSTNode<E> {
    private E value;
    private BSTNode<E> leftNode=null;
    private BSTNode<E> rightNode=null;

    public BSTNode getLeftNode(){
        return this.leftNode;
    }
    public void setLeftNode(BSTNode rootLeft){
        BSTNode newLeftNode=new BSTNode();
        newLeftNode.leftNode=null;
        this.leftNode=newLeftNode;
        newLeftNode.value=rootLeft;
    }
    public BSTNode getRightNode(){
        return this.rightNode;
    }
    public void setRightNode(BSTNode rootRight){
        BSTNode newRightNode=new BSTNode();
        newRightNode.rightNode=null;
        this.rightNode=newRightNode;
        newRightNode.value=rootRight;
    }

    public E getNodeValue(){
        return this.value;
    }

    public void setNodeValue(E value){
        this.value=value;
    }

}

Why am I seeing my result like the following?

BSTNode@246f9f88
5
BSTNode@1c52ac68

instead of

3
5
8

printInOrder is working just fine. The left node's value isn't 3; the left node's value is another node, because setLeftNode :

public void setLeftNode(BSTNode rootLeft){
    BSTNode newLeftNode=new BSTNode();
    newLeftNode.leftNode=null;
    this.leftNode=newLeftNode;
    newLeftNode.value=rootLeft;
}

isn't hooking the provided rootLeft node into this.leftNode . It's creating another node to be the leftNode and setting that node's value to rootLeft . The same problem appears in setRightNode .

You need to fix setLeftNode and setRightNode . Also, if you're using an IDE (such as Eclipse), you know all those places your IDE is showing yellow warning indicators? The ones where if you mouse over them, it says you're not using generics properly? If you had included the <E> s when the IDE was warning you about that, the compiler would have caught the bugs in setLeftNode and setRightNode for you.

Not fresh on my Java, but I think you want to define BSTNode left and right node members like so:

public class BSTNode<E> {
   private E value;
   private BSTNode<E> leftNode=null;
   private BSTNode<E> rightNode=null;
}

Your setleft/right are actually wrong:

they should be:

public void setRightNode(BSTNode rootRight){
    this.rightNode=rootRight;
}
public void setLeftNode(BSTNode rootLeft){
    this.leftNode=rootLeft;
}

You already have a node - so you just need to set it. there is no need to create an extra node object.

Hint: if you look at the java warnings in your ide you will find that it complains that you should parameterize some of your values (always use BSTNode in all parts of BSTNode). Once you add it, it will tell you that it cannot conver BSTNode to E in you set*Node functions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM