我是Java编程和数据结构的新手。不过,经过这么多努力,我可以实现以下代码。在那里,我需要将值插入节点,并在每个节点中打印值,以在递归的帮助下演示所有03种深度优先遍历技术。
我已经开发了03种单独的方法来实现上述03种遍历方法。 (我已在问题末尾给出的代码中注释了特定的部分)
但是,当我尝试运行代码时,得到以下错误消息Click here to view,作为引用,我以如下文本格式提供了错误消息。
Exception in thread "main" java.lang.NullPointerException
data -> 14
data -> 5
null
at BSTreeDemo.BSTree.preOrderTraversal(BSTreeDemo.java:75)
at BSTreeDemo.BSTree.preOrderTraversal(BSTreeDemo.java:75)
at BSTreeDemo.BSTree.preOrderTraversal(BSTreeDemo.java:75)
at BSTreeDemo.BSTree.preOrderTraversal(BSTreeDemo.java:75)
at BSTreeDemo.BSTreeDemo.main(BSTreeDemo.java:16)
我不明白为什么会收到上述错误。因为在我看来,我的方法是正确的。以下是我实现的完整代码
package BSTreeDemo;
public class BSTreeDemo {
public static void main(String[] args) { //Main class
BSTree t1 = new BSTree();
t1.addNode(25);
t1.addNode(14);
t1.addNode(78);
t1.addNode(45);
t1.addNode(5);
t1.addNode(89);
System.out.println("-------------PreOrderTraversal------------");
t1.preOrderTraversal(t1.root);
System.out.println("-------------PostOrderTraversal------------");
t1.postOrderTraversal(t1.root);
System.out.println("-------------InOrderTraversal------------");
t1.inOrderTraversal(t1.root);
}
}
class BSTNode { // Binary Search tree node class
int data;
BSTNode leftChild;
BSTNode rightChild;
public BSTNode(int data){
this.data=data;
}
@Override
public String toString(){
return "data -> "+data;
}
}
class BSTree {
BSTNode root;
public void addNode(int data) { // method to add values to each node in binary search tree
BSTNode newNode = new BSTNode(data);
if(root == null){
root=newNode;
}
else{
BSTNode currentNode = root;
while(true){
BSTNode parentNode = currentNode;
if(newNode.data<currentNode.data){
currentNode=currentNode.leftChild;
if(currentNode==null){
parentNode.leftChild = newNode;
return;
}
}
else{
currentNode = currentNode.rightChild;
if(currentNode ==null){
parentNode.rightChild = newNode;
return;
}
}
}
}
}
public void preOrderTraversal(BSTNode currentNode){ // Recursive function to print the values in Pre-Order traversal
System.out.println(currentNode);
preOrderTraversal(currentNode.leftChild);
preOrderTraversal(currentNode.rightChild);
}
public void postOrderTraversal(BSTNode currentNode){ // Recursive function to print the values in Post-Order traversal
postOrderTraversal(currentNode.leftChild);
postOrderTraversal(currentNode.rightChild);
System.out.println(currentNode);
}
public void inOrderTraversal(BSTNode currentNode){ // Recursive function to print the values in In-Order traversal
inOrderTraversal(currentNode.leftChild);
System.out.println(currentNode);
inOrderTraversal(currentNode.rightChild);
}
}
有人可以浏览一下我的代码,并请友善显示我为未能成功运行代码而犯的任何愚蠢错误,以及出现上述错误的原因吗? 在这一点上,我真的很烂。提前致谢。
最佳答案
您缺少递归遍历函数的基本条件,即if (currentNode == null) return;
例子:
public void postOrderTraversal(BSTNode currentNode){ // Recursive function to print the values in Post-Order traversal
if (currentNode == null)
return;
postOrderTraversal(currentNode.leftChild);
postOrderTraversal(currentNode.rightChild);
System.out.println(currentNode);
}
关于java - 使用递归时二进制搜索树遍历无法执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66600592/