아래 코드에서 기본 을 제거하고 작업을 제거하려고했습니다. 그러나 복잡한 제거 부분을 시작하기 전에 리프 노드을 제거하지 못했습니다. 변수가 정의 된 부분과 관련이있을 수 있지만 해결할 수는 없습니다. 어떤 아이디어라도 감사 할 것입니다.이진 검색 트리에서 노드 제거
노드를 삭제하고자하는 부분은 기본적으로 delete temp;
부분으로 구현됩니다 (덧붙여서 내가 삽입하면 delete[]temp;
으로 동작하지 않습니다).
void remove(int value){
if(root==NULL)
cout<<"The list is empty!"<<endl;
else {
Node *temp=root;
while(temp!=NULL)
{
cout<<"Processing: "<<temp->data<<endl;
if(value==temp->data)
{
cout<<"Data verified"<<endl;
//DELETE ROOT
if(temp->left && temp->right) //If it has two children
{
cout<<"Root with two children"<<endl;
return;
}
if(temp->left || temp->right)
{
cout<<"Root with a single child"<<endl;
return;
}
else {
cout<<"Leaf node"<<endl;
delete temp;
return;
}
}
else if(value<temp->data){
if(temp->left)
temp=temp->left;
else
return;
}
else{
if(temp->right)
temp=temp->right;
else
return;
}
}
}
}