1

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!

1 Answer 1

3

Fix Student class declaration:

class Student implements Serializable, Comparable<Student> {
Sign up to request clarification or add additional context in comments.

1 Comment

do I need to implement Comparable<Student> in the class I'm creating the tree in too?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.