找不到二进制搜索树查找方法

时间:2018-04-16 16:09:45

标签: java binary-search-tree

这是我正在做的家庭作业,我遇到了一些麻烦。我已经实现了自己的二叉搜索树版本,而不是使用JDK。我正在根据学生的ID(这是一个类型字符串)将多个学生对象插入到二叉搜索树中。我没有得到任何编译错误但程序仍然返回,当它应该是插入树中的第四个学生时找不到该值。我已经实现了所有的查找方法,但不确定我在哪里出错,因为输出应该是说找到了值。

输出:

run:
11114 not found
BUILD SUCCESSFUL (total time: 0 seconds)

Homework5.class / main:

package homework5;

import java.util.LinkedList;

public class Homework5 {

    static Roster rost = new Roster();

    public static void main(String[] args) {

        addStudent();
        lookupStudent("11114");
    }

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

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

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 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(String id) {
        LinkedList list = new LinkedList();
        list.add(id);
    }
}

Roster.class

class Roster {

    Student root;
    int numStudents;

    BST<Student> roster = new BST<>();

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

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

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

BST.java

package homework5;

class BST<Roster extends Comparable> {

    private Node root;

    public BST() {
        root = null;
    }

    // Generic find method
    public Node 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;

    }

    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.compareTo(current.element) < 0) {
                    current = current.left;
                    if (current == null) {
                        parent.left = newNode;
                        return;
                    }
                } else {
                    current = current.right;
                    if (current == null) {
                        parent.right = newNode;
                        return;
                    }
                }
            }
        }
    }

    // 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 localRoot) {
        if (localRoot != null) {
            inOrder(localRoot.left);
            System.out.print(localRoot.element + " ");
            inOrder(localRoot.right);
        }
    }

}

class Node {

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

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

2 个答案:

答案 0 :(得分:3)

Roster.find()始终返回null。

将其更改为

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

答案 1 :(得分:1)

你的代码看起来很好......像评论建议的一些误解,只需改变那几行

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

并从current.element返回roster.find(),将签名更改为public Student find(String id)

注意:请记住这会影响代码的泛型属性,更好的方法是将节点实现为通用数据容器,例如Node<Student> make roster.find()返回Node个对象并从节点获取数据,以便最后返回Student对象

或者至少只改变这个功能(它比我的解决方案更好)

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