开发者

Why is this binary tree code wrong?

I'm coding something I thought was simple for my programming class but I'm a开发者_JAVA技巧 little stuck :S, could someone take a look at my code, and help me? Thx

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

It doesn't work for any of the 3 cases :S


I think this piece of code looks suspicious

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

Just look, here you assign children of nodo to nodo's right child and then do the following

nodo = nodo.der

In fact you got that nodo.der == nodo.der, so I doubt that you really wanted to achieve such thing.

Just review those pieces of code and I'm sure you'll find where you're wrong.


Some more suspicious places in code:

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

First, if this.predecesor(nodo) really returns the predecesor of nodo then why are you rewriting its children?

Second, deleteDatumABB(nodo.izq.nodeValue) shouldn't it be deleteDatumABB(tmp, datum) or something like this?

And the question from my comment: is not calling deleteDatumABB in cases with only one child right?


Thank you very much, Xappymah, I have corrected my code succesfully and now it works like a charm, to anyone that would like to se the code, here it is :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);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜