Question

I am still fairly new to generics and binary search trees, and I am trying to add a custom Student object to a BST, and don't know exactly how to go about implementing it. I have my Student class declaration like follows:

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
....

and then my Node class:

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
....

and then I'm trying to to declare Tree<Student> tree = new Tree<Student>(); and it is saying that that student is an invalid argument. I want also call tree.insertNode(new Student(studentNumber, firstName, lastName, major, gpa));to add it to the node. Is there an extra step that I am not following or something I am not doing right? I have done a lot of research on generics and also BSTs but I am having troubles tying the two together. please help!

Was it helpful?

Solution

Fix Student class declaration:

class Student implements Serializable, Comparable<Student> {
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top