如何使用双指针释放树结构?

时间:2022-06-01 18:44:32

I have to free a tree and set his root to NULL using a particular function. I tried to use a recoursive method. But if I compile i get some warnings about "incompatible pointer type" and I'm not able to resolve it. This is the struct:

我必须使用特定函数释放树并将其根设置为NULL。我试图使用一种递归方法。但是,如果我编译我得到一些关于“不兼容的指针类型”的警告,我无法解决它。这是结构:

typedef struct node {
int key; 
struct node *left, *mid, *right;
} node_t;

And here the function. The first line cannot be changed:

而这里的功能。第一行无法更改:

void free_tree (node_t ** root){
if(root != NULL){
    free_tree((*root)->left);
    free_tree((*root)->mid);
    free_tree((*root)->right);
    free(*root);
    }
return;
}

Any help would be appreciated

任何帮助,将不胜感激

2 个解决方案

#1


1  

Your function expected a pointer to a pointer-to-node. You're giving it a pointer-to-node three times in your recursive calls. Further, you're not validating that the pointer-to-pointer, and the pointer it points to, are non-null; you're only validating the former.

您的函数需要一个指向节点指针的指针。你在递归调用中给它一个指向节点的指针三次。此外,您没有验证指向指针的指针及其指向的指针是否为空;你只是在验证前者。

In short, your function should look like this:

简而言之,您的功能应如下所示:

void free_tree (node_t ** root)
{
    if(root && *root)
    {
        free_tree(&(*root)->left);
        free_tree(&(*root)->mid);
        free_tree(&(*root)->right);
        free(*root);
        *root = NULL;
    }
}

The last functional line is optional, but frankly it's pointless to do this with pointers-to-pointers unless you're going to do that anyway, as it sets the caller's pointer to NULL after obliterating the tree. Given a properly built tree, your caller should deliver the address of the tree root when destroying the entire tree, as:

最后一个功能行是可选的,但坦率地说,使用指针指向它是没有意义的,除非你打算这样做,因为它在删除树之后将调用者的指针设置为NULL。给定一个正确构建的树,您的调用者应该在销毁整个树时提供树根的地址,如下所示:

node_t *root = NULL;

// ... build tree ...

free_tree(&root);

// root is now NULL; tree is destroyed

#2


1  

Your question cannot be answered very clearly but at least I can tell you why you have this warning about incompatible pointer type :

你的问题不能很清楚地回答,但至少我可以告诉你为什么你有关于不兼容指针类型的警告:

Your function prototype is

你的函数原型是

void free_tree (node_t ** root);

It's argument is a node_t **.

它的参数是node_t **。

Your struct is

你的结构是

typedef struct node {
    int key; 
    struct node *left, *mid, *right;
} node_t;

So in your function :

所以在你的功能中:

void free_tree (node_t ** root)
{
    if(root != NULL)
    {
        free_tree((*root)->left);   <<< '(*root)->left' is of type 'node_t *'
        free_tree((*root)->mid);    <<< '(*root)->mid' is of type 'node_t *'
        free_tree((*root)->right);  <<< '(*root)->right' is of type 'node_t *'
        free(*root);
    }
    return;
}

You call you function giving a node_t * as argument whereas your function expects a node_t **

你调用函数给出一个node_t *作为参数,而你的函数需要一个node_t **

#1


1  

Your function expected a pointer to a pointer-to-node. You're giving it a pointer-to-node three times in your recursive calls. Further, you're not validating that the pointer-to-pointer, and the pointer it points to, are non-null; you're only validating the former.

您的函数需要一个指向节点指针的指针。你在递归调用中给它一个指向节点的指针三次。此外,您没有验证指向指针的指针及其指向的指针是否为空;你只是在验证前者。

In short, your function should look like this:

简而言之,您的功能应如下所示:

void free_tree (node_t ** root)
{
    if(root && *root)
    {
        free_tree(&(*root)->left);
        free_tree(&(*root)->mid);
        free_tree(&(*root)->right);
        free(*root);
        *root = NULL;
    }
}

The last functional line is optional, but frankly it's pointless to do this with pointers-to-pointers unless you're going to do that anyway, as it sets the caller's pointer to NULL after obliterating the tree. Given a properly built tree, your caller should deliver the address of the tree root when destroying the entire tree, as:

最后一个功能行是可选的,但坦率地说,使用指针指向它是没有意义的,除非你打算这样做,因为它在删除树之后将调用者的指针设置为NULL。给定一个正确构建的树,您的调用者应该在销毁整个树时提供树根的地址,如下所示:

node_t *root = NULL;

// ... build tree ...

free_tree(&root);

// root is now NULL; tree is destroyed

#2


1  

Your question cannot be answered very clearly but at least I can tell you why you have this warning about incompatible pointer type :

你的问题不能很清楚地回答,但至少我可以告诉你为什么你有关于不兼容指针类型的警告:

Your function prototype is

你的函数原型是

void free_tree (node_t ** root);

It's argument is a node_t **.

它的参数是node_t **。

Your struct is

你的结构是

typedef struct node {
    int key; 
    struct node *left, *mid, *right;
} node_t;

So in your function :

所以在你的功能中:

void free_tree (node_t ** root)
{
    if(root != NULL)
    {
        free_tree((*root)->left);   <<< '(*root)->left' is of type 'node_t *'
        free_tree((*root)->mid);    <<< '(*root)->mid' is of type 'node_t *'
        free_tree((*root)->right);  <<< '(*root)->right' is of type 'node_t *'
        free(*root);
    }
    return;
}

You call you function giving a node_t * as argument whereas your function expects a node_t **

你调用函数给出一个node_t *作为参数,而你的函数需要一个node_t **