没有免费递归二叉树递归、二叉树

2023-09-11 22:43:51 作者:囚于心

。参见问题Deallocating二叉树结构用C

 结构节点{
    节点*父母;
    节点*下一个;
    节点*子女;
}
 

我试图把一个二叉树。我的问题是分配的对象是5520和调用free函数的数量是2747.我不知道为什么,它应该真正自由和遍历各地在树中的节点,这里是code我用

  INT number_of_iterations = 0;
INT number_of_deletions = 0;
    无效removetree(节点*节点)
    {
        number_of_iterations ++;
        而(节点!= NULL)
        {
            节点* TEMP =节点;

            如果(与于节点GT;!孩子= NULL)
            {
                节点=  - 于节点GT;儿童;
                &TEMP-GT;孩子=于节点>接着,
                于节点>接下来=温度;
            }
            其他
            {
                节点=于节点>接着,
                删除(临时);
                number_of_deletions ++
            }
        }
    }
 

迭代数是5440和缺失的数量是2747。

新的不动code:是code正确

 常量节点*下一个(常量节点*节点)
    {
        如果(节点== NULL)返回NULL;
        如果(与于节点GT;孩子)返回与于节点GT;儿童;

        而(节点&安培;&安培;于节点>接着== NULL){
            节点=  - 于节点GT;父母;
        }

        如果(节点)返回于节点>接着,
        返回NULL;
    }

 为(p值= ctx-> obj_root;磷; p值=下一个(p)的){
      自由(对);
     }
 
二叉树的非递归遍历

解决方案

如果您的节点确实包含有效的指针,那么整个事情可以在多少事更紧凑,更易读的方式

 无效removetree(节点*节点)
{
  而(节点!= NULL)
  {
    节点*接下来=&于节点GT;儿童;
     - 于节点GT;孩子= NULL;

    如果(下一个== NULL)
    {
      接下来=于节点>接着!= NULL?于节点>接下来: - 于节点GT;父母;
      删除(节点); //或`免费(节点)`
    }

    节点=下一个;
  }
}
 

refering to the question Deallocating binary-tree structure in C

struct Node{
    Node *parent;
    Node *next;
    Node *child;
}

I tried to free a binary tree. The problem I have is the allocated objects are 5520 and the number of calls to the free functions is 2747. I don't know why, it should really free and iterate all over the nodes in the tree, here is the code that I use

int number_of_iterations =0;
int number_of_deletions =0;
    void removetree(Node *node)
    {
        number_of_iterations++;
        while(node != NULL)
        {
            Node *temp = node;

            if(node->child != NULL)
            {
                node = node->child;
                temp->child = node->next;
                node->next = temp;
            }
            else
            {
                node = node->next;
                remove(temp);
                number_of_deletions++ 
            }
        }
    }

Number of iteration is 5440 and the number of deletions is 2747.

New Fixed code: is that code correct ?

 const Node *next(const Node *node)
    {
        if (node == NULL) return NULL;
        if (node->child) return node->child;

        while (node && node->next == NULL) {
            node = node->parent;
        }

        if (node) return node->next;
        return NULL;
    }

 for ( p= ctx->obj_root; p; p = next(p)) {
      free(p);
     }

解决方案

If your nodes do indeed contain valid parent pointers, then the whole thing can be done in a much more compact and readable fashion

void removetree(Node *node)
{
  while (node != NULL)
  {
    Node *next = node->child;
    node->child = NULL;

    if (next == NULL)
    {
      next = node->next != NULL ? node->next : node->parent;
      remove(node); // or `free(node)`
    }

    node = next;
  }
}