【问题标题】:Freeing an allocated binary tree - C programming释放分配的二叉树 - C 编程
【发布时间】:2013-10-24 19:37:25
【问题描述】:
struct node {
    int data;
    struct node* left;
    struct node* right;
};

创建要插入的节点函数:

struct node* create_node(int val) {

 // Allocate space for the node, set the fields.
 struct node* temp;
 temp = (struct node*)malloc(sizeof(struct node));
 temp->data = val;
 temp->left = NULL;
 temp->right = NULL;

 return temp; // Return a pointer to the created node.
}

我的插入节点功能:

struct node* insert(struct node *root, struct node *element) {

 if (root == NULL)
   return element;
 else {

   // element should be inserted to the right.
   if (element->data > root->data) {

     // There is a right subtree to insert the node.
     if (root->right != NULL)
       root->right = insert(root->right, element);

     // Place the node directly to the right of root.
     else
       root->right = element;
   }

   // element should be inserted to the left.
   else {

     // There is a left subtree to insert the node.
     if (root->left != NULL)
       root->left = insert(root->left, element);

     // Place the node directly to the left of root.
     else
       root->left = element;
   }

   // Return the root pointer of the updated tree.
   return root;
 }
}

我将节点插入树的主要位置:

    scanf("%d", &numCases);

    for(i=0; i<numCases;i++){

        scanf("%d", &numNodes);

        for(j=0; j < numNodes; j++){
            scanf("%d", &val);
            temp_node = create_node(val);
            my_root = insert(my_root, temp_node);
        }
// calling the function to free the tree after all nodes have been inserted
postOrderFree(my_root);

现在我的计划是使用 Post order traverse 方法来释放每个节点,但是当我尝试使用我的 Post order 功能时,它似乎无法正常工作。它根本不会释放任何节点,对于我给它的每一种情况,它都会继续向前一个树添加节点,直到它不可避免地崩溃。

这是我正在尝试使用的 Post order traverse 函数:

void postOrderFree(struct node* root){
    if(root != NULL) {
        postOrderFree(root->left);
        postOrderFree(root->right);
        free(root->data);
    }
}

我们将不胜感激任何和所有帮助,包括样式以及是否有任何冗余!

【问题讨论】:

    标签: c malloc binary-tree


    【解决方案1】:

    您的 postOrderFree() 函数释放了错误的东西..

    应该是

     free(root);
    

    而不是

      free(root->data);
    

    释放二叉树后,您还必须将根节点设置回 NULL,否则它将是一个悬空指针。 那就是你必须这样做:

     postOrderFree(my_root);
     my_root = NULL;
    

    【讨论】:

    • void postOrderFree(struct node* root){ if(root != NULL) { postOrderFree(root->left); postOrderFree(root->right);免费(根); postOrderFree(根);根=空; } } 它是否正确?这对我来说是崩溃的。
    • 我将 main 中的 my_root 设置为 NULL 并且它运行正常。非常感谢。
    【解决方案2】:

    由于您为节点分配了内存,而不是数据,因此您不应该释放节点数据,而是释放节点本身:

    void postOrderFree(struct node* root){
        if(root != NULL) {
            postOrderFree( root->left );
            postOrderFree( root->right );
            free( root );
        }
    }
    

    【讨论】:

    • 我也试过了,它会在第一次尝试释放 root 时使程序崩溃。
    • free( root ); 行之后,需要再添加一行。 root = NULL; .
    猜你喜欢
    • 2012-02-29
    • 1970-01-01
    • 2015-12-20
    • 1970-01-01
    • 1970-01-01
    • 2021-12-04
    • 2015-06-16
    • 1970-01-01
    • 2011-03-29
    相关资源
    最近更新 更多