二叉树的前序、中序、后序遍历,非递归前、中、后序遍历
来源:互联网 发布:深圳淘宝网店培训 编辑:程序博客网 时间:2024/06/05 20:31
#include<iostream>using namespace std;//the definition of struct of binary tree;typedef char DataType;typedef struct bnode{DataType data;struct bnode *lchild, *rchild;}Bnode,*BTree;//the definition of struct of Stack for BTreetypedef struct seqStack{BTree* seq;int index;int length;}*PSeqStack,SeqStack;//create a new BTree by user's input//input eg: AB#D##CE##F##//Illustrate: create a new Binary Tree follow preorder search// '#' represent NULL , it means that its parent doesnot have rchild or lchild.BTree CreateBinTree(){BTree t;char ch; ch=getchar();if(ch=='#') t=NULL;else{t=(Bnode *)malloc(sizeof(Bnode));t->data=ch;t->lchild=CreateBinTree();t->rchild=CreateBinTree();}return t;}//this is a init algorithm of stackPSeqStack Init_SeqStack(int length){PSeqStack Pseqstack=(PSeqStack)malloc(sizeof(SeqStack));Pseqstack->index=-1;Pseqstack->length=length;if(!(Pseqstack->seq=(BTree*)malloc(length*sizeof(BTree)))){cout<<"Init Stack failed"<<endl;exit(0);}return Pseqstack;}//this is a function which push BTree into the Stackvoid Push_SeqStack(PSeqStack s,BTree p){if(s->index+1==s->length){cout<<"Push stack failed, becasuse of a lack of memory"<<endl;exit(1);}(s->index)++;*(s->seq+s->index)=p;}//this is a function which check the Stack whether is empty.int Empty_SeqStack(PSeqStack p){if(p->index==-1){return 1;}else{return 0;}}//this is a function which get the data of the top stackvoid Pop_SeqStack(PSeqStack s,BTree * p){if(s->index==-1){cout<<"error, This stack is empty!"<<endl;exit(0);}*p=*(s->seq+s->index);(s->index)--;}//search for preOrdervoid preOrder(BTree t){if(t){//preOrder(t->lchild);cout<<t->data;preOrder(t->lchild);preOrder(t->rchild);}else{//cout<<"#";}}void PreOrder_Stack(BTree t){PSeqStack s;BTree p=t;s=Init_SeqStack(50);cout<<"PreOrder_Stack:";while(p||!Empty_SeqStack(s)){if(p){cout<<p->data;Push_SeqStack(s,p); p=p->lchild;}else{Pop_SeqStack(s,&p);p=p->rchild;}}cout<<endl;}void InOrder(BTree t){if(t){InOrder(t->lchild);cout<<t->data;InOrder(t->rchild);}}void InOrder_Stack(BTree t){PSeqStack s;BTree p=t;s=Init_SeqStack(50);cout<<"InOrder_Stack:";while(p||!Empty_SeqStack(s)){if(p){Push_SeqStack(s,p); p=p->lchild;}else{Pop_SeqStack(s,&p);cout<<p->data;p=p->rchild;}}cout<<endl;}void PostOrder(BTree t){if(t){PostOrder(t->lchild);PostOrder(t->rchild);cout<<t->data;}}void PostOrder_Stack(BTree t){PSeqStack s1;int flag[50],index=-1;BTree p=t;s1=Init_SeqStack(50);cout<<"PostOrder_Stack:";while(p||!Empty_SeqStack(s1)){if(p){Push_SeqStack(s1,p);flag[++index]=2;p=p->lchild;}else{Pop_SeqStack(s1,&p);if(flag[index]==2){Push_SeqStack(s1,p);p=p->rchild;flag[index]=1;}else{cout<<p->data;--index;p=NULL;}}}cout<<endl;}int main(){BTree bt;bt=CreateBinTree();cout<<"PreOrder:";preOrder(bt);cout<<endl;PreOrder_Stack(bt);cout<<"InOrder:";InOrder(bt);cout<<endl;InOrder_Stack(bt);cout<<"PostOrder:";PostOrder(bt);cout<<endl;PostOrder_Stack(bt);char input;cin>>input;return 1;}
Result:
1 0
- 二叉树的非递归【前/中/后 序遍历】
- 二叉树非递归前、中、后序遍历
- 二叉树非递归前、中、后序遍历实现
- 非递归前,中,后序遍历二叉树
- 非递归实现二叉树的后序遍历、前序遍历、中序遍历
- 【二叉树】 前序、中序和后序的递归遍历与非递归遍历
- 二叉树的前序、中序、后序遍历,非递归前、中、后序遍历
- C# 二叉树的非递归中序 前序 后序遍历 层序遍历
- 二叉树的非递归遍历以及层次遍历(前序、中序、后序)
- 二叉树的前序,中序,后序的递归与非递归遍历
- 二叉树的前序遍历、中序遍历和后序遍历的递归和非递归算法
- 【二叉树】实现二叉树的前序、中序、后序的非递归遍历
- 前、中、后、序递归遍历二叉树以及非递归遍历
- 前,中,后序遍历二叉树 (递归 && 非递归的栈 && 非递归非栈的线索二叉树)
- 二叉树的创建,遍历(前序,中序,后序)-递归 非递归
- 二叉树的前序、中序、后序遍历 递归非递归实现
- 二叉树的前序、中序、后序(递归、非递归)遍历java实现
- 数据结构:二叉树的前序,中序,后序遍历(递归和非递归)
- struts.xml配置顺序
- 实时流计算Spark Streaming原理介绍
- linux python2.6升级到2.7
- HTML5中ul,ol,div标签及其使用语法
- 深度学习(caffe+VS2013+WIN10)使用GPU编译——调用python接口并且成功运行mnist
- 二叉树的前序、中序、后序遍历,非递归前、中、后序遍历
- 【py交易】算法竞赛入门经典6.3.1 小球下落 python
- mybatis基本配置之properties
- 替换空格
- 18.利用助手生成表单
- 批处理的注释
- Java 根据环境属性区分不同操作系统
- 输出9*9乘法表,输出以直角三角形的形状输出
- HDOJ 1042 N! 大数