usingnamespacestd;#defineNULL0#defineOK1#defineOVE"> usingnamespacestd;#defineNULL0#defineOK1#defineOVE" />
二叉树遍历源代码(通过vc6.0编译).doc

二叉树遍历源代码(通过vc6.0编译).doc

ID:55557967

大小:16.00 KB

页数:4页

时间:2020-05-17

二叉树遍历源代码(通过vc6.0编译).doc_第1页
二叉树遍历源代码(通过vc6.0编译).doc_第2页
二叉树遍历源代码(通过vc6.0编译).doc_第3页
二叉树遍历源代码(通过vc6.0编译).doc_第4页
资源描述:

《二叉树遍历源代码(通过vc6.0编译).doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库

1、#include"iostream.h"#include"stdlib.h"#include"stdio.h"#includeusingnamespacestd;#defineNULL0#defineOK1#defineOVERFLOW-1typedefintStatus;typedefstructnode{chardata;structnode*lchild;structnode*rchild;}*bitree;intk=0;intdepth(bitreeT)//树的高度{if(!T)return0;else{intm=depth(T->lch

2、ild);intn=depth(T->rchild);return(m>n?m:n)+1;}}//先序,中序建树structnode*create(char*pre,char*ord,intn){structnode*T;intm;T=NULL;if(n<=0){returnNULL;}else{m=0;T=new(structnode);T->data=*pre;T->lchild=T->rchild=NULL;while(ord[m]!=*pre)m++;T->lchild=create(pre+1,ord,m);T->rchild=create(pre+

3、m+1,ord+m+1,n-m-1);returnT;}}//中序递归遍历voidinorder(structnode*T){if(!T)return;else{inorder(T->lchild);cout<data;inorder(T->rchild);}}voidinpre(structnode*T){if(!T)return;else{cout<data;inpre(T->lchild);inpre(T->rchild);}}voidpostorder(structnode*T){if(!T)return;else{postorder(

4、T->lchild);postorder(T->rchild);cout<data;}}//先序非递归遍历voidinpre1(structnode*T){structnode*p;structnode*stack[20];inttop=0;p=T;cout<<"非递归先序为:"<

5、

6、top!=0){while(p){stack[top++]=p;cout<data;p=p->lchild;}p=stack[--top];p=p->rchild;}}//中序非递归遍历voidinorder1(structnode*T)

7、{structnode*p;structnode*stack[20];inttop=0;p=T;cout<<"非递归中序为:"<

8、

9、top!=0){while(p){stack[top++]=p;p=p->lchild;}p=stack[--top];cout<data;p=p->rchild;}}//主函数intmain(){bitreeT;charpre[30],ord[30];intn,m;cout<<"请输入先序为-+a*b%cd/ef的二叉树:"<

10、d-e/f的二叉树:"<

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。