英文:
How to destory a binary tree?
问题
The code for destroying the binary tree appears to have a few issues. Here's the corrected code:
void BinaryTreeDestroy(BTNode* root) {
if (root == NULL) {
return;
}
BinaryTreeDestroy(root->left);
BinaryTreeDestroy(root->right);
free(root);
}
Changes made:
- The function name has been changed to
BinaryTreeDestroy
for consistency. - The parameter now takes a single
BTNode*
instead ofBTNode**
. - The conditions for checking if
root
or*root
is NULL have been simplified. - Removed unnecessary
return
statement at the end of the function.
With these changes, the function should correctly destroy the binary tree.
英文:
I created the following binary tree, but I don't know why the code that destorys the binary tree doesn't work.
typedef char BTDataType;
typedef struct BinaryTreeNode
{
BTDataType data;
struct BinaryTreeNode* left;
struct BinaryTreeNode* right;
}BTNode;
BTNode* BinaryTreeCreate(BTDataType* a, int* pi)
{
BTDataType data = a[(*pi)++];
if (data == '#')
return NULL;
BTNode* Node = (BTNode*)malloc(sizeof(BTNode));
if (Node == NULL)
{
perror("malloc fail");
return NULL;
}
Node->data = data;
Node->left = BinaryTreeCreate(a, pi);
Node->right = BinaryTreeCreate(a, pi);
return Node;
}
void BinaryTreeDestory(BTNode** root) {
if (root == NULL || *root == NULL) {
return;
}
BinaryTreeDestory(&(*root)->left);
BinaryTreeDestory(&(*root)->right);
free(*root);
*root = NULL;
return;
}
int main()
{
BTDataType a[] = "ABD##E#H##CF##G##";
int i = 0;
BTNode* root = BinaryTreeCreate(a, &i);
BinaryTreeDestory(&*root);
}
I want to know where the code for destroying the binary tree is wrong and how to correct it
答案1
得分: 2
以下是翻译好的内容:
这一行应该有一个编译器警告
BinaryTreeDestory(&*root);
因为参数的类型错误,而 `&*root` 就是 `root`。这一行应该是
BinaryTreeDestory(&root);
请注意,`BinaryTreeCreate()` 并不对数据进行排序,可以从我添加的一个简单的 `BinaryTreeShow()` 函数中看到。
void BinaryTreeShow(BTNode* root) {
if (root == NULL) {
return;
}
BinaryTreeShow(root->left);
printf("%c", root->data);
BinaryTreeShow(root->right);
}
我认为代码在其他方面都没问题,但 `BinaryTreeDestory()` 在传递父节点的地址时显得多余笨拙。可以通过在节点指针为 `NULL` 时返回,然后调用者可以使用 `free()` 释放节点。
英文:
There should be a compiler warning for this line
BinaryTreeDestory(&*root);
because the argument is the wrong type, and &*root
is just root
. The line should be
BinaryTreeDestory(&root);
Note that BinaryTreeCreate()
doesn't order the data, which can be seen from a simple BinaryTreeShow()
function I added.
void BinaryTreeShow(BTNode* root) {
if (root == NULL) {
return;
}
BinaryTreeShow(root->left);
printf("%c", root->data);
BinaryTreeShow(root->right);
}
I think the code is otherwise OK, but BinaryTreeDestory()
is needlessly clumsy passing the address of the parent. It can be done by returning when the node pointer is NULL
and then the caller can free()
the node.
答案2
得分: 0
Weather Vane提供了正确答案。混淆可能部分来自于传递指向指针的指针。简化您的代码可能只需如下所示,依赖调用者明确处理释放的指针为NULL。然后您可以直接传递根指针,无需取其地址。
void BinaryTreeDestory(BTNode* root) {
if (root == NULL) {
return;
}
BinaryTreeDestory(root->left);
BinaryTreeDestory(root->right);
free(root);
return;
}
int main()
{
BTDataType a[] = "ABD##E#H##CF##G##";
int i = 0;
BTNode* root = BinaryTreeCreate(a, &i);
BinaryTreeDestory(root);
root = NULL; // not needed, but it is good form
}
英文:
Weather Vane provided the correct answer. Some of the confusion could be from passing pointers to pointers. A simplification of your code could be simply be this and rely on the caller to explicitly deal with setting the free'd pointer to NULL. Then you can just pass the root pointer without taking the address of it.
void BinaryTreeDestory(BTNode* root) {
if (root == NULL) {
return;
}
BinaryTreeDestory(root->left);
BinaryTreeDestory(root->right);
free(root);
return;
}
int main()
{
BTDataType a[] = "ABD##E#H##CF##G##";
int i = 0;
BTNode* root = BinaryTreeCreate(a, &i);
BinaryTreeDestory(root);
root = NULL; // not needed, but it is good form
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论