-1
我似乎無法找到從二叉搜索樹中刪除節點的正確邏輯。 從BST中刪除節點而不使用泛型
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");
}
}
這是爲了您的請求「邏輯」刪除....它在C++ ....只是要清除 – Bar
是否有可能不使用泛型? –
是的,只是取代預先確定的數據類型... – Bar