Java 为什么赢了';我的二叉树显示什么';正在插入什么?
我想知道为什么我的Java 为什么赢了';我的二叉树显示什么';正在插入什么?,java,algorithm,data-structures,binary-tree,nodes,Java,Algorithm,Data Structures,Binary Tree,Nodes,我想知道为什么我的display()。我试着比较学生的姓氏。我只想让我的树显示出来,这样我就可以确认insert()有效 这是我的Node.java文件: class Node { Student data; Node left; Node right; public Node(Student data) { this.data = data; left = null; right = null; } } public class Binary
display()。我试着比较学生的姓氏。我只想让我的树显示出来,这样我就可以确认insert()
有效
这是我的Node.java
文件:
class Node {
Student data;
Node left;
Node right;
public Node(Student data) {
this.data = data;
left = null;
right = null;
}
}
public class BinaryTree {
public Node root;
public void insert(Student s) {
root.left = new Node(s);
root.right = new Node(s);
root = insert(s,root);
}
private Node insert(Student s, Node t) {
if(t == null) {
t = new Node(s);
return t;
}
else {
if(s.getLastName().compareTo(t.data.getLastName()) < 0) {
t.left = new Node(s);
t.left = insert(s,t.left);
return t.left;
} else if(s.getLastName().compareTo(t.data.getLastName()) > 0) {
t.right = new Node(s);
t.right = insert(s,t.right);
return t.right;
}
}
return t;
}
public void display(Node root) {
if(root == null) {
System.out.println("Nothing found.");
} else if(root != null) {
display(root.right);
System.out.println(root.data);
display(root.left);
}
}
}
public class Main {
public static void main(String[] args) {
Student student = new Student("hi", "bye", "testing");
Student student2 = new Student("out", "some", "names");
BinaryTree bt = new BinaryTree();
bt.insert(student);
bt.insert(student2);
bt.display(bt.root);
}
}
这是我的BinaryTree.java
文件:
class Node {
Student data;
Node left;
Node right;
public Node(Student data) {
this.data = data;
left = null;
right = null;
}
}
public class BinaryTree {
public Node root;
public void insert(Student s) {
root.left = new Node(s);
root.right = new Node(s);
root = insert(s,root);
}
private Node insert(Student s, Node t) {
if(t == null) {
t = new Node(s);
return t;
}
else {
if(s.getLastName().compareTo(t.data.getLastName()) < 0) {
t.left = new Node(s);
t.left = insert(s,t.left);
return t.left;
} else if(s.getLastName().compareTo(t.data.getLastName()) > 0) {
t.right = new Node(s);
t.right = insert(s,t.right);
return t.right;
}
}
return t;
}
public void display(Node root) {
if(root == null) {
System.out.println("Nothing found.");
} else if(root != null) {
display(root.right);
System.out.println(root.data);
display(root.left);
}
}
}
public class Main {
public static void main(String[] args) {
Student student = new Student("hi", "bye", "testing");
Student student2 = new Student("out", "some", "names");
BinaryTree bt = new BinaryTree();
bt.insert(student);
bt.insert(student2);
bt.display(bt.root);
}
}
这是我的Main.java
文件:
class Node {
Student data;
Node left;
Node right;
public Node(Student data) {
this.data = data;
left = null;
right = null;
}
}
public class BinaryTree {
public Node root;
public void insert(Student s) {
root.left = new Node(s);
root.right = new Node(s);
root = insert(s,root);
}
private Node insert(Student s, Node t) {
if(t == null) {
t = new Node(s);
return t;
}
else {
if(s.getLastName().compareTo(t.data.getLastName()) < 0) {
t.left = new Node(s);
t.left = insert(s,t.left);
return t.left;
} else if(s.getLastName().compareTo(t.data.getLastName()) > 0) {
t.right = new Node(s);
t.right = insert(s,t.right);
return t.right;
}
}
return t;
}
public void display(Node root) {
if(root == null) {
System.out.println("Nothing found.");
} else if(root != null) {
display(root.right);
System.out.println(root.data);
display(root.left);
}
}
}
public class Main {
public static void main(String[] args) {
Student student = new Student("hi", "bye", "testing");
Student student2 = new Student("out", "some", "names");
BinaryTree bt = new BinaryTree();
bt.insert(student);
bt.insert(student2);
bt.display(bt.root);
}
}
这是我在控制台中的输出
:
Nothing found.
我要说的是,把重点放在插入逻辑上,因为这是错误的
您不需要显示方法来查看树。启动调试器,并检查左侧和右侧节点对象引用
1) 您将立即遇到空树上的nullpointerexception
2) 您只需要在空树上分配根,而不是将可以到达的每个节点都设置为同一个学生节点
举个例子,就是这样
public class BinaryTree {
public Node root;
public void insert(Student s) {
root = insert(s,root);
}
private Node insert(Student s, Node t) {
if (root == null) return new Node(s);
if (...) // compare names
然后,如果您查看此处,则可以在创建新节点时覆盖该节点
if(s.getLastName().compareTo(t.data.getLastName()) < 0) {
t.left = new Node(s); // this is useless
t.left = insert(s,t.left);
return t.left;
if(s.getLastName().compareTo(t.data.getLastName())<0){
t、 左=新节点;//这没有用
t、 左=插入(s,t.左);
返回t.left;
当你在一个叶节点上时,你需要检查,然后相应地返回。你需要返回当前节点,而不是返回子节点
if(s.getLastName().compareTo(t.data.getLastName()) < 0) {
if (t left == null) {
t.left = new Node(s);
} else {
t.left = insert(s,t); // insert using t as the new root
}
return t; // return t with its new child
if(s.getLastName().compareTo(t.data.getLastName())<0){
if(t left==null){
t、 左=新节点;
}否则{
t、 left=insert(s,t);//使用t作为新根插入
}
return t;//返回t及其新子级
对右侧进行同样的操作
如果这不起作用,请尝试为一个小的3节点树编写算法,然后为一个4-7节点的平衡树编写算法,如果(t==null){root=t;}
您正在将根设置为null
您的显示逻辑也有问题,因为您正在调用System.exit(0)
当命中基本大小写时。这将杀死整个程序。您可能只想返回。@marainesparnisari,但我将递归地插入到右侧和左侧。由于根始终为null,因此它不会进入其他大小写。root=insert(s,root)
我发现插入和遍历都有问题。让插入工作起来,然后再担心显示。这非常清楚,谢谢!我100%理解逻辑,这完全有道理,但当我尝试实现这一点时,我的控制台输出是:什么都没有找到。Student@4b71bbc9什么也没找到。Student@17dfafd1未找到任何内容。
递归display()
方法每次在树中遇到null
时,都会打印未找到的内容。通常会有许多这样的内容。相反,如果根为null
,则只打印该消息,即在之前(或代替之前)调用递归方法。不,@nampa。在第一次调用display()
时,它是,但是当您递归调用时,root
不是树的绝对根,只是子树的相对根。如果整个树不为空,它可能为空。