实现二叉树的先序遍历、中序遍历、后序遍历的递归非递归算法以及层次遍历算法

#include"iostream"
#define maxsize 50
using namespace std;
class node{
private:
	char data;
	node* lchild;
	node* rchild;
public:
	void createnode(node *&,char *);
	void fnode(node* b)
	{
		if(b!=NULL)
		{
			cout << b->data ;
			fnode(b->lchild);
			fnode(b->rchild);
		}
	}
	void mnode(node* b)
	{
		if(b!=NULL)
		{
			mnode(b->lchild);
			cout << b->data ;
			mnode(b->rchild);
		}
	}
	void lnode(node* b)
	{
		if(b!=NULL)
		{
			lnode(b->lchild);
			lnode(b->rchild);
			cout << b->data ;
		}
	}
	void fnode1(node *);
	void mnode1(node *);
	void lnode1(node *);
	void all(node *);
};

void node::createnode(node* &b,char* a){
	node *st[maxsize],*p;
	int top=-1,k,j=0;
	char ch;
	b=NULL;
	ch=a[j];
	while(ch!='\0')
	{
		switch(ch)
		{
		case '(':
			top++;
			st[top]=p;
			k=1;
			break;
		case ')':
			top--;
			break;
		case ',':
			k=2;
			break;
		default:
			p=new node;
			p->data=ch;
			p->lchild=p->rchild=NULL;
			if(b==NULL){
				b=p;
			}
			else{
				switch(k)
				{
				case 1:
					st[top]->lchild=p;
					break;
				case 2:
					st[top]->rchild=p;
					break;
				}
			}
		}
		j++;
		ch=a[j];
	}
}

void node::fnode1(node* b)
{
	node* s[maxsize],*p;
	int top=-1;
	if(b!=NULL)
	{
		top++;
		s[top]=b;
		while(top>-1)
		{
			p=s[top];
			top--;
			cout << p->data ;
			if(p->rchild!=NULL)
			{
				top++;
				s[top]=p->rchild;
			}
			if(p->lchild!=NULL)
			{
				top++;
				s[top]=p->lchild;
			}
		}
	}
}

void node::mnode1(node* b)
{
	node *s[maxsize],*p;
	int top=-1;
	if(b!=NULL)
	{
		p=b;
		while(top>-1||p!=NULL)
		{
			while(p!=NULL)
			{
				top++;
				s[top]=p;
				p=p->lchild;
			}
			if(top>-1)
			{
				p=s[top];
				top--;
				cout << p->data ;
				p=p->rchild;
			}
		}
	}
}

void node::lnode1(node* b)
{
	node* s[maxsize],*p;
	int flag,top=-1;
	if(b!=NULL)
	{
		do
		{
			while(b!=NULL)
			{
				top++;
				s[top]=b;
				b=b->lchild;
			}
			p=NULL;
			flag=1;
			while(top!=-1&&flag)
			{
				b=s[top];
				if(b->rchild==p)
				{
					cout << b->data ;
					top--;
					p=b;
				}
				else
				{
					b=b->rchild;
					flag=0;
				}
			}
		}while(top!=-1);
	}
}

void node::all(node* b)
{
	struct snode
	{
		node *a;
		int parent;
	}qu[maxsize];
	node *q;
	int front,rear,p;
	front=rear=-1;
	rear++;
	qu[rear].a=b;
	qu[rear].parent=-1;
	while(front!=rear)
	{
		front++;
		q=qu[front].a;
		if(q->lchild==NULL&&q->rchild==NULL)
		{
			p=front;
			while(qu[p].parent!=-1)
			{
				cout << qu[p].a->data;
				p=qu[p].parent;
			}
			cout << qu[p].a->data;
		}
		if(q->lchild!=NULL)
		{
			rear++;
			qu[rear].a=q->lchild;
			qu[rear].parent=front;
		}
		if(q->rchild!=NULL)
		{
			rear++;
			qu[rear].a=q->rchild;
			qu[rear].parent=front;
		}
	}
}


void main()
{
	node *b;
	char a[]="A(B(D,E(H(J,K(L,M(,N))),)),C(F,G(,I)))";
	b->createnode(b,a);
	cout << "递归先序编历:";
	b->fnode(b);
	cout << endl ;
	cout << "递归中序编历:";
	b->mnode(b);
	cout << endl ;
	cout << "递归后序编历:";
	b->lnode(b);
	cout << endl ;
	cout << "非递归先序编历:";
	b->fnode1(b);
	cout << endl ;
	cout << "非递归中序编历:";
	b->mnode1(b); 
	cout << endl ;
	cout << "非递归后序编历:";
	b->lnode1(b);
	cout << endl ;
	cout << "层次遍历:";
	b->all(b);
	cout << endl ;
}

    原文作者:递归算法
    原文地址: https://blog.csdn.net/u012651730/article/details/16833125
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞