二叉树及其应用--二叉树特征值与销毁
来源:互联网 发布:wemall java版 编辑:程序博客网 时间:2024/06/05 10:18
给定二叉树的数据类型如下
typedef char Element;struct Node{ Element data; struct Node *lchild; struct Node *rchild;};typedef struct Node BTNode;typedef struct Node * BTree;
①二叉树输出
完成void PrintBTree(BTree root)函数,该函数输出二叉树的广义表表示。
void PrintBTree(BTree root){ if(root==NULL)return; printf("%c",root‐>data); if(root‐>lchild==NULL&&root‐>rchild==NULL) return; printf("("); if(root‐>lchild!=NULL) PrintBTree(root‐>lchild); printf(","); if(root‐>rchild!=NULL) PrintBTree(root‐>rchild); printf(")");}
②二叉树输出叶节点
完成void PrintLeaf(BTree root)函数,该函数按先序遍历的方式输出二叉树所有的叶节点。
void PrintLeaf(BTree root){ if(root==NULL)return; if(root‐>lchild==NULL&&root‐>rchild==NULL) printf("%c\n",root‐>data); PrintLeaf(root‐>lchild); PrintLeaf(root‐>rchild);}
③二叉树叶节点数
完成int GetLeafNum(BTree root);函数,该函数统计二叉树root中叶节点数目并返回该值。
int GetLeafNum(BTree root){ if(root==NULL) return 0; if(root‐>lchild==NULL&&root‐>rchild==NULL) return 1; return GetLeafNum(root‐>lchild)+GetLeafNum(root‐>rchild);}
④二叉树树深
完成int GetDepth(BTree root)函数,该函数返回二叉树root的树深。
int GetDepth(BTree root){ int ldepth,rdepth; if(root==NULL) return 0; rdepth=GetDepth(root‐>rchild); ldepth=GetDepth(root‐>lchild); return (rdepth>ldepth?rdepth:ldepth)+1;}
⑤二叉树节点数
完成int GetNum(BTree root)函数,该函数统计二叉树root中节点数目并返回该值。
int GetNum(BTree root){ int num; if(root==NULL) return 0; num=1; if(root‐>lchild!=NULL) num+=GetNum(root‐>lchild); if(root‐>rchild!=NULL) num+=GetNum(root‐>rchild); return num;}
⑥二叉树销毁
完成BTree Dispose(BTree root)函数,该函数销毁二叉树并返回NUL指针。
BTree Dispose(BTree root){ if(root==NULL) return NULL; Dispose(root‐>lchild); Dispose(root‐>rchild); free(root); return NULL;}
0 0
- 二叉树及其应用--二叉树特征值与销毁
- 二叉树及其应用
- 二叉树及其应用
- 二叉树及其应用--二叉树遍历
- 二叉树及其应用--二叉树创建
- 二叉树的创建与销毁
- 二叉树及其应用--树
- 二叉树接口及其应用
- 遍历二叉树及其应用
- 遍历二叉树及其应用
- 二叉树及其应用--二叉树的应用
- 平衡二叉树及其应用场景
- 哈夫曼树及其应用-最优二叉树
- 平衡二叉树及其应用场景
- 二叉树的创建及其简单应用
- 平衡二叉树及其应用场景
- 二叉树的遍历及其应用
- C++二叉树及其算法和应用
- 今天给大家讲个故事
- 近期学习内容顺序
- 支付宝支付
- Mac系统下,Hadoop 2.6.2 + Mahout 0.12.1 完全分布式配置
- webview自适应高度
- 二叉树及其应用--二叉树特征值与销毁
- junit
- Android开发之如何保证Service不被杀掉
- mysql的约束
- 线性表-多项式加法(数据结构基础 第2周)
- Activity生命周期详解
- TCP/IP、Http/Soap协议-基本认识
- 银联支付
- The Key Sations_toj2189_割点