二叉树非递归遍历

#include<iostream>
#include<stack>
#include<queue>
using namespace std;

typedef struct bintree
{
	char data;
	struct bintree *leftchild;
	struct bintree *rightchild;
}BinTree;

BinTree* Node(char data,BinTree *leftchild,BinTree *rightchild);
BinTree* build_tree(void);
int preorder_traverse(BinTree * const root);
int inorder_traverse(BinTree * const root);
int postorder_traverse(BinTree * const root);
int levelorder_traverse(BinTree * const root);


int main()
{   
	BinTree *root=NULL;
	if(!(root=build_tree()))
	{
		cerr<<"build tree failed!"<<endl;
		return -1;
	}
	preorder_traverse(root);
	inorder_traverse(root);
	postorder_traverse(root);
	levelorder_traverse(root);
	return 0;
}

int preorder_traverse(BinTree * const root)
{
	if(NULL==root)
	{
		cerr<<"tree is not exit"<<endl;
		return -1;
	}
	stack<BinTree*> s;
    BinTree *p=root;

/*naive thought
action1:
	while(p)
	{
		cout<<p->data<<endl;
	    s.push(p);
		p=p->leftchild;
	}
action2:
	if(s.empty())
		return 0;
	p=s.top();
	s.pop();

	if(p->rightchild!=NULL)
	{
	    p=p->rightchild;
		goto action1;
	}
	else
    {
	   goto action2;
	}
	然后写得漂亮一点
*/
	while(p  || !s.empty())
	{
		if(p)
		{
			cout<<p->data<<endl;
			s.push(p);
			p=p->leftchild;
		}
		else
		{
			p=s.top();
			s.pop();
			p=p->rightchild;
		}
	}
	return 0;
}

int inorder_traverse(BinTree * const root)
{
	if(NULL==root)
	{
		cerr<<"tree is not exit"<<endl;
		return -1;
	}
	stack<BinTree*> s;
    BinTree *p=root;

	while(p  || !s.empty())
	{
		if(p)
		{
			s.push(p);
			p=p->leftchild;
		}
		else
		{
			p=s.top();
			s.pop();
			cout<<p->data<<endl;
			p=p->rightchild;
		}
	}
	return 0;
}

int postorder_traverse(BinTree * const root)
{
	if(NULL==root)
	{
		cerr<<"tree is not exit"<<endl;
		return -1;
	}
	stack<BinTree*> s;
    BinTree *p=root;
	BinTree *flag=NULL;

	while(p  || !s.empty())
	{
		if(p)
		{
			s.push(p);
			p=p->leftchild;
			
		}
		else
		{
			if(s.top()->rightchild != flag)
			{
				p=s.top()->rightchild;
				flag=NULL;
			}
			else
			{
				flag=s.top();
				s.pop();
				cout<<flag->data<<endl;
			}
		}
	}
	return 0;
}

int levelorder_traverse(BinTree * const root)
{
	if(!root) { return -1;}
	queue<BinTree *> q;
	BinTree *p=root;
	q.push(p);
	while(!q.empty())
	{
		p=q.front();
		q.pop();
		cout<<p->data<<endl;
		if(p->leftchild) { q.push(p->leftchild);}
		if(p->rightchild) { q.push(p->rightchild);}
	}
	return 0;
}

BinTree* build_tree(void)
{
	BinTree *d=Node('D',0,0);
	BinTree *g=Node('G',0,0);
	BinTree *h=Node('H',0,0);
	BinTree *i=Node('I',0,0);
	BinTree *b=Node('B',d,0);
	BinTree *e=Node('E',g,h);
	BinTree *f=Node('F',0,i);
	BinTree *c=Node('C',e,f);
	BinTree *a=Node('A',b,c);
	return a;
}

BinTree* Node(char data,BinTree *leftchild,BinTree *rightchild)
{
	BinTree *p=new BinTree;
	p->data=data;
	p->leftchild=leftchild;
	p->rightchild=rightchild;
	return p;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值