[英]Delete node from a C binary tree without messing it up
我是C语言二叉树库的初学者,我想知道如何从二叉树中删除一个节点而不弄乱整个结构,这是我如何创建树的方法:
结构:
struct Node {
int value;
struct Node *left;
struct Node *right;
};
typedef struct Node TNode;
typedef struct Node *binary_tree;
树的创建:
binary_tree NewBinaryTree(int value_root) {
binary_tree newRoot = malloc(sizeof(TNode));
if (newRoot) {
newRoot->value = value_root;
newRoot->left = NULL;
newRoot->right = NULL;
}
return newRoot;
}
添加元素:
void Insert(binary_tree *tree, int val) {
if (*tree == NULL) {
*tree = (binary_tree)malloc(sizeof(TNode));
(*tree)->value = val;
(*tree)->left = NULL;
(*tree)->right = NULL;
} else {
if (val < (*tree)->value) {
Insert(&(*tree)->left, val);
} else {
Insert(&(*tree)->right, val);
}
}
}
我的问题基本上是如何删除例如一个左节点,然后“链接”链接到该左节点的其他节点(或叶子),从而使树没有NULL叶子? 例如:如果叶子4有2个子节点(left3和right8),则删除叶子4,将子节点left3和right8链接到上节点(在leaf4之上)。很难解释我如何尽力而为。
谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.