创建二进制搜索树

时间:2012-07-19 04:26:25

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

好的,所以我正在尝试创建一个二叉搜索树,每个节点都包含对某个对象的引用,以及对其左子节点的引用和对其右子节点的引用(总共3个变量)。左边的孩子必须总是比父母小,而右边的孩子总是要比父母更大。我必须创建两个方法:1方法(contains())来检查元素是否在树中,以及add()方法将元素添加到树中的适当位置。

这是BinarySearchTree类:

public class BinarySearchTree extends BinaryTree {

public BinarySearchTree(TreeNode t){
    super(t);
}

public boolean contains (Comparable obj){
    if (this.myRoot != null){
        return containshelper(obj, this.myRoot);
    }
    return false;

}

public static boolean containshelper(Comparable comp, TreeNode t){
    boolean flag = true;
    int x = comp.compareTo(t.myItem);
    if (x < 0 && t.myLeft != null){
        containshelper(comp,t.myLeft);
    }
    if (x > 0 && t.myRight != null){
        containshelper(comp,t.myRight);
    }
    if (x == 0){
        return true;
    }
    return false;
}


public void add(Comparable key) {
    if (!this.contains(key)){
        if (this.myRoot != null){
            add(myRoot, key);
        }
        System.out.print("Getting Here ");
    }
    else {
        System.out.print("Tree already contains item");
    }
}

private static TreeNode add(TreeNode t, Comparable key) {

    if (((Comparable) t.myItem).compareTo(key) < 0 && t.myLeft != null){
        add(t.myLeft,key);

    }
    if(((Comparable) t.myItem).compareTo(key) > 0 && t.myRight != null ) {
        add(t.myRight,key);

    }
    if (((Comparable) t.myItem).compareTo(key) < 0 && t.myLeft == null){
        TreeNode q = new TreeNode(key);
        t.myLeft = q;

    }
    if (((Comparable) t.myItem).compareTo(key) > 0 && t.myRight == null ){
        TreeNode w = new TreeNode(key);
        t.myRight = w;

    }
    return t;
}
}

这是TreeNode类(包含在BinaryTree中):

    public static class TreeNode {

    public Object myItem;
    public TreeNode myLeft;
    public TreeNode myRight;
    public int size;

    public TreeNode (Object obj) {
        size = size(this);
        myItem = obj;
        myLeft = myRight = null;
    }
    public int size(TreeNode t) { 
        return(sizehelper(t)); 
    }
    private int sizehelper(TreeNode node) { 
        if (node == null){ 
            return(0); 
        }
        else { 
            return(size(node.myLeft) + 1 + size(node.myRight)); 
        } 
    }


    public TreeNode (Object obj, TreeNode left, TreeNode right) {
        myItem = obj;
        myLeft = left;
        myRight = right;
    }
}
}

我很确定我的contains()方法有效,但对于我的生活,我无法弄清楚为什么add()不起作用。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

containsHelper

中有一些错误
public static boolean containshelper(Comparable comp, TreeNode t){

    // TreeNode.myItem should be declared of type `Comparable` instead of `Object`
    int x = comp.compareTo(t.myItem);
    if (x < 0 && t.myLeft != null){
        return containshelper(comp,t.myLeft); // where does result of this call go? We must return the result !!
    }
    if (x > 0 && t.myRight != null){
        return containshelper(comp,t.myRight); // this too ?
    }
    if (x == 0){
        return true;
    }
    return false;
}

这里也有同样的错误:

private static TreeNode add(TreeNode t, Comparable key) {

if (t.myItem.compareTo(key) < 0 && t.myLeft != null){
    t = add(t.myLeft,key); // where does returned result go?
}
else if (t.myItem.compareTo(key) > 0 && t.myRight != null ) {
    t = add(t.myRight,key);
}
else if (t.myItem.compareTo(key) < 0 && t.myLeft == null){
    TreeNode q = new TreeNode(key);
    t.myLeft = q;
}
else if (t.myItem.compareTo(key) > 0 && t.myRight == null ){
    TreeNode w = new TreeNode(key);
    t.myRight = w;
}
return t;
}

您应声明类型为TreeNode.myItem的{​​{1}},以强制每个类都适合您的二进制搜索,以提供Comparable方法。这比将对象转换为compareTo更好,因为对象可能没有实现Comparable,然后会导致运行时错误