从二叉搜索树打印链表

时间:2018-04-17 17:15:47

标签: java linked-list binary-search-tree

我正在编写一个程序,该程序具有一个二进制搜索树(Roster),Student对象通过它们的String Id插入。每个学生都有一个链接列表,其中添加了课程,其中包含课程的字符串和成绩。二叉搜索树是我自己的实现版本。

我无法实施我的方法来打印所有包含特定课程的学生。我认为我的printCourse方法中的实现是关闭的,因为我无法从我的Roster类下的displayStudent()方法调用它。这是一项家庭作业,我已经实施了大部分其他方法,只是在努力解决这个问题,非常感谢任何帮助!

  

displayStudents(" Math161&#34);

是我正在努力实施的方法。它从我名册中的方法调用,我试图通过我的BST搜索

在我的BST中,方法printCourse()应该检查每个学生链表,如果它包含上面列出的课程,并打印每个学生。这是我到目前为止所做的,这是不正确的:

> public void printCourse(Node n, String course) { 
>         if (n != null) {
>             inOrder(n);
>             if (n.element.getId().equals(course)) { 
>                 System.out.print(n.element.getId() + " ");
>             }
>         }
>     }

Homework5.class / Main:

public class Homework5 {

    static Roster rost = new Roster();

    public static void main(String[] args) {

        addStudent();
        displayAllStudents();
        lookupStudent("11114");
        addCourse();
        displayStudents("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.displayAllStudents();
    }

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

Student.class:

class Student implements Comparable<Student> {

    String id;
    String firstName;
    String lastName;

    LinkedList<Course> courses = new LinkedList<>();

    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 String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @Override
    public int compareTo(Student other) {    
        return this.getId().compareTo(other.getId());
    }

    public void addCourse(Course course) {
        courses.add(course);
    }

}

Course.class:

class Course {

    LinkedList<Course> course = new LinkedList<>();

    String id;  // course id
    int grade;

    Course(String id, int grade) {
        this.id = id;
        this.grade = grade;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public int getGrade() {
        return grade;
    }

    public void setId(int grade) {
        this.grade = grade;
    }

}

Roster.class:

class Roster {

    Student root;
    int numStudents;

    BST<Student> roster = new BST<>();
    LinkedList<Course> courseList = new LinkedList<>();

    public Roster() {
        root = null;
        numStudents = 0;
    }

    public void addStudent(Student st) {
        roster.insert(st);
        numStudents++;
    }

    public void displayAllStudents() {
        roster.traverse(2);
    }

    public Student find(String id) {
        return roster.find(id);
    }

    public void addCourse(String id, Course course) {
        Student student = roster.find(id);
        student.addCourse(course);      
    }

    public void displayStudents(String courseId) {
        roster.printCourse(courseId);
    }
}

BST.java

class BST<Roster extends Comparable> {

    private Node root;

    public BST() {
        root = null;
    }

    // Generic find method
    public Student find(String id) {

        Node current = root;

        while (id.compareTo(current.element.getId()) != 0) {
            if (id.compareTo(current.element.getId()) < 0) {
                current = current.left;
            } 
            else {
                current = current.right;
            }
            if (current == null) {
                return null;
            }
        }
        return current.element;

    }

    public void insert(Student st) {
        Node newNode = new Node(st);

        if (root == null) {
            root = newNode;
        } else {
            Node current = root;
            Node parent = null;

            while (true) {
                parent = current;
                if (st.getId().compareTo(current.element.getId()) < 0) {
                    current = current.left;
                    if (current == null) {
                        parent.left = newNode;
                        return;
                    }
                } else {
                    current = current.right;
                    if (current == null) {
                        parent.right = newNode;
                        return;
                    }
                }
            }
        }
    }

    public void printCourse(Node n, String course) { 
        if (n != null) {
            inOrder(n);
            if (n.element.getId().equals(course)) { 
                System.out.print(n.element.getId() + " ");
            }
        }
    }

    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("\nList of all students:  ");
                inOrder(root);
                break;
            case 3:
                System.out.print("\nPostorder traversal: ");
                // call postOrder(root) and implement postOrder()
                postOrder(root);
                break;
        }
        System.out.println();
    }

    private void inOrder(Node localRoot) {
        if (localRoot != null) {
            inOrder(localRoot.left);
            System.out.print(localRoot.element.getId() + " ");
            inOrder(localRoot.right);
        }
    }

    private void preOrder(Node localRoot) {
        if (localRoot != null) {
            System.out.print(localRoot.element + " ");
            preOrder(localRoot.left);
            preOrder(localRoot.right);
        }
    }

    private void postOrder(Node localRoot) {
        if (localRoot != null) {
            postOrder(localRoot.left);
            postOrder(localRoot.right);
            System.out.print(localRoot.element + " ");
        }
    }

}

class Node {

    protected Student element;
    protected Node left;
    protected Node right;

    public Node(Student st) {
        element = st;
    }

}

1 个答案:

答案 0 :(得分:0)

我不知道这是否一切,但我认为看似错误的两件事是:

  1. 在Roster.class中:displayStudents()中的roster.printCourse(courseId)调用只包含一个参数,即courseId,但BST.java中的printCourse()方法有两个参数,一个Node和一个String。 / LI>
  2. 在Roster.class中:名为名册的BST被赋予类型<Student>,但BST.java中BST的定义包含一个名单,而不是学生。注意:您无需使用&lt;&gt;内部类型除非该类型可以更改。如果BST只能应用于Student类型,那么在定义或创建BST时不需要它。另一方面,如果您的BST应该适用于任何类型的数据,您应该将BST定义为BST类<T extends Comparable>,并确保在类型应该去的地方使用T. (查看Google上的通用类型以获取更多示例。)