c - 我在删除 C 中的树数据结构中的节点时遇到问题
问题描述
删除函数中的代码适用于具有 1 个子节点和离开节点的节点,但是当我输入 12(节点有 1 个子节点)时,该节点仍然存在,删除函数是否有任何问题:
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
我希望输出 3、4、5、12、13、15、20 为 3、4、5、13、15、20,但是当我输入 12 时节点 12 仍然存在。
解决方案
推荐阅读
- node.js - NPM 未启动
- core-data - CoreData 模型架构和关系
- ruby - 如何测试具有本地 GEM 依赖项的 GEM,$LOADED_FEATURES 未指向我的本地 GEM
- ios - Xcode 和 SwiftUI - 如何重置画布?
- apache - 如何通过apache下载文件?
- python - Jupyter 上的雪花连接器
- python - 分类数据的线性回归
- reactjs - 无法让 Heroku App 运行,但我可以让它在本地运行
- azure - 如何在 Azure ADF 中进行行转换?
- rust - 为什么从
for U在 Rust 中意味着 Into for T?