二叉树是一种特殊的树.docVIP

  • 30
  • 0
  • 约3.1千字
  • 约 9页
  • 2019-09-27 发布于江苏
  • 举报
二叉树是一种特殊的树,每个节点只能有最多二个孩子节点,或者没有孩子节点。建立,与撤销或遍历二叉树主要是靠递归的方法。 #includestdio.h #includestdlib.h typedef struct stud/*定义二叉树的结构,只有一个数据项,和两个孩子节点*/ { char data; struct stud *left; struct stud *right; } bitree; void destroy(bitree **root)/*撤销二叉树,这里用的是递归和二级指针*/ { if(*root!=NULL(*root)-left!=NULL)/*当前结点与左子树不空,递归撤销左子树*/ destroy((*root)-left); if(*root!=NULL(*root)-right!=NULL)/*当前结点与右子树不空,递归撤销右子树*/ destroy((*root)-right); free(*root);/*左右子树都为空,撤销该节点,并递归撤销其上的所有节点*/ } void inititate(bitree **root)/*初始化二叉树的头结点,并分配空间*/ { *root=(bitree *)malloc(sizeof(bitree )); (*root)-left=NULL; (*root)-right=NULL; } bitre

文档评论(0)

1亿VIP精品文档

相关文档