为什么我的二叉树不会显示当前的二进制树?

时间:2017-04-27 22:49:16

标签: java algorithm data-structures binary-tree nodes

我正在尝试display()我的二叉树已经内部的东西,但由于某种原因它不起作用。
我什么都没有作为输出。

也许我对这些方法的逻辑是关闭的?我不太确定。

这是我的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);
    }
}

1 个答案:

答案 0 :(得分:0)

这里几乎没有错,它可能无法立即解决问题。

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

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

3)您需要在某个时候指定n.leftn.right

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

5)你总是从根插入BinaryTree,所以将它作为参数传递似乎是多余的,但它不需要是静态的。

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;
}