我編碼的東西,我認爲是我的編程類的簡單,但我有點卡住:S,有人可以看看我的代碼,並幫助我嗎? 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;
}
它沒有任何的3例工作:S
這是怎麼回事現在錯了嗎? – Argote 2011-03-27 04:00:28
節點不刪除,它不會崩潰或任何東西,但數據只是保持 – 2011-03-27 04:04:59