二叉树的常见遍历

二叉树的常见遍历层次遍历用队列,深度遍历非递归用栈

#include <stdio.h>
#include<malloc.h>
#include<stack> 
using namespace std;
struct node
{
	int key;
	struct node* lchild;
	struct node* rchild;

};
typedef struct node NODE,*Ptree;
//返回指针
NODE * CreatTree()
{
	NODE * Phead;
	int nodekey;
	scanf("%d",&nodekey);
	if(0==nodekey)
		return NULL;
	Phead=(NODE*)malloc(sizeof(NODE));

	if(NULL==Phead)
		return NULL;

	Phead->key=nodekey;
	Phead->lchild=CreatTree();
	Phead->rchild=CreatTree();
	return Phead;
}
//至今不明白为什么这样可以
void MakeTree(NODE*& Tree)
{
	Tree=(NODE*)malloc(sizeof(NODE));
	if(Tree==NULL)
		return;
	int s;
	scanf("%d",&s);
	if(s==0)
	{
		Tree=NULL;
		return;
	}
	Tree->key=s;
	MakeTree(Tree->lchild);
	MakeTree(Tree->rchild);

}
//非递归实现三种查找
void firorderpri(NODE* Tree)
{
	stack<NODE* > s;//定义栈
	NODE* p=Tree;
	if(Tree==NULL)
		return;
	s.push(Tree);
	while(!s.empty())
	{
		p=s.top();
		s.pop();
		printf("%d ",p->key);
		if(p->rchild!=NULL)
			s.push(p->rchild);
		if(p->lchild!=NULL)
			s.push(p->lchild);
	}

	
}
//中序遍历
void midorderpri(NODE* Tree)
{
	stack<NODE* > s;
	NODE* p;
	int a[100];
	for(int i=0;i<=99;i++)	//第二次出栈时才访问
		a[i]=0;
	if(Tree==NULL)
		return;
	s.push(Tree);
	
	while(!s.empty())
	{
		p=s.top();
		s.pop();
	
		if(a[p->key]!=1)
		{
			a[p->key]=1;										
		if(p->rchild!=NULL)
			s.push(p->rchild);
		s.push(p);		//中序遍历要求根结点在中间入栈
		if(p->lchild!=NULL)
			s.push(p->lchild);
		}
		else {
		printf("%d ",p->key);

		}

	}
}
//后序遍历
void lastorderpri(NODE* Tree)
{
	stack<NODE* > s;
	NODE* p;
	int a[100];
	for(int i=0;i<=99;i++)
		a[i]=0;
	if(Tree==NULL)
		return;
	s.push(Tree);

	while(!s.empty())
	{
		p=s.top();
		s.pop();
	
		if(a[p->key]!=1)
		{
			a[p->key]=1;
			s.push(p);	//后序遍历要求先入栈							
		if(p->rchild!=NULL)
			s.push(p->rchild);		
		if(p->lchild!=NULL)
			s.push(p->lchild);

		}
		else {
				printf("%d ",p->key);

		}

	}
	
}
//按层次遍历
void QPrintfTree(NODE* Tree)
{
	int in,ou;
	in=ou=0;
	NODE * array[100];	//定义队列
	array[0]=Tree;
	while(ou<=in)
	{
		if(array[ou]->lchild!=NULL)
			array[++in]=array[ou]->lchild;

		if(array[ou]->rchild!=NULL)
			array[++in]=array[ou]->rchild;

		printf("%d  ",array[ou++]->key);
	}

}

//递归实现三种遍历
void PrintfTree(NODE* Tree)
{

    if(NULL==Tree)
		return ;
		static int nth=0;
	nth++;
	printf("%dth is %d\n",nth,Tree->key);
	PrintfTree(Tree->lchild);
	PrintfTree(Tree->rchild);

}
void mPrintfTree(NODE* Tree)
{

    if(NULL==Tree)
		return ;

	mPrintfTree(Tree->lchild);
		static int nmth=0;
	nmth++;
	printf("%dth is %d\n",nmth,Tree->key);
	mPrintfTree(Tree->rchild);

}
void lPrintfTree(NODE* Tree)
{

    if(NULL==Tree)
		return ;
	lPrintfTree(Tree->lchild);
	lPrintfTree(Tree->rchild);
	printf("%dth is %d\n",nlth,Tree->key);

}
//删除树
void deleteTree(NODE* Tree)
{
	if(Tree==NULL)
		return;
	NODE* l,*r;
	l=Tree->lchild;
	r=Tree->rchild;
	free(Tree);
	Tree=NULL;
	deleteTree(l);
	deleteTree(r);

}

void main(){
	NODE * Tree,*tree;	
	Tree=CreatTree();//建二叉树
//	 QPrintfTree(Tree);//层次遍历

	//	PrintfTree(Tree);//前序遍历
       	mPrintfTree(Tree);//中序遍历
	/*	lPrintfTree(Tree);//后序遍历
			MakeTree(tree);
QPrintfTree(tree);//层次遍历

		PrintfTree(tree);//前序遍历*/
	firorderpri(Tree);
		printf("\n");
	midorderpri(Tree);
	printf("\n");
	lastorderpri(Tree);

}


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章