二叉树非递归遍历

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<iostream>
#include<string>
#include<stack>
#include<queue>
#include<algorithm>
using namespace std;
struct node{
	int data;
	struct node *lc,*rc;
};
struct Node{
	bool first;
	node *a;
};
class bintree{
	private:
		node *root;
		int num;
		node* create(node *root,int &num);
		void preorder(node *root);
    	void inorder(node *root);
    	void postorder(node *root);
    	void road_(node *root);
	public:
		bintree(){root=NULL; num=0;}
		~bintree();
		void bincreate();
		void pre_order();
		void in_order();
		void post_order();
		void road();
		int nums();
};
bintree::~bintree()
{
  stack<node *> s;
  while(root!=NULL || !s.empty())
  {
    if(root!=NULL)
	{
		s.push(root);
		root=root->lc;
	}
	else if(!s.empty())
	{
		node *tep=s.top();
		s.pop();
		root=tep->rc;
		free(tep);
	}
  }
}
node *bintree::create(node *root,int &num)
{
	int n;
	scanf("%d",&n);
	if(n==0)
		return NULL;
	else
	{
		root=new node;
		root->data=n;
		num++;
		root->lc=create(root->lc,num);
		root->rc=create(root->rc,num);
		return root;
	}
}
void bintree::bincreate()
{
	root=create(root,num);
}
void bintree::preorder(node *root)
{
    stack<node *> s;
    while(root!=NULL || !s.empty())
    {
    	if(root)
    	{
    		cout<<root->data<<" ";
    		s.push(root);
    		root=root->lc;
		}
		else
		{
			node *tep=s.top();
			s.pop();
			root=tep->rc;
		}
	}
}
void bintree::pre_order()
{
	cout<<"preorder\n";
    preorder(root);
    cout<<"end\n\n";
}
void bintree::inorder(node *root)
{
    stack<node *> s;
    while(root!=NULL || !s.empty())
    {
    	if(root)
    	{
    		s.push(root);
    		root=root->lc;
		}
		else
		{
			node *tep=s.top();
			s.pop();
			cout<<tep->data<<" ";
			root=tep->rc;
		}
	}
}
void bintree::in_order()
{
	cout<<"inorder\n";
    inorder(root);
    cout<<"end\n\n";
}
void bintree::postorder(node *root)
{
    Node tep;
    stack<Node> s;
    while(root!=NULL || !s.empty())
    {
    	if(root!=NULL)
    	{
    		while(root!=NULL)
    		{
    			tep.first=true;
    			tep.a=root;
    			s.push(tep);
    			root=root->lc;
			}
		}
		else if(!s.empty())
		{
			tep=s.top();
			s.pop();
			if(tep.first==true)
			{
				tep.first=false;
				s.push(tep);
				root=tep.a->rc;
			}
			else
			{
				cout<<tep.a->data<<" ";
				root=NULL;
			}
		}
	}
}
void bintree::post_order()
{
	cout<<"postorder\n";
    postorder(root);
    cout<<"end\n\n";
}
int bintree::nums()
{
	return num;
}
void bintree::road()
{
	road_(root);
}
void bintree::road_(node *root)
{
	int des,pre;
	int flag=0;
	int r[nums()+1];
	for(int i=1;i<=nums();i++)
	  r[i]=i;
	cout<<"Please input the point you want to search for\n";
	scanf("%d",&des);

	stack<node *> s;
	pre=root->data;
	while(root!=NULL || !s.empty())
	{
		while(root!=NULL)
		{
			s.push(root);
			if(root->data==des)
			{
				flag=1;
				break;
			}
			if(root->lc)
			{
				r[root->lc->data]=root->data;
				//pre=root->data;
			}
			root=root->lc;
		}
		if(!s.empty())
		{
			root=s.top();
			s.pop();
			if(root->rc)
			{
				r[root->rc->data]=root->data;
				//pre=root->data;
			}
			root=root->rc;
		}
		if(flag)
		{
			break;
		}
	}
	if(!flag)
	{
		printf("Failed to find that point\n");
		return ;
	}
	for(int i=1;i<=nums();i++)
		printf("%d%s",r[i],i==nums()?"\n":" ");
	stack<int> ss;
	pre=des;
	do{
		ss.push(pre);
		pre=r[pre];
	}while(pre!=1);
	ss.push(r[1]);
	cout<<ss.top();
	ss.pop();
	while(!ss.empty())
	{
		int tep=ss.top();
		ss.pop();
		printf(" -> %d",tep);
	}
	cout<<endl;
}
int main()
{
    bintree b;
    b.bincreate();
    b.pre_order();
    b.in_order();
    b.post_order();
    b.road();
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值