我正在尝试从二叉搜索树中删除一个节点。但是当我想测试该功能时,我会在“案例 3:两个孩子”中收到一条错误消息。
clist->name = temp->name;
此行会导致错误消息。它说:左操作数必须是左值。我是 C 和二叉搜索树的新手。我无法弄清楚,为什么这条线不起作用。我在互联网上找到了很多关于如何实现删除功能的想法。所有这些方法都使用这行代码。
以下是完整的功能。
(greaterThan、lessThan、toLowerCase 是我实现的辅助功能。它们肯定工作正常)
struct student {
int ID;
char name[20];
struct student *left;
struct student *right;
};
struct student * minValueNode(struct student* node) {
struct student* current = node;
while (current->left != NULL)
current = current->left;
return current;
}
struct student * bst_delete (struct student *clist, char* token, struct student **parent)
{
if (clist == NULL)
return clist;
char* clistName = clist->name;
char* tok = token;
toLowerCase(clistName);
toLowerCase(tok);
if (lessThan(tok, clistName))
clist->left = bst_delete(clist->left, token, &clist->left);
else if (greaterThan(tok, clistName))
clist->right = bst_delete(clist->right, token, &clist->right);
else
{
//Case 1: No children
if (clist->left == NULL && clist->right == NULL) {
free(clist);
clist = NULL;
}
//Case 2: One Child
if (clist->left == NULL) {
struct student *temp = clist;
clist = clist->right;
free(temp);
} else if (clist->right == NULL) {
struct student *temp = clist;
clist = clist->left;
free(temp);
}
//Case 3: Two Children
else {
struct student *temp = minValueNode(clist->right);
clist->name = temp->name;
clist->right = bst_delete(clist->right, temp->right, &clist->right);
}
}
return clist;
}
如果有人能告诉我为什么这条线不起作用,那就太好了。以及如何解决此错误。
谢谢你!