在不使用泛型的情况下从BST删除节点

时间:2015-08-15 05:51:57

标签: binary-search-tree

我似乎找不到从二叉搜索树中删除节点的正确逻辑。

public void delete(int key){
    node current=root;
    int flag=0;
    if(current.data==key){
        System.out.println(key+" is deleted");
        current.rightchild=null;
        current.leftchild=null;
        current=null;
    }
    else{
        while(true){
        if(current.data>key){
            current=current.leftchild;
            if(current==null){
                flag=1;
                break;        
            }
            if(current.data==key){
                System.out.println(key+" is deleted");
                current.leftchild=null;
                current.rightchild=null;
                current=null;

                break;
            }
         }
        else{
           current=current.rightchild;
           if(current==null){
               flag=1;
               break;
           }
           if(current.data==key){
               System.out.println(key+" is deleted");
               current.leftchild=null;
               current.rightchild=null;
               current=null;
               break;
           }
        }
      }
    }
    if(flag==1){`enter code here`
        System.out.println(key+" Not Found");
    }
}

0 个答案:

没有答案