Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 例外在二元搜索树中声明学生名册时发生初始化错误_Java_Binary Search Tree - Fatal编程技术网

Java 例外在二元搜索树中声明学生名册时发生初始化错误

Java 例外在二元搜索树中声明学生名册时发生初始化错误,java,binary-search-tree,Java,Binary Search Tree,我正在努力初始化包含学生的二叉搜索树名册。我试图从把学生加入名册开始,但似乎无法让名册正常工作。我还创建了自己的BST类,而不是使用java.utils。我将在下面发布我所有的代码。如何正确初始化我的花名册BST以添加学生 编译后出错: run: java.lang.ExceptionInInitializerError Caused by: java.lang.RuntimeException: Uncompilable source code - Erroneous tree type:

我正在努力初始化包含学生的二叉搜索树名册。我试图从把学生加入名册开始,但似乎无法让名册正常工作。我还创建了自己的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)
除主要方法外:

  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);
}

}
学生班

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);        
}

}
花名册.类别:

  BST rost = new BST();
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++;
}

}
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;
}
}
包花名册;
类二进制搜索树{
私有节点根;
公共二进制搜索树(){
root=null;
}
//通用查找方法
公共节点查找(E){
节点电流=根;
//循环直到e。与当前元素的比较不等于0
while(e.compareTo(current.element)!=0){
//如果e.compare小于0,则将current设置为current.left
如果(例如,比较器(当前元素)<0){
current=current.left;
}//否则,如果当前值为0或大于0,则设置当前值
//对,对
否则{
current=current.right;
}
//如果当前值为null,则返回null
如果(当前==null){
返回null;
}
}
//循环结束时返回当前值
回流;
}
//检查是否找到泛型值并返回true或false
公共布尔findTF(E){
//如果find(e)返回一个值,则返回true
//如果未找到值,则返回false
如果(查找(e)!=null){
返回true;
}否则{
返回false;
}
}
公共空白插入(E){
Node newNode=新节点(e);
if(root==null){
根=新节点;
}否则{
节点电流=根;
节点父节点=null;
while(true){
父项=当前;
如果(例如,比较器(当前元素)<0){
current=current.left;
如果(当前==null){
parent.left=newNode;
回来
}
}否则{
current=current.right;
//如果当前值等于null,
//将parent.right设置为newNode并返回
如果(当前==null){
parent.right=newNode;
回来
}
}
}
}
}
公共void遍历(int-traverseType){
开关(traverseType){
案例1:
System.out.print(“\n优先级遍历:”);
//调用preOrder(root)并实现preOrder()
前序(根);
打破
案例2:
System.out.print(“\n顺序遍历:”);
顺序(根);
打破
案例3:
System.out.print(“\nPostorder遍历:”);
//调用postOrder(根)并实现postOrder()
后序(根);
打破
}
System.out.println();
}
//递归方法-遍历通用BST
//当root不等于null时,访问左侧节点并打印值
//,然后访问右侧节点。重复此操作,直到root变为null
私有void索引顺序(节点localRoot){
if(localRoot!=null){
顺序(localRoot.left);
System.out.print(localRoot.element+“”);
顺序(localRoot.right);
}
}
//递归方法-遍历通用BST
//当root不等于null时,打印root的值
//然后访问左侧节点,然后访问右侧节点。重复此操作,直到root变为null
私有void预订单(节点localRoot){
if(localRoot!=null){
System.out.print(localRoot.element+“”);
预订单(localRoot.left);
预订单(localRoot.right);
}
}
//递归方法-遍历通用BST
//而root不等于null访问左节点并访问
//右节点并打印root的值。重复此操作,直到root变为null
私有void postOrder(节点localRoot){
if(localRoot!=null){
postOrder(localRoot.left);
postOrder(localRoot.right);
System.out.print(localRoot.element+“”);
}
}
}
类节点{
受保护的E元素;
左保护节点;
受保护的节点权限;
公共节点(E){
元素=e;
}
}

由于以下原因导致的错误
:java.lang.RuntimeException:不可编译源代码-错误的树类型:花名册.BST
指示-

您运行的代码中有一个或多个java文件未成功或正确编译

我看到的一件事是,您正在使用
BST rost=new BST(),但您没有任何名为
BST
的类。它不应该是
BinarySearchTree rost=newbinarysearchtree()


随后,清理/删除所有旧类文件并重新编译。哇,我完全忽略了这一点,是的,它现在编译成功了。非常感谢。
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;
}
}