6.45.c

来自「部分高校使用anyview编程测试数据结构习题,此代码为数据结构题集(c语言版)」· C语言 代码 · 共 40 行

C
40
字号
6.45④ 编写递归算法:对于二叉树中每一个元素值为x
的结点,删去以它为根的子树,并释放相应的空间。

要求实现下列函数:
void ReleaseX(BiTree &bt, char x);
/* Delete all subtrees whose root is  */
/* the node which element value is x, */
/* then release correspondant space.  */

二叉链表类型定义:
typedef struct BiTNode {
    TElemType data;
    BiTNode  *lchild, *rchild;
} BiTNode, *BiTree;
void Delete(BiTree T);
void ReleaseX(BiTree &bt, char x)
/* Delete all subtrees whose root is  */
/* the node which element value is x, */
/* then release correspondant space.  */
{ BiTree p;
  p=bt;
  if(bt->data==x)
  {
   bt=NULL;
   Delete(p);   
   }
  else
     {
      if(bt->lchild) ReleaseX(bt->lchild,x);
      if(bt->rchild) ReleaseX(bt->rchild,x);
       }
}
void Delete(BiTree T)
{
  if(T->lchild) Delete(T->lchild);
  if(T->rchild) Delete(T->rchild);
  free(T);
  }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?