在二进制搜索树中排序的麻烦

时间:2014-12-05 21:58:11

标签: java sorting binary-search-tree

我在使这个二叉搜索树工作时遇到了麻烦。这个想法是一个人被放入,然后根据他们的名字进行分类。

我用于人的课程是:

package Tree;

public class Person {
    private int age;
    private String name;
    private String gender;

    public Person( String name, String gender,int age) {
        this.age = age;
        this.name = name;
        this.gender = gender;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    @Override
    public String toString() {
        return "Person [age=" + age + ", name=" + name + ", gender="
                + gender + "]";
    }
}

,搜索树是:

package Tree;

public class BinarySearchPerson {

private boolean empty;
private Person person;
private static BinarySearchPerson left;
private static BinarySearchPerson right;

public BinarySearchPerson(Person person, BinarySearchPerson left,
        BinarySearchPerson right) {
    this.empty = false;
    this.person = person;
    this.left = left;
    this.right = right;
}

public BinarySearchPerson() {
    this.empty = true;
}

public boolean isEmpty() {
    return empty;
}

public Person getPerson() {
    if (isEmpty()) {
        throw new IllegalStateException(
                "Trying to access root of an empty tree");
    }
    return person;
}

public void setPerson(Person person) {
    this.person = person;
}


public BinarySearchPerson getLeft() {
    if (isEmpty()) {
        throw new IllegalStateException(
                                        "Trying to access subtree of an empty tree");
    }
    return left;
}


public void setLeft(BinarySearchPerson left) {
    this.left = left;
}


/**
 * gets the right subtree of this node
 */
public BinarySearchPerson getRight() {
    if (isEmpty()) {
        throw new IllegalStateException(
                                        "Trying to access subtree of an empty tree");
    }
    return right;
}


public void setRight(BinarySearchPerson right) {
    this.right = right;
}



public static BinarySearchPerson insert(Person person, BinarySearchPerson bt){
    int n = person.getName().compareTo(bt.person.getName());


    if (n<0){
        if(bt.getLeft().isEmpty() == true){
            bt.setLeft(new BinarySearchPerson(person,new BinarySearchPerson(),new BinarySearchPerson()));
            return bt;
        }
        else{
            return insert(person, bt.getLeft());

        }
    }

    if (n>0){
        if(bt.getRight().isEmpty() == true){
            bt.setRight(new BinarySearchPerson(person,new BinarySearchPerson(),new BinarySearchPerson()));
            return bt;
        }
        else{
            return insert(person, bt.getRight());
        }
    }
    else return bt;


}



}

我遇到的问题是在底部附近使用名为insert的排序方法。由于某种原因,它只是向左或向右创建无数个分支,具体取决于名称的排序位置。我无法在这里看到我的错误,所以任何帮助都会很棒。

2 个答案:

答案 0 :(得分:1)

中删除静态修改器

私有静态BinarySearchPerson离开; private static BinarySearchPerson right;

树中的每个节点都应该有自己的左右。

答案 1 :(得分:0)

因此,我没有看到您的代码最初实际调用插入,因此我无法验证这一点。但是,您试图从插入方法返回什么?难道不是新创建的BinarySearchPerson吗?如果是这样,您不应该返回新创建的元素而不是您当前所在的元素吗?此外,您正在创建的新BinarySearchPersons不会引用之前的BinarySearchPersons。你应该做这样的事情(对于右边也这样做):

if (n<0){
    if(bt.getLeft().isEmpty() == true){
        BinarySearchPerson newLeft = new BinarySearchPerson(person,new BinarySearchPerson(),bt);
        bt.setLeft(newLeft);
        return newLeft;
    }
    else{
        return insert(person, bt.getLeft());

    }
}