二叉树中序遍历的非递归算法实现

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

试验五课程名称实验室名称实验名称二叉树中序遍历的非递归算法实现指导教师成绩1、实验目的二叉树中序遍历的非递归算法实现2、实验原理和内容二叉树中序遍历的非递归算法实现3、实验步骤1.链式存储结构的定义和栈结构的定义2.编写进栈函数push和出栈函数pop实现中序遍历过程中需存储的数的进栈和出栈过程3.创建一棵二叉树4.对该二叉树进行中序遍历,采用非递归算法实现4、程序及运行结果(或实验数据记录及分析)#includestdio.h#includestdlib.htypedefchardatatype;//*链式存储结构*//typedefstructnode{datatypedata;structnode*lchild,*rchild;}bintnode;typedefbintnode*bintree;typedefstructstack{/*栈结构定义*/bintreedata[100];inttop;}seqstack;voidpush(seqstack*s,bintreet){s-data[s-top]=t;s-top++;}bintreepop(seqstack*s){if(s-top!=0){s-top--;return(s-data[s-top]);}elsereturnNULL;}voidcreatebintree(bintree*t){charch;if((ch=getchar())=='')*t=NULL;else{*t=(bintnode*)malloc(sizeof(bintnode));(*t)-data=ch;createbintree(&(*t)-lchild);createbintree(&(*t)-rchild);}}voidinorder1(bintreet){seqstacks;s.top=0;while((t!=NULL)||(s.top!=0)){while(t){push(&s,t);t=t-lchild;}if(s.top!=0){t=pop(&s);printf(%c,t-data);t=t-rchild;}}}main(){bintreeroot;printf(inputthetreeaspreorder:);createbintree(&root);printf(\n中序遍历结果是:);inorder1(root);}在屏幕上输出的结果:

1 / 5
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功