二叉树子系统

上传人:F****n 文档编号:98256178 上传时间:2019-09-09 格式:DOC 页数:5 大小:23KB
返回 下载 相关 举报
二叉树子系统_第1页
第1页 / 共5页
二叉树子系统_第2页
第2页 / 共5页
二叉树子系统_第3页
第3页 / 共5页
二叉树子系统_第4页
第4页 / 共5页
二叉树子系统_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
资源描述

《二叉树子系统》由会员分享,可在线阅读,更多相关《二叉树子系统(5页珍藏版)》请在金锄头文库上搜索。

1、#include#include #define TREEMAX 100typedef struct BT/ 定义二叉树结构体 char data; struct BT* lchild; struct BT* rchild;BT;BT *CreateTree();void ShowTree(BT *T);void Preorder(BT *T);void Postorder(BT *T);void Levelorder(BT *T);void Inorder(BT *T);void Leafnum(BT *T);void Nodenum(BT *T);int TreeDepth(BT *T);

2、int count=0;/ 定义计算结点个数的变量void main() BT *T=NULL; char ch1,ch2,a; ch1=y; while(ch1=y|ch1=Y) printf(n);printf(ntt 二叉树子系统);printf(ntt*);printf(ntt* 1-建 二 叉 树 *);printf(ntt* 2-凹 入 显 示 *);printf(ntt* 3-先 序 遍 历 *);printf(ntt* 4-中 序 遍 历 *);printf(ntt* 5-后 序 遍 历 *);printf(ntt* 6-层 次 遍 历 *);printf(ntt* 7-求 叶

3、 子 数 *);printf(ntt* 8-求 结 点 数 *);printf(ntt* 9-求 树 深 度 *);printf(ntt* 0-返 回 *);printf(ntt*);printf(ntt 请选择菜单号(0-9):);scanf(%c,&ch2);getchar();printf(n);switch(ch2) case 1:printf(ntt请按先序序列输入二叉树的结点:n);printf(ntt说明:输入结点(0表示后继结点为空)后按回车.n);printf(ntt请输入根结点:);T=CreateTree();printf(ntt二叉树成功建立!n);break; cas

4、e2:ShowTree(T);break; case3:printf(ntt该二叉树的先序遍历序列为:);Preorder(T);break; case4:printf(ntt该二叉树的中序遍历序列为:);Inorder(T);break; case5:printf(ntt该二叉树的后序遍历序列为:);Postorder(T);break; case6:printf(ntt该二叉树的层次遍历序列为:);Levelorder(T);break; case7:count=0;Leafnum(T);printf(ntt该二叉树有%d个叶子。n,count);break; case8:count=0;

5、Nodenum(T);printf(ntt该二叉树总共有%d个结点。n,count);break; case9:printf(ntt该树的深度是:%d,TreeDepth(T);break; case0:ch1=n;break; default:printf(ntt*请注意:输入有误!*);if(ch2!=0) printf(nntt按回车键继续,按任意键返回主菜单!n); a=getchar(); if(a!=xA) getchar();ch1=n; BT *CreateTree() / 建立二叉树 BT *t; char x; scanf(%c,&x); getchar(); if(x=0

6、)t=NULL; else t=(BT *)malloc(sizeof(BT);t-data=x;printf(ntt请输入%c结点的左子结点:,t-data);t-lchild=CreateTree();printf(ntt请输入%c结点的右子结点:,t-data);t-rchild=CreateTree(); return t;void Preorder(BT *T) / 先序遍历 if(T) printf(%3c,T-data);Preorder(T-lchild);Preorder(T-rchild); void Inorder(BT *T) / 中序遍历 if(T) Inorder(

7、T-lchild);printf(%3c,T-data);Inorder(T-rchild); void Postorder(BT *T) / 后序遍历 if(T) Postorder(T-lchild);Postorder(T-rchild);printf(%3c,T-data); void Levelorder(BT *T) / 层次遍历 int i,j; BT *q100,*p; p=T; if(p!=NULL) i=1;qi=p; j=2; while(i!=j) p=qi;printf(%3c,p-data);if(p-lchild!=NULL) qj=p-lchild;j+; if

8、(p-rchild!=NULL) qj=p-rchild; j+;i+; void Leafnum(BT *T) / 求叶子数 if(T) if(T-lchild=NULL&T-rchild=NULL) count+;Leafnum(T-lchild);Leafnum(T-rchild); void Nodenum(BT *T) / 求结点数 if(T) count+;Nodenum(T-lchild);Nodenum(T-rchild); int TreeDepth(BT *T) / 求树深度 int ldep,rdep; if(T=NULL)return 0; else ldep=Tree

9、Depth(T-lchild);rdep=TreeDepth(T-rchild);if(ldeprdep) return ldep+1;else return rdep+1; void ShowTree(BT *T) / 凹入法显示二叉树 BT *stackTREEMAX,*p; int levelTREEMAX2,top,n,i,width=4; if(T!=NULL) printf(ntt凹入表示法:ntt);top=1;stacktop=T;leveltop0=width;while(top0) p=stacktop; n=leveltop0; for(i=1;idata); for(i=n+1;irchild!=NULL) top+; stacktop=p-rchild; leveltop0=n+width; leveltop1=2; if(p-lchild!=NULL) top+; stacktop=p-lchild; leveltop0=n+width; leveltop1=1; 差距大,市场体系不完善,缺乏集聚效应等问题,同时充分考虑到该地周围已形成成熟建材商圈的商业价值,因地制宜的进行家居建材广场的建设。通过合理布局、优化环境、提升服务,该项目必将切实发挥商业区在引导消费、拉动经济增长方面的作用,促进该县经济和社会又好又快发展。

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > 教学/培训

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号