Data structures 如何删除一棵树?每个节点中也有一个父指针

Data structures 如何删除一棵树?每个节点中也有一个父指针,data-structures,tree,n-ary-tree,Data Structures,Tree,N Ary Tree,节点的结构如下所示 struct node { int data; int noofchilds; node *child[n]; node *parent; }; 我希望使用递归和非递归方法。删除节点并递归删除其子节点 如果必须删除整个树(如您的问题所示),则父指针无关紧要(并且随着节点本身的删除而删除)。删除节点并递归删除其子节点 如果必须删除整个树(如您的问题所示),则父指针无关紧要(并且随着节点本身的删除而删除)。迭代算法: Start at the pare

节点的结构如下所示

struct node
{
   int data;
   int noofchilds;
   node *child[n];
   node *parent;
 };

我希望使用递归和非递归方法。

删除节点并递归删除其子节点


如果必须删除整个树(如您的问题所示),则父指针无关紧要(并且随着节点本身的删除而删除)。

删除节点并递归删除其子节点


如果必须删除整个树(如您的问题所示),则父指针无关紧要(并且随着节点本身的删除而删除)。

迭代算法:

Start at the parent node.

Then do the following actions as long as possible:
   if the current node has one or more children: 
      set one of the children nodes as the (next) current node
   else
      delete the current node and use its parent as the (next) current node.
      if the current node was the root node (which has no parent), stop.

迭代算法:

Start at the parent node.

Then do the following actions as long as possible:
   if the current node has one or more children: 
      set one of the children nodes as the (next) current node
   else
      delete the current node and use its parent as the (next) current node.
      if the current node was the root node (which has no parent), stop.
非递归版本:

struct node {
    struct node *parent;
    unsigned nchild;
    struct node *child[XXX];
    int data;
    };

void deltree(struct node *np)
{
struct node *par;

while (np) {
        /* if this node has any children, start by
        ** "descending" to the highest numbered child and kill that first.
        */
        if (np->nchild--) {
                np = np->child[np->nchild];
                continue;
                }
        /* when we arrive here, *np has no more children left,
        ** so kill it, and step up to its parent
        */
        par = node->parent;
        // if np->child was obtained via malloc() uncomment next line
        // free (np->child);
        free (np);
        np = par;
        }
return;
}
非递归版本:

struct node {
    struct node *parent;
    unsigned nchild;
    struct node *child[XXX];
    int data;
    };

void deltree(struct node *np)
{
struct node *par;

while (np) {
        /* if this node has any children, start by
        ** "descending" to the highest numbered child and kill that first.
        */
        if (np->nchild--) {
                np = np->child[np->nchild];
                continue;
                }
        /* when we arrive here, *np has no more children left,
        ** so kill it, and step up to its parent
        */
        par = node->parent;
        // if np->child was obtained via malloc() uncomment next line
        // free (np->child);
        free (np);
        np = par;
        }
return;
}

是的,但如果我想迭代进行,即不使用堆栈或队列,那么该方法是什么,我认为父指针可能会对我们有好处通常从根开始,因为这是唯一一个您也可以直接访问的节点。。。在这种情况下,家长是没有用的,因为你只需要去'向下'。。。我想知道迭代解决方案是否有助于解决这个问题;执行{if(node->noofchild){node=node->child[noofchild-1];else{node->parent->noofchild--;curr=node->parent;free(node);node=curr;}}while(node->parent | | |(node==root&&node))是的,但如果我想迭代进行,即不使用堆栈或队列,那么该方法是什么,我认为父指针可能对我们有好处通常从根开始,因为这是唯一一个节点,您也可以直接访问…在这种情况下,父指针没有用,因为您只需要“向下”…我很好奇ng如果迭代解决方案在这方面有帮助。node=root;do{if(node->noofchild){node=node->child[noofchild-1];else{node->parent->noofchild--;curr=node->parent;free(node);node=curr;}while(node->parent | | |(node==root&&node))如果当前节点是根节点,我们不能停止,因为只有对应于一个子节点的子树才会为真,但我认为下面类似于您的算法将是donode=root;do{if(node->noofchild){node=node->child[noofchild-1];else{node->parent->noofchild--;curr=node->parent;free(node);node=curr;}}}while(node->parent | | | |(node==root&&node))您是对的。我的算法只是一个快速草稿…您的算法似乎有一些问题,例如,如果树是空的。我将使用node=root;while(node){if(node->noofchild){node->noofchild--;node=node->child[node->noofchild];}其他{curr=node->parent;free(node);node=curr;}如果当前节点是根节点,我们就不能停止,因为只有对应于一个子节点的子树才是真的,但我认为下面类似于您的算法将是donode=root;do{if(node->noofchild){node=node->child[noofchild-1];else{node->parent->noofchild--;curr=node->parent;free(node);node=curr;}}而(node->parent | |(node==根和节点))你是对的。我的算法只是一个快速草稿…你的算法似乎有一些问题,例如,如果树是空的。我会使用node=root;而(node){if(node->noofchild){node->noofchild--;node=node->child[node->noofchild];}否则{curr=node->parent;free(node);node=curr;}我们可以在树中进行签入,开始时树是空的,然后返回树本身