二叉树的基本操作
来源:互联网 发布:网络推广部职能 编辑:程序博客网 时间:2024/05/17 03:20
Code:
#include<stdlib.h>#include<stdio.h>#include<malloc.h>//函数状态码定义#define TRUE 1#define FALSE 0#define OK 1#define ERROR 0#define OVERFLOW -1#define INFEASIBLE -2#define NULL 0typedef int Status;//二叉链表存储结构定义typedef int TElemType;typedef struct BiTNode{ TElemType data; struct BiTNode *lchild, *rchild; } BiTNode, *BiTree;//先序创建二叉树各结点Status CreateBiTree(BiTree &T){ TElemType e; scanf("%d",&e); if(e==0)T=NULL; else{ T=(BiTree)malloc(sizeof(BiTNode)); if(!T)exit(OVERFLOW); T->data=e; CreateBiTree(T->lchild); CreateBiTree(T->rchild); } return OK; }//求树的深度int GetDepthOfBiTree ( BiTree T){ if(T == NULL) return 0; else{ int l = GetDepthOfBiTree(T->lchild); int r = GetDepthOfBiTree(T->rchild); if(l < r) return r + 1; else return l + 1; }}//求树的叶子数int LeafCount(BiTree T){ int ans = 0; if(T == NULL) // 如果是一棵空树 return 0; if(T != NULL && (T->lchild == NULL && T->rchild == NULL)) //只有一个节点 return 1; else // { ans += LeafCount(T->lchild); //如果不是,分别递归求出来左右子树的叶子节点数目 ans += LeafCount(T->rchild); } return ans;}//下面是主函数int main(){ BiTree T; int depth, numberOfLeaves; CreateBiTree(T); depth= GetDepthOfBiTree(T); numberOfLeaves=LeafCount(T); printf("%d %d\n",depth,numberOfLeaves);}
阅读全文
0 0
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 火车车厢重新排列问题
- 计蒜客-计数问题
- JavaScript忍者秘籍笔记03之正则表达式
- 学习中记扎
- Redis数据结构
- 二叉树的基本操作
- Spring嵌套事物,事物的传播之REQUIRES_NEW
- java日常学习:直接排序与冒泡排序
- 结构体大小计算
- c++ 单模式
- Bootstrap- Modal对话框如何在关闭时触发事件
- 求出数组元素的最大值
- 新疆大学OJ(ACM) 1047: string 字符串排序
- 新疆大学OJ(ACM) 1099: 数列有序!