我在C中删除树数据结构中的节点时遇到问题
delete函数中的代码适用于具有1个子节点和离开节点的节点,但当我输入12(该节点具有1个子节点)时,该节点仍然存在,delete函数是否存在任何问题:我在C中删除树数据结构中的节点时遇到问题,c,C,delete函数中的代码适用于具有1个子节点和离开节点的节点,但当我输入12(该节点具有1个子节点)时,该节点仍然存在,delete函数是否存在任何问题: void Delete(struct Node *current, int data){ struct Node *X; if((current == NULL) printf("Can not delete that node since the tree is empty"); else{
void Delete(struct Node *current, int data){
struct Node *X;
if((current == NULL)
printf("Can not delete that node since the tree is empty");
else{
if(data > current->data)
Delete(current->rightchild, data);
else(data < current->data)
Delete(current->leftchild, data);
else{
X = current;
if(current->leftchild == NULL){
current = current->rightchild;
}
else(current->rightchild == NULL){
current = current->leftchild;
}
X = NULL;
}
}
int main(){
int tree[7] = {5, 4, 15, 3, 12, 20, 13};
struct Node *current;
int I = 0;
for(; I < 7; ++I){
Tree(tree[I]); // Tree is the tree-creating function
}
current = root;
printf("Entering the node u want to delete: ");
scanf("%d", &j);
Delete(current, j);
InOrder_Traversal(root); // this is the printing function
void Delete(结构节点*当前,整数数据){
结构节点*X;
如果((当前==NULL)
printf(“树为空,无法删除该节点”);
否则{
如果(数据>当前->数据)
删除(当前->右子项,数据);
其他(数据<当前->数据)
删除(当前->左侧子项,数据);
否则{
X=电流;
如果(当前->左子项==NULL){
当前=当前->右子项;
}
else(当前->rightchild==NULL){
当前=当前->左子项;
}
X=零;
}
}
int main(){
int-tree[7]={5,4,15,3,12,20,13};
结构节点*当前;
int I=0;
对于(;I<7;++I){
Tree(Tree[I]);//Tree是树创建函数
}
电流=根;
printf(“输入要删除的节点:”);
scanf(“%d”和“&j”);
删除(当前,j);
按顺序遍历(根);//这是打印函数
我希望输出3、4、5、12、13、15、20是3、4、5、13、15、20,但当我输入12时节点12仍然在那里。
current=current->rightchild;
调用者不会知道这一点。current
是一个局部变量。你能帮我解决这个问题吗,我真的不明白你的意思查找“按值传递vs按引用传递”@NguyễnTrọngCương?current=current->rightchild;
调用者不会知道这一点。current
是一个局部变量。你能帮我解决这个问题吗,我真的不明白你的意思查找“按值传递vs按引用传递”。@NguyễnTrọngCương?