首页 前序建立二叉树 先中后遍历 叶子数 树深度

前序建立二叉树 先中后遍历 叶子数 树深度

举报
开通vip

前序建立二叉树 先中后遍历 叶子数 树深度前序建立二叉树 先中后遍历 叶子数 树深度 #include #include #include #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE -1 #define OVERFLOW -2 typedef int Status; typedef char elemtype; typedef struct BiTNode{ elemtype data; struct BiTNod...

前序建立二叉树 先中后遍历 叶子数 树深度
前序建立二叉树 先中后遍历 叶子数 树深度 #include #include #include #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE -1 #define OVERFLOW -2 typedef int Status; typedef char elemtype; typedef struct BiTNode{ elemtype data; struct BiTNode *lchild,*rchild; }BiTNode,*BiTree; //构造二叉树 Status CreateBiTree(BiTree &T){ elemtype ch; ch=getchar(); if(ch==' '){T=NULL;} else{ if(!(T=(BiTNode *)malloc(sizeof(BiTNode)))) return FALSE; T->data=ch; CreateBiTree(T->lchild); CreateBiTree(T->rchild); } return OK; } // 先序遍历 void PreOrderTraverse(BiTree T){ if(T!=NULL){ printf("%c ",T->data); PreOrderTraverse(T->lchild); PreOrderTraverse(T->rchild); } } //叶子节点的个数 Status Leafnumber(BiTree T){ int num1=0,num2=0; if(T==NULL) return 0; else if (T->lchild==NULL&&T->rchild==NULL) return 1; else { num1=Leafnumber(T->lchild); num2=Leafnumber(T->rchild); return(num1+num2); } } //树的深度 Status DepthTree(BiTree T){ int llength=0,rlength=0; if(T==NULL) return 0; else{ llength=DepthTree(T->lchild); rlength=DepthTree(T->rchild); return(llength>rlength)?(llength+1):(rlength+1); } } void main() { BiTree s; printf("输入字符串,使用空格代 关于同志近三年现实表现材料材料类招标技术评分表图表与交易pdf视力表打印pdf用图表说话 pdf 空\n"); CreateBiTree(s); printf("先序输出:\n"); PreOrderTraverse(s); printf("\n树的深度:%d\n",DepthTree(s)); getch(); } //增加个变量i,修改一下PreOrderTraverse #include #include #include #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE -1 #define OVERFLOW -2 typedef int Status; typedef char elemtype; typedef struct BiTNode{ elemtype data; struct BiTNode *lchild,*rchild; }BiTNode,*BiTree; //构造二叉树 Status CreateBiTree(BiTree &T){ elemtype ch; ch=getchar(); if(ch==' '){T=NULL;} else{ if(!(T=(BiTNode *)malloc(sizeof(BiTNode)))) return FALSE; T->data=ch; CreateBiTree(T->lchild); CreateBiTree(T->rchild); } return OK; } // 先序遍历 void PreOrderTraverse(BiTree T,int i) { if(T!=NULL) { printf("%c\t%d\n",T->data,i); PreOrderTraverse(T->lchild,i+1); PreOrderTraverse(T->rchild,i+1); } } //叶子节点的个数 Status Leafnumber(BiTree T){ int num1=0,num2=0; if(T==NULL) return 0; else if (T->lchild==NULL&&T->rchild==NULL) return 1; else { num1=Leafnumber(T->lchild); num2=Leafnumber(T->rchild); return(num1+num2); } } //树的深度 Status DepthTree(BiTree T) { int llength=0,rlength=0; if(T==NULL) return 0; else { llength=DepthTree(T->lchild); rlength=DepthTree(T->rchild); return(llength>rlength)?(llength+1):(rlength+1); } } void main() { BiTree s; int i=1; printf("输入字符串,使用空格代表空\n"); CreateBiTree(s); printf("先序输出:\n值\t层次\n"); PreOrderTraverse(s,i); printf("\n树的深度:%d\n",DepthTree(s)); getch(); }
本文档为【前序建立二叉树 先中后遍历 叶子数 树深度】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_321635
暂无简介~
格式:doc
大小:19KB
软件:Word
页数:6
分类:互联网
上传时间:2017-09-27
浏览量:52