2011-03-27 55 views
0

我編碼的東西,我認爲是我的編程類的簡單,但我有點卡住: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

+2

這是怎麼回事現在錯了嗎? – Argote 2011-03-27 04:00:28

+0

節點不刪除,它不會崩潰或任何東西,但數據只是保持 – 2011-03-27 04:04:59

回答

1

我覺得這段代碼看起來可疑

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

你看,在這裏你指定的nodonodo兒童然後執行以下操作:

nodo = nodo.der 

其實你有nodo.der == nodo.der,所以我懷疑你是否真的想達到這樣的目的。

只需查看這些代碼段,我相信你會發現你錯了。

+0

你是對的,我想我已經糾正了這段代碼,但問題仍然存在:S – 2011-03-27 04:19:41

+0

嗯,但'節點'的即使'nodo'只有一個,右(左)小孩本身可以有超過1個孩子:)或者它應該是這樣的設計?順便說一句,爲什麼你不在這種情況下調用'deleteDatumABB'? – xappymah 2011-03-27 04:26:50

0

在代碼中的一些更可疑的地方:

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

首先,如果真的this.predecesor(nodo)返回nodo的predecesor那麼你爲什麼要重寫它的孩子嗎?

二,deleteDatumABB(nodo.izq.nodeValue)不應該是deleteDatumABB(tmp, datum)或類似的東西?

而我的評論中的問題是:在只有一個孩子的情況下沒有調用deleteDatumABB

+0

它返回一個剛剛有predecesor nodeValue的新節點,它具有der&izq屬性的空值,所以我只是定義了時間節點的der和izq屬性以及刪除的節點,然後刪除了predecesor節點(現在重複)。
然後我添加了一個方法,它沒有被複制,只是接收到數據的機會。
第三個問題,我糾正了:) – 2011-03-27 04:58:22

+0

這真的讓我覺得,即使葉節點,我已調試,當我站在所需的節點,並希望將其刪除(null它)成功地將它歸零並返回true(迄今爲止都是這樣),但是當我打印樹時,節點奇蹟般地重現! – 2011-03-27 05:23:35

+1

這是因爲'nodo'實際上只是你的功能的一個參數。所以,如果你寫'nodo = null',那麼你只需要取消參數引用。如果你想從樹中刪除這個節點,你需要在其父節點中取消引用。 – xappymah 2011-03-27 06:26:46

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