在java中使用二进制搜索树的AddressBook

在java中使用二进制搜索树的AddressBook,java,Java,您好,我正在用java编写一个地址簿应用程序,我已经编写了整个程序。唯一的问题是,它没有按预期工作,代码中没有错误,因此我无法对其进行故障排除。任何帮助都是非常感激的 编辑:这不是一个重复的问题,因为它包括所有的方法。另一个没有主方法,而且不完整,因为它关闭了,我不得不问一个新问题。有道理吗 package com.addressbook; public abstract class KeyedItem<KT extends Comparable<? super KT>>

您好,我正在用java编写一个地址簿应用程序,我已经编写了整个程序。唯一的问题是,它没有按预期工作,代码中没有错误,因此我无法对其进行故障排除。任何帮助都是非常感激的

编辑:这不是一个重复的问题,因为它包括所有的方法。另一个没有主方法,而且不完整,因为它关闭了,我不得不问一个新问题。有道理吗

package com.addressbook;

public abstract class KeyedItem<KT extends Comparable<? super KT>> {
private KT searchKey;

public KeyedItem(KT key){
    searchKey = key;
}

public KT getKey(){
    return searchKey;
}
}




package com.addressbook;

public class People extends KeyedItem<String> {
private String address;
private String phone;


public People(String n, String a, String p){
    super(n);
    this.address = a;
    this.phone = p;
}

public void setAddress(String a){
    address = a;
}

public void setPhone(String p){
    phone = p;
}

public String toString(){
    return "Name:" + getKey() + "\nAddress:" + address + "\nPhone:" + phone;
}

public String getTheKey(){
    return getKey();
}
}



package com.addressbook;

public class BinaryNode{
// Friendly data; accessible by other package routines
private People people;      // The data in the node
private BinaryNode left;     // Left child
private BinaryNode right;    // Right child
// Constructors

public BinaryNode(People pe, BinaryNode l, BinaryNode r) {
    people = pe;
    left = l;
    right = r;
}

public BinaryNode(People pe) {
    people = pe;
    left = right = null;
}

public void setData(People p){
    people = p;
}

public String getSearch(){
    return people.getTheKey();
}

public People getData(){
    return people;
}

public BinaryNode getLeft(){
    return left;
}

public BinaryNode getRight(){
    return right;
}
}



package com.addressbook;

import com.addressbook.People;
import com.addressbook.BinaryNode;

public class AddressBook {
private BinaryNode root;

public AddressBook() {
    super();
}

public AddressBook(People p) {
    super();
    root = new BinaryNode(p);
}

public void insert(People p){
    insert(p, root);
}

public People get(String key) {
    BinaryNode node = root;
    while (node != null) {
        if (key.compareTo(node.getSearch()) == 0) {
            return node.getData();
        }
        if (key.compareTo(node.getSearch()) < 0) {
            node = node.getLeft();
        } else {
            node = node.getRight();
        }
    }
    return null;
}

protected BinaryNode insert(People p, BinaryNode node) {
    if (node == null) {
        return new BinaryNode(p);
    }
    if (p.getTheKey().compareTo(node.getSearch()) == 0) {
        return new BinaryNode(p, node.getLeft(), node.getRight());
    } else {
        if (p.getTheKey().compareTo(node.getSearch()) < 0) {    // add to left subtree
            insert(p, node.getLeft());
        } else {                                                // add to right subtree
            insert(p, node.getRight());
        }
    }
    return node;
}

public void remove(String key) {
    remove(key, root);
}

protected BinaryNode remove(String k, BinaryNode node) {
    if (node == null) { // key not in tree
        return null;
    }
    if (k.compareTo(node.getSearch()) == 0) { // remove this node
        if (node.getLeft() == null) { // replace this node with right child
            return node.getRight();
        } else if (node.getRight() == null) { // replace with left child
            return node.getLeft();
        } else {
            // replace the value in this node with the value in the
            // rightmost node of the left subtree
            node = getRightmost(node.getLeft());
            // now remove the rightmost node in the left subtree,
            // by calling "remove" recursively
            remove(node.getSearch(), node.getLeft());
            // return node;  -- done below
        }
    } else {        // remove from left or right subtree
        if (k.compareTo(node.getSearch()) < 0) {
            // remove from left subtree
            remove(k, node.getLeft());
        } else {        // remove from right subtree
            remove(k, node.getRight());
        }
    }
    return node;
}

protected BinaryNode getRightmost(BinaryNode node) {
    assert(node != null);
    BinaryNode right = node.getRight();
    if (right == null) {
        return node;
    } else {
        return getRightmost(right);
    }
}

protected String toString(BinaryNode node) {
    if (node == null) {
        return "";
    }
    return node.getData().toString() + "(" + toString(node.getLeft()) + ", " +
        toString(node.getRight()) + ")";
}

public static void main(String[] arguments) {
    AddressBook tree = new AddressBook();

    People p1 = new People("person 1", "adresa 1", "404040404");
    People p2 = new People("person 2", "adresa 2", "4040434345");
    People p3 = new People("person 3", "adresa 3", "346363463");
    People p4 = new People("person 4", "adresa 4", "435346346");
    People p5 = new People("person 5", "adresa 5", "4363907402");

    tree.insert(p1);
    tree.insert(p2);
    tree.insert(p3);
    tree.insert(p4);
    tree.insert(p5);

    System.out.println(tree.get("person 1"));
    }
}
package com.addressbook;
公共抽象类KeyedItem一方面:

public void insert(People p){
    insert(p, root);
}
但这种方法是从

protected BinaryNode insert(People p, BinaryNode node) {
    if (node == null) {
        return new BinaryNode(p);
    }
这意味着,将这两部分放在一起考虑,您总是忽略新的根,因此您的树永远不会填充。请尝试以下方法:

public void insert(People p){
    root = insert(p, root);
}

同样,您也可以忽略
insert
内部
insert
的返回值。您应该以类似的方式处理它们。

这需要查看大量代码。你期望它如何工作,它有什么不同之处?你能把范围缩小到代码的某一部分吗?到底是什么问题?问题是,当我在BST中插入5个元素,然后执行println时,它总是返回null而不是实际的person。可能insert方法不正确。故障排除的第一步应该是使用调试器逐步检查代码。假设您使用某种不会出现任何问题的IDE(Eclipse或类似产品)。我使用Eclipse,但无法调试代码,尝试了多次,Eclipse抛出错误,尝试重新安装,以及同样的事情:(错误是无法连接到VM,我已经按照mac安装的所有说明进行了操作。
AddressBook
构造函数初始化
root
。为什么会是
null
?有两个构造函数,但只有一个初始化
root
。猜猜使用了哪一个?@a.H-对,你错过了。你可能想知道将这一事实添加到您的答案中。@PM77-1:这与原始问题无关-所以不要混淆问题。谢谢您,我正在用新的建议重构代码,并尝试一下。