我似乎找不到从二叉搜索树中删除节点的正确逻辑。
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");
}
}