我正在努力初始化一个包含学生的二元搜索树名单。我试图开始只是将学生添加到名册中,但似乎无法使名册正常工作。我还创建了自己的BST类,而不是使用java.utils。我将在下面发布我的所有代码。如何正确初始化我的名册BST以添加学生?
编译后出错:
run:
java.lang.ExceptionInInitializerError
Caused by: java.lang.RuntimeException: Uncompilable source code - Erroneous
tree type: roster.BST
at Roster.Roster.<init>(Roster.java:152)
at Roster.Roster_Test.<clinit>(Roster.java:6)
Exception in thread "main"
BUILD FAILED (total time: 0 seconds)
编译后我的错误出现在以下几行:
Roster.class:
BST rost = new BST();
以上主要方法:
static Roster rost = new Roster();
主要课程:
package roster;
import java.util.LinkedList;
public class Roster_Test {
static Roster rost = new Roster();
public static void main(String[] args) {
addStudent();
displayAllStudents();
/*lookupStudent("11114");
addCourse();
displayStudents("Math161");
displayCourses("11112");
getCourseAverage("Math161");
dropCoursesBelow("11114", 65);
getCourseAverage("Math161");
dropCourse("11111", "Math161");
getCourseAverage("Math161");
changeGrade("11112", "Math161", 80);
getCourseAverage("Math161");*/
}
// add students to the roster
static void addStudent() {
rost.addStudent(new Student("11111", "Jon", "Benson"));
rost.addStudent(new Student("11112", "Erick", "Hooper"));
rost.addStudent(new Student("11113", "Sam", "Shultz"));
rost.addStudent(new Student("11114", "Trent", "Black"));
rost.addStudent(new Student("11115", "Michell", "Waters"));
rost.addStudent(new Student("11116", "Kevin", "Johnson"));
}
// display all students in the roster
static void displayAllStudents() {
//rost.inOrder();
}
// lookup a student in the roster
static void lookupStudent(String id) {
if (rost.find(id) != null) {
System.out.println(id + " found");
} else {
System.out.println(id + " not found");
}
}
// add courses to the roster
static void addCourse() {
rost.addCourse("11111", new Course("CS116", 80));
rost.addCourse("11111", new Course("Math161", 90));
rost.addCourse("11112", new Course("Math161", 70));
rost.addCourse("11112", new Course("CS146", 90));
rost.addCourse("11112", new Course("CS105", 85));
rost.addCourse("11113", new Course("CS216", 90));
rost.addCourse("11114", new Course("CIS255", 75));
rost.addCourse("11114", new Course("CS216", 80));
rost.addCourse("11114", new Course("Math161", 60));
rost.addCourse("11114", new Course("COMM105", 90));
}
// display students enrolled in a given course id
static void displayStudents(String courseId) {
rost.displayStudents(courseId);
}
// display courses taken by a student
static void displayCourses(String id) {
rost.displayCourses("id");
}
// display the average grade for a student
static void getCourseAverage(String courseId) {
rost.getCourseAverage(courseId);
}
// display the average grade for a student
static void dropCoursesBelow(String id, int grade) {
rost.dropCoursesBelow(id, grade);
}
// drop a course from a student
static void dropCourse(String id, String courseId) {
rost.dropCourse(id, courseId);
}
// change the grade for a student
static void changeGrade(String id, String courseId, int grade) {
rost.changeGrade(id, courseId, grade);
}
}
Student.class
class Student implements Comparable <Student> {
String id;
String firstName;
String lastName;
Student(String id, String fName, String lName) {
this.id = id;
this.firstName = fName;
this.lastName = lName;
}
public String getName() {
return lastName;
}
public void setName(String lName) {
this.lastName = lName;
}
public int compareTo(Student st) {
int lastName = this.lastName.compareTo(st.getName());
if (lastName > 0) {
return 1;
} else if (lastName < 0) {
return -1;
} else {
return 0;
}
}
public void addCourse(String id) {
LinkedList list = new LinkedList();
list.add(id);
}
}
Course.class:
class Course {
String id; // course id
int grade;
Course(String id, int grade) {
this.id = id;
this.grade = grade;
}
}
Roster.class:
class Roster {
Student root;
int numStudents;
BST rost = new BST();
public Roster() {
root = null;
numStudents = 0;
}
public void addStudent(Student st) {
rost.insert(st);
numStudents++;
}
}
BST.java
package roster;
class BinarySearchTree<E extends Comparable<E>> {
private Node<E> root;
public BinarySearchTree() {
root = null;
}
// Generic find method
public Node find(E e) {
Node<E> current = root;
// Loop until e.compare to current element is not equal to 0
while (e.compareTo(current.element) != 0) {
// if e.compare is less than 0 set current to current.left
if (e.compareTo(current.element) < 0) {
current = current.left;
} // else if current is 0 or greater than 0 set current
// to current.right
else {
current = current.right;
}
// if current is null, return null
if (current == null) {
return null;
}
}
// return current value when loop ends
return current;
}
// Check whether the generic value was found and return true or false
public boolean findTF(E e) {
// if find(e) returns a value return true
// else return false if value was not found
if (find(e) != null) {
return true;
} else {
return false;
}
}
public void insert(E e) {
Node<E> newNode = new Node<>(e);
if (root == null) {
root = newNode;
} else {
Node<E> current = root;
Node<E> parent = null;
while (true) {
parent = current;
if (e.compareTo(current.element) < 0) {
current = current.left;
if (current == null) {
parent.left = newNode;
return;
}
} else {
current = current.right;
// if current is equal to null,
// set parent.right to newNode and return
if (current == null) {
parent.right = newNode;
return;
}
}
}
}
}
public void traverse(int traverseType) {
switch (traverseType) {
case 1:
System.out.print("\nPreorder traversal: ");
// call preOrder(root) and implement preOrder()
preOrder(root);
break;
case 2:
System.out.print("\nInorder traversal: ");
inOrder(root);
break;
case 3:
System.out.print("\nPostorder traversal: ");
// call postOrder(root) and implement postOrder()
postOrder(root);
break;
}
System.out.println();
}
// Recursive method - traverse generic BST
// While root is not equal to null visit left node and print value
// of root, then visit right node. Repeat until root becomes null
private void inOrder(Node<E> localRoot) {
if (localRoot != null) {
inOrder(localRoot.left);
System.out.print(localRoot.element + " ");
inOrder(localRoot.right);
}
}
// Recursive method - traverse generic BST
// While root is not equal to null print the value of the root
// and visit left then right nodes. Repeat until root becomes null
private void preOrder(Node<E> localRoot) {
if (localRoot != null) {
System.out.print(localRoot.element + " ");
preOrder(localRoot.left);
preOrder(localRoot.right);
}
}
// Recursive method - traverse generic BST
// While root is not equal to null visit left node and visit
// the right node and print value of root. Repeat until root becomes null
private void postOrder(Node<E> localRoot) {
if (localRoot != null) {
postOrder(localRoot.left);
postOrder(localRoot.right);
System.out.print(localRoot.element + " ");
}
}
}
class Node<E> {
protected E element;
protected Node<E> left;
protected Node<E> right;
public Node(E e) {
element = e;
}
}
答案 0 :(得分:1)
错误Caused by: java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: roster.BST
表示 -
您正在运行一个代码,其中一个/多个java文件未成功或正确编译。
我看到的一件事是,您使用的是BST rost = new BST();
,但您没有任何名为BST
的课程。不应该是BinarySearchTree rost = new BinarySearchTree ()
而是/(\d+?x\d+?)\./
。
随后,清理/删除所有旧类文件重新编译并尝试。