后序遍历二叉树的非递归算法

上传人:公**** 文档编号:469238211 上传时间:2023-02-18 格式:DOC 页数:17 大小:47KB
返回 下载 相关 举报
后序遍历二叉树的非递归算法_第1页
第1页 / 共17页
后序遍历二叉树的非递归算法_第2页
第2页 / 共17页
后序遍历二叉树的非递归算法_第3页
第3页 / 共17页
后序遍历二叉树的非递归算法_第4页
第4页 / 共17页
后序遍历二叉树的非递归算法_第5页
第5页 / 共17页
点击查看更多>>
资源描述

《后序遍历二叉树的非递归算法》由会员分享,可在线阅读,更多相关《后序遍历二叉树的非递归算法(17页珍藏版)》请在金锄头文库上搜索。

1、1 请根据顾客输入的“扩展的先序遍历序列” (用小圆点表达空子树),建立以二叉链表方式存储的二叉树,然后写出后序遍历该二叉树的非递归算法。措施一:#nude tdib.#inclde #deine MAX_REE_SIZE00typedef trut oechaata;suc BiTNd* lchid;truct BNode *rcld;iNode,*iTre;/函数声明odPrnt(BiTr*root);void Slectin(intsl,iTe *root);void RExPrer(BTre *rot);vid PrintExrOr(Te root);oid PordeTrvere(B

2、iTreeT);/主函数vod a()BiTreeroo=U;/初始化根结点 Print(&oo);wile (te)printf(nPress enter to continue.);getha();etchar();system(cls);Print(rot);void Print(Bre oot) 提示int sel;printf(使用阐明:本程序可实现二叉链表方式存储的二叉树,输入为扩展先序遍历序列.n);printf(-n);pri(1.输入扩展先序遍历序列并建立相应的二叉树.n);prinf(.打印目前的二叉树的扩展先序遍历序列.);rinf(3.后序遍历目前的二叉树并打印遍历序列

3、.n);pritf(4按其他任意键退出n);print(-n);pintf(请选择你要的操作:);sn(, &se);gchar();Select(sl,rot);vo ectin(in sel, BiTre *rot) /根据顾客输入决定下一环节switch (sel) ase 1: ReadxPrOe(ro); reak;ca : PrinxPrrde(*oot); eak;se 3:PstOrdeaverse(*roo); reak;defal: exi(0);vd ReadExeOer(BTree root) /先序遍历二叉树,r为指向二叉树(或某一子树)根结点的指针hr ch;c=

4、gechr();i (h = )*ro NULL;else(*root) = (BTree)aloc(seo(iTd);(*root)-data = ch;ReadExPreOrder(&(rot)-lchid);RadEreOrdr((*ot)-rhld);od PrintExPreOrdr(Biree oo)char c;if (roo!=NU)ch= oo-a;pintf(c, ch);PrntExPreOrder(rot-lcld);PritEeOrder(rot-child);eeprintf(.);iPostOderverse(BiTree ) BiTre sackMX_TRESI

5、Z, p;it tagMAX_RE_IZE,tp=0;p= T;wile(p | top != 0)hl(p) tp+;stacktop=p;tagtop;p=plchil;/从根开始,左结点依次入栈if(p0) if(tagop=1)/表达是从该结点的右子树返回,则访问该结/点p=ackto;rint(c, pa);to-;p=NL;将p指向NULL,则下次进入whe循环时,不做左子/树入栈操作ee/表白是从该结点左子树返回,应继续访问其右子树=sttop;f(top ) ppchil;tagtop=1; /表白该结点的右子树已访问 /end felse/endofif/enf he措施二(

6、顺序栈):#includeing nmace td;tpedef sruct nodecharh;tuctnde *hil;stctne cild;noe;ypedef trtbinarytreeode *ead;/指向根结点bt;in n=0;voi ini(t * b)bnew bt;b-ead=NUL;od pecreate(node * &t)/先序递归建立二叉树coutc;if(c=.) t=NULL;ese+;=new node;tch=c;precree(tchd);precreae(t-rcd);/-/非递归算法需要栈def stut sak noe *bas;node *op

7、;int tacks;stack;d ni(stck&s) sbae=new node *1 ;s.top=ase;.stacksiz=100;void psh(ak &s,nod *elem) (s.top+)=lem;void po(stac &s,nod * &eem)le= (-sto);it sempt(stak &s) f(s.bse=s.to)etun 1;ese rturn0;vo ettop(ck &s,node*&t) t=*(.top-);vo afttaerse(d *t) /后序遍历非递归算法stck s;in(s);node lastvist= NUL;while

8、(NULL! | !sempy(s) wil(ULL != t) push(s,t); t = t-lhild; geop(s,t);i (ULL =t-rcild|lastit = t-rchild) cut hed);/构造二叉树coutnn后序遍历: ;afttrvers(b-ha);couticlutemch; B-lchd=reatTre(); B-child=reateTr(); eturn B; vi Postrder(Tree T)Tee p,q; st s,top; nt p; pT; op=NULL; dwhile(!=NL)s(st)malloc(sie(Stk));使用链表形式存储栈,即/链栈 s-=; snext=tp; ops; p=hild; op=1;/该标志用于控制进入下面的while循环 q=NLL; whl(top!=NUL&o=)if(topm-rchild=q)/若目前栈顶结点的右孩子是上次访问的结点,则打印该结点 prntf(%c,top-mitem);/第一次时打印最左下边结点 q=top-m; /q记录上次访问的结点 to=o-ext; 栈指针后移,即将该结点从栈中弹出

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 解决方案

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