我无法理解,我的display()
方法怎么办?我可以发誓我正确地将物品插入树中。
这是我的Node.java
文件:
public class Node {
String data;
Node left;
Node right;
public Node(Student s) {
this.data = data;
left = null;
right = null;
}
}
这是我的BinaryTree.java
文件:
public class BinaryTree {
private Node root;
public Node insert(Student s) {
return insert(this.root, s);
}
private Node insert(Node n, Student s) {
if(this.root == null) { // If the root is null.
this.root = n; // Then set root to node n.
return n; // Return n which is null.
}
if(s.getLastName().compareTo(n.data) < 0) { // If the name that's passed in is less than 0.
if(n.left == null) { // If the left node of the tree is empty.
n.left = new Node(s); // Then create a left node.
insert(n.left,s);
}
return n.left;
} else if(s.getLastName().compareTo(n.data) > 0) { // If the name that's passed in is greater than 0.
if(n.right == null) { // If the right node of the tree is empty.
n.right = new Node(s); // Then create a right node.
insert(n.right,s);
}
return n.right;
}
return n;
}
public void display() {
display(root);
}
private void display(Node root) {
if(root == null) {
System.out.println("Nothing found.");
System.exit(0);
} else {
display(root.right);
System.out.println(root.data);
display(root.left);
}
}
}
这是我的Student.java
文件:
public class Student {
private String firstName;
private String lastName;
private String id;
public Student(String first, String last, String Identification) {
firstName = first;
lastName = last;
id = Identification;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public boolean equals(String studentId) {
return id.equals(studentId);
}
public int compareTo(String s) {
int lengthOfLongestString = 0;
int i = 0;
int a = 0;
int b = 0;
if(lastName.length() < s.length()) {
lengthOfLongestString = lastName.length();
} else {
lengthOfLongestString = s.length();
}
if(lastName.charAt(0) == s.charAt(0)) {
for(i = 0; i < lengthOfLongestString; i++) {
if(lastName.charAt(i) != s.charAt(i)) {
a++;
b++;
break;
}
}
if(i == lengthOfLongestString - 1) {
return 0;
}
if(lastName.charAt(a) < s.charAt(b)) {
return -1;
}
}
return 1;
}
}
这是我的Main.java
文件:
public class Main {
public static void main(String[] args) {
Student student = new Student("hi", "bye", "brown");
Student student2 = new Student("hi", "green", "now");
BinaryTree bt = new BinaryTree();
System.out.println(bt.insert(student));
System.out.println(bt.insert(student2));
bt.display();
}
}
这是我的output
:
null
null
Nothing found.
答案 0 :(得分:1)
我猜你的插入不适用于root元素和实际插入后的额外插入调用。可能有以下变化可能有所帮助。
...
private Node insert(Node n, Student s) {
if(this.root == null) { // If the root is null.
this.root = new Node(s); // Create a new node and return it
return this.root; // Return n which is null.
}
if(s.getLastName().compareTo(n.data) < 0) { // If the name that's passed in is less than 0.
if(n.left == null) { // If the left node of the tree is empty.
n.left = new Node(s); // Then create a left node.
//insert(n.left,s); // Guess this is not needed as the insert already happened
}
return n.left;
} else if(s.getLastName().compareTo(n.data) > 0) { // If the name that's passed in is greater than 0.
if(n.right == null) { // If the right node of the tree is empty.
n.right = new Node(s); // Then create a right node.
//insert(n.right,s); // Guess this is not needed as the insert already happened
}
return n.right;
}
...
希望它有所帮助!