gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 05:55:14 24 4
gpt4 key购买 nike

我对泛型和二叉搜索树还相当陌生,我正在尝试添加自定义 Student反对 BST,并且不知道如何实现它。我有我的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 做了很多研究,但我在将两者联系在一起时遇到了困难。请帮忙!

最佳答案

修复学生类(class)声明:

class Student implements Serializable, Comparable<Student> {

关于java - 将自定义对象添加到二叉搜索树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23159045/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com