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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/gao158190523/article/details/53471885
#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:


猜你喜欢

转载自blog.csdn.net/gao158190523/article/details/53471885
今日推荐