为什么这个二叉树代码错了?

时间:2011-03-27 03:55:38

标签: java tree binary-tree

我正在编写一些我觉得编程类很简单的东西,但是我有点卡住:S,有人可以查看我的代码,并帮助我吗?谢谢
Der = right;
Izq = left;

    public boolean eliminaDatoABB(T dato){      
       if(this.isEmpty()||this.containsABBRecursivo(dato)==false)
           return false;
       return eliminaDatoABBUtil(this.raiz, dato);

     }

    private boolean deleteDatumABBUtil(NodeTree<T> nodo, T datum){
    if(nodo==null)
        return false;
    int i = nodo.nodeValue.compareTo(dato);
    if(i==0){ //Found it
        //Cases
        if(isLeaf(nodo)){ //Is leaf
            nodo=null;
            return true;
        }
        else
            if((hasChildRight(nodo)&&!hasChildLeft(nodo))){ //Has just one child at right
                NodoArbol<T> tmp = new NodoArbol<T>(nodo.der.nodeValue);
                tmp.der=nodo.der.der;
                tmp.izq=nodo.der.izq;
                nodo=tmp;
                //The trash collector should then delete the nodo.der
                return true;
            }
            else
                if((!hasChildRight(nodo)&&hasChildLeft(nodo))){ //Has just one child at left
                    NodoArbol<T> tmp = new NodoArbol<T>(nodo.izq.nodeValue);
                    tmp.izq=nodo.izq.izq;
                    tmp.der=nodo.izq.der;
                    nodo=tmp;
                    //The trash collector should then delete the nodo.izq
                    return true;
                }
                else{ //Has two children
                    NodoArbol<T> tmp = this.predecesor(nodo);
                    tmp.der=nodo.der;
                    tmp.izq=nodo.izq;
                    return deleteDatumABB(nodo.izq.nodeValue);
                }
    }
    else{ //Search the datum
        if(i>0)
            return deleteDatumABBUtil(nodo.izq, dato);
        else
            return deleteDatumABBUtil(nodo.der, dato);
    }
}

//Bonus Methods

public boolean isLeaf(NodeTree<T> nodo){
    return (nodo.der==null&&nodo.izq==null);
}

public boolean hasChildRight(NodeTree<T> nodo){
    return nodo.der!=null;
}

public boolean hasChildLeft(NodeTree<T> nodo){
    return nodo.izq!=null;
}

对于3种情况中的任何一种都不起作用:S

3 个答案:

答案 0 :(得分:1)

我认为这段代码看起来很可疑

nodo.der.der=nodo.der;
nodo.der.izq=nodo.izq;

请注意,在这里您将nodo的孩子分配给nodo的正确孩子,然后执行以下操作

nodo = nodo.der

事实上你得到了nodo.der == nodo.der,所以我怀疑你是否真的想要实现这样的目标。

只需查看这些代码,我们确定您会找到您错误的地方。

答案 1 :(得分:0)

代码中有一些可疑的地方:

NodoArbol<T> tmp = this.predecesor(nodo);
tmp.der=nodo.der;
tmp.izq=nodo.izq;
return deleteDatumABB(nodo.izq.nodeValue);

首先,如果this.predecesor(nodo)真的返回nodo的前身,那你为什么要重写它的孩子?

其次,deleteDatumABB(nodo.izq.nodeValue)不应该是deleteDatumABB(tmp, datum)或类似的东西吗?

我的评论中提出的问题是:在只有一个孩子的情况下,不是在调用deleteDatumABB吗?

答案 2 :(得分:0)

非常感谢,Xappymah,我已经成功纠正了我的代码,现在它就像一个魅力,对于任何想要代码的人来说,这里是:D:

public boolean deleteDatumABB(T datum){
if(this.isEmpty())
    return false;
return deleteDatumABBUtil(this.root, datum);

}
private boolean deleteDatumABBUtil(NodeTree<T> nodo, T datum){
    if(nodo==null)
        return false;
    int i = nodo.nodeValue.compareTo(dato);
    if(i==0){ //Found it :D
        //Base cases
        if(isLeaf(nodo)){ //Is Leaf
            if(this.root==nodo){
                this.root=null;
                return true;
            }
            else{
                NodeTree<T> parent = getParentABBRecursivePriv(root, datum);
                if(dato.compareTo(parent.nodeValue)<0)
                    parent.izq=null;
                else
                    parent.der=null;    
                return true;
            }
        }
        else
            if((hasChildRight(nodo)&&!hasChildLeft(nodo))){ //Just one kind at right
                if(nodo==this.root){
                    this.root=nodo.der;
                    return true;
                }
                else{
                    NodeTree<T> parent = getParentABBRecursivoPriv(root, dato);
                    if(datum.compareTo(prent.nodeValue)<0)
                        parent.izq = nodo.der;
                    else
                        parent.der = nodo.der;
                    return true;
                }
            }
            else
                if((!hasChildRight(nodo)&&hasChildLeft(nodo))){ //TJust one child at left
                    if(nodo==this.root){
                        this.root=nodo.izq;
                        return true;
                    }
                    else{
                        NodeTree<T> parent = getParentABBRecursivePriv(root, datum);
                        if(datum.compareTo(parent.nodeValue)<0)
                            parent.izq = nodo.izq;
                        else
                            parent.der = nodo.izq;
                        return true;
                    }
                }
                else{ //Children in both
                    if(nodo==this.raiz){

                        NodoArbol<T> pred = new NodeTree<T>(predecesor(nodo).nodeValue);
                        pred.der = this.root.der;
                        pred.izq = this.root.izq;

                        this.root = pred;
                        deleteDatumABBUtil(nodo.izq, root.nodeValue);



                        return true;
                    }
                    else{

                        NodeTree<T> parent = getParentABBRecursivePriv(root, datum);
                        NodoArbol<T> pred = new NodeTress<T>(predecesor(nodo).nodeValue);
                        pred.der = padre.der.der;
                        pred.izq = padre.der.izq;
                        deleteDatumABBUtil(nodo.izq, pred.nodeValue);
                        parent.der = pred;
                        return true;
                    }

                }
    }
    else{ //Search
        if(i>0)
            return deleteDatumABBUtil(nodo.izq, datum);
        else
            return deleteDatumABBUtil(nodo.der, datum);
    }
}