二叉树的前序、中序、后序遍历,非递归前、中、后序遍历

#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 BTree
typedef 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 stack
PSeqStack 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 Stack
void 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 stack
void 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 preOrder
void 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:


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值