【问题标题】:Printing linked list from binary search tree从二叉搜索树打印链表
【发布时间】:2018-04-17 17:15:47
【问题描述】:

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

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

displayStudents("Math161");

是我正确努力实施的方法。它从我的花名册类中的方法调用,我正在尝试实现该方法来搜索我的 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);
    }
}

学生班级:

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

}

课程.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;
    }

}

名册.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;
    }

}

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-07
      相关资源
      最近更新 更多