2017-04-01 22 views
-1

目的のノードを削除できないのはなぜですか?たとえば、この順番で1,2,3,4,5のエントリを持つツリーを作成し、私のdeleteメソッドを使って3を削除すると、3以下のノードが削除され、3だけ削除され、4,5もし私が再びすべてのノードをpreorder.Please助けてください印刷します。Javaのバイナリ検索ツリーの削除

import java.util.Scanner;

クラスTNODE {

protected int data; 

protected tnode left,right; 

public tnode(){ 

    data=0; 

    right=left=null; 

} 
public tnode(int v){ 

    data=v; 

    right=left=null; 
} 

public int getdata(){ 

    return data; 
} 

public tnode getleft(){ 

    return left; 
} 

public tnode getright(){ 

    return right; 
} 

}クラスBツリー{

protected tnode root; 

public btree(){ 

    root=null; 
} 

public boolean isEmpty(){ 

    return root==null; 
} 

public void insert(int val){ 

    root=insert(root,val); 
} 

private tnode insert(tnode r,int val){ 

    if(r==null){ 

     r=new tnode(val); 
    } 

    else{ 

     if(val>r.getdata()){ 

      r.right=insert(r.right,val); 
     } 

     else{ 

      r.left=insert(r.left,val); 
     } 
    } 

    return r; 
} 
public void preorder(){ 

    preorder(root); 
} 

private void preorder(tnode r){ 

    if(r!=null){ 

     System.out.print(r.getdata()+" "); 

     preorder(r.getleft()); 

     preorder(r.getright()); 
    } 
} 

public int min(){ 

    return min(root); 

} 

public int min(tnode r){ 

    if(r.left==null){ 

     return r.getdata(); 

    } 

    else{ 

     return min(r.left); 

    } 

} 

public void delete(int val){ 

    root=delete(root,val); 

} 

private tnode delete(tnode r,int val){ 

    if(r==null){ 

     return null; 

    } 

    else if(val>r.getdata()){ 

     r=delete(r.right,val); 

    } 

    else if(val<r.getdata()){ 

     r=delete(r.left,val); 

    } 

    else{// when r.data=value 

     //if node has both children 

     if(r.left!=null && r.right!=null){ 

      tnode temp=r; 

      //get the minimum value in right sub tree 

      int min_right=min(temp.right); 

      //replace this with the node to be deleted 

      r.data=min_right; 

      //delete this node 

      r=delete(r.right,min_right); 

     } 

     //if has left child 

     else if(r.left!=null){ 

      r=r.left; 

     } 

     //if has right child 

     else if(r.right!=null){ 

      r=r.right; 

     } 

     else{ 

      //if has no child 

      r=null; 

     } 

    } 

    return r; 

} 

}

答えて

0
private tnode delete(tnode r,int val){ 

if(r==null){ 

    return null; 

} 

else if(val>r.getdata()){ 

    r=delete(r.right,val); //it should be r.right=delete(r.right,val); 

} 

else if(val<r.getdata()){ 

    r=delete(r.left,val); //it should be r.left=delete(r.left,val); 

} 

else{// when r.data=value 

    //if node has both children 

    if(r.left!=null && r.right!=null){ 

     tnode temp=r; 

     //get the minimum value in right sub tree 

     int min_right=min(temp.right); 

     //replace this with the node to be deleted 

     r.data=min_right; 

     //delete this node 

     r.right=delete(r.right,min_right); 

    } 

    //if has left child 

    else if(r.left!=null){ 

     r=r.left; 

    } 

    //if has right child 

    else if(r.right!=null){ 

     r=r.right; 

    } 

    else{ 

     //if has no child 

     r=null; 

    } 

} 

return r; 

}

関連する問題