這裏是印刷二叉搜索樹的中序遍歷的代碼: 公共類BSTPrint {爲什麼在我的BST的順序遍歷中顯示指針而不是字符串?
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);
}
}
這裏的BSTNode類:
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;
}
}
爲什麼我看到我的結果類似如下?
[email protected]
5
[email protected]
代替
3
5
8
你的泛型全搞砸了。 – Rishav
完全不相關,但這是您的代碼應該以適當的泛型來看待的方式。 http://ix.io/l9P – Rishav