我有一棵二叉树。我试图删除节点之一和他的所有“孩子”。但是,我的函数只删除了孩子,而“父亲”仍然留在那里。我的代码有什么问题?为什么“父亲”没有被删除??
BinTree* familyGotVaccin(BinTree* root)
{
int isFather = 1;
BinTree* father = NULL;
BinTree* gotVaccinated = NULL;
int ID;
if (root == NULL)
return NULL;
printf("Who got vaccinated (ID)?\n");
scanf("%d", &ID);
if (checkIfExist(root, ID) == 0)
{
printf("There is no ID %d\n", ID);
return root;
}
if (root->ID == ID)
{
gotVaccinated = root;
isFather=0;
}
else
{
father = findNode(root, ID);
if (father == NULL)
return root;
if (father->left != NULL && father->left->ID == ID)
gotVaccinated = father->left;
if (father->middle != NULL && father->middle->ID == ID)
gotVaccinated = father->middle;
if (father->right != NULL && father->right->ID == ID)
gotVaccinated = father->right;
}
gotVaccinated=deleteVaccinated(gotVaccinated);
free(gotVaccinated);
if (isFather == 0)
root = NULL;
return root;
}
BinTree* deleteVaccinated(BinTree* root)
{
if (root == NULL)
return NULL;
if (root->left == NULL && root->middle == NULL && root->right == NULL)
{
printf("%s ID: %d Survived!\n", root->name, root->ID);
root = NULL;
free(root);
return NULL;
}
if (root->left != NULL)
root->left=deleteVaccinated(root->left);
if (root->middle != NULL)
root->middle=deleteVaccinated(root->middle);
if (root->right != NULL)
root->right=deleteVaccinated(root->right);
printf("%s ID: %d Survived!\n", root->name, root->ID);
root = NULL;
free(root);
return NULL;
}