AddressBook使用java中的二叉搜索树

时间:2013-04-02 15:41:54

标签: java

您好我正在用java编写AddressBook应用程序,我编写了整个程序。唯一的事情是它没有按预期工作,代码中没有错误,所以我无法解决它。任何帮助都会非常有用。

编辑:这不是一个重复的问题,因为这包括所有方法。另一个没有主要方法,并且是不完整的,因为它被关闭,我被迫提出一个新问题。有意义吗?

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

1 个答案:

答案 0 :(得分:2)

一方面:

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

同样,您也会忽略insertinsert的返回值。你应该以类似的方式处理它们。