将自定义对象添加到二叉搜索树

时间:2014-04-18 17:28:41

标签: java generics binary-search-tree

我对于泛型和二叉搜索树仍然相当新,我正在尝试向BST添加自定义Student对象,并且不知道如何实现它。我有Student类声明,如下所示:

class Student <E>implements Serializable, Comparable {

int studentNumber;

String firstName;
String lastName;
String major;
double gpa;

Student leftChild;
Student rightChild;

public Student() {
    this(0, "", "", "", 0.0);
} // end no-argument studentNumberRecordSerializable constructor

public Student(int sNum, String first, String last, String major, double gpa) {
    setstudentNumber(sNum);
    setFirstName(first);
    setLastName(last);
    setMajor(major);
    setGPA(gpa);
} // end four-argument studentNumberRecordSerializable constructor
....

然后我的Node类:

class TreeNode<E extends Comparable<E>> {

TreeNode<E> leftNode;
E data;
TreeNode<E> rightNode;

public TreeNode(E nodeData) {
    data = nodeData;
    leftNode = rightNode = null; 
} // end TreeNode constructor

public void insert(E insertValue) {

    if (insertValue.compareTo(data) < 0) {

        if (leftNode == null)
            leftNode = new TreeNode<E>(insertValue);
        else
            leftNode.insert(insertValue);
    } // end if
....

然后我试图宣布Tree<Student> tree = new Tree<Student>();,并且说这个学生是无效的论点。我还想调用tree.insertNode(new Student(studentNumber, firstName, lastName, major, gpa));将其添加到节点。是否还有一个我没有遵循的额外步骤或者我做得不对的事情?我已经做了很多关于仿制药和BST的研究,但是我把两者捆在一起很麻烦。请帮忙!

1 个答案:

答案 0 :(得分:2)

修复学生班级声明:

class Student implements Serializable, Comparable<Student> {
相关问题