为什么我的二叉树不显示其中的当前内容? [英] Why won't my binary tree display what's currently in it?

查看:84
本文介绍了为什么我的二叉树不显示其中的当前内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用内部已经存在的二进制文件 display()我的二叉树,但是由于某种原因它不起作用。

我正在获取没有任何输出。

I'm trying to display() my binary tree with what's inside already but for some reason it's not working.
I'm getting nothing as an output.

也许我的方法逻辑不对了?我不太确定。

Maybe my logic for the methods is off? I'm not quite sure.

这是我的 Node.java 文件:

(缺少内容)

这是我的 BinaryTree.java 文件:

public class BinaryTree {
public static Node root;

public BinaryTree() {
    this.root = null;
}

public void insert(Node n, Student s) {
    if(n != null) {
        while(true) {
            if(s.getLastName().compareTo(root.data) < 0) {
                insert(n.left,s);
            } else if(s.getLastName().compareTo(root.data) > 0) {
                insert(n.right,s);
            } else {
                System.out.println("Error!!!!!");
            }
        }
    }
}

public void display(Node root) {
    if(root != null) {
        display(root.left);
        System.out.println(root.left);
        display(root.right);
    }   
}

}

这是我的 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; // Length of longest string that we will base.
        int i = 0; // Index of current string, will be incremented in for loop below.
        int a = 0; // Index of 
        int b = 0;

        if(lastName.length() < s.length()) { // If the string that's currently inside < incoming string
            lengthOfLongestString = lastName.length(); // Then set the current string as the new length.        
        } else {
            lengthOfLongestString = s.length();  // Else incoming string is the new 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) { // If i = length of last character of the string
                return 0;
            }

            if(lastName.charAt(a) < s.charAt(b)) {
                return -1;
            }   
        }
        return 1;
        }
}

这是我的 Main.java 文件:

public class Main extends Student {

public Main(String first, String last, String ID) {
    super(first, last, ID);
}

public static void main(String[] args) {
        Student student = new Student("hi", "purple", "brown");
        Student student2 = new Student("red", "purple", "now");
        Node n = null;

        BinaryTree bt = new BinaryTree();
        bt.insert(BinaryTree.root, student);
        bt.insert(BinaryTree.root, student2);
        bt.display(BinaryTree.root);
    }
}


推荐答案

很少此处出现错误,可能无法立即解决问题。

Few things wrong here, and it may not solve the problem immediately.

1) while(true)将会永远旋转,而您永远也不会脱离它。另外,您在递归语句中有一个无限循环,这只是糟糕的设计。

1) while(true) is going to spin forever and you never break out of it. Also you have an infinite loop within a recursive statement, which is just poor design.

2)您始终在比较 s root 。您应该比较 n.data

2) You always are comparing s and root. You should compare n.data

3)您需要分配 n.left n.right

3) You need to assign n.left and n.right at some point.

4)(可选)学生可以使用相同的姓氏。

4) (Optional) Students can have the same last names.

5)您总是从根插入BinaryTree,因此将其作为参数传递似乎是多余的,但不必是静态的。

5) You always insert into a BinaryTree from the root, so passing it in as a parameter seems redundant, but it doesn't need to be static.

bt.insert(student);

您可以这样写

private Node root;

public boolean insert(Student s) {
    return insert(this.root, s);
}

private boolean insert(Node n, Student s) {

    if (this.root == null) {
        this.root = n;
        return n == null;  // did you really insert something? 
    }

    if (n == null) return false;

    boolean inserted = false;

    if(s.getLastName().compareTo(n.data) <= 0) { // Compare same names
        if (n.left == null) {
            n.left = new Node(s); // Not sure what this looks like
            return true;       // Inserted a node
        }
        inserted = insert(n.left,s);
    } else if(s.getLastName().compareTo(n.data) > 0) {
        if (n.right == null) {
            n.right = new Node(s); // Not sure what this looks like
            return true;       // Inserted a node
        }
        inserted = insert(n.right,s);
    } else { // Should never get here 
        // Not actually an error
        // System.out.println("Error!!!!!");
    }
    return inserted;
}

这篇关于为什么我的二叉树不显示其中的当前内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆