二叉樹的常見遍歷

二叉樹的常見遍歷層次遍歷用隊列,深度遍歷非遞歸用棧

#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);

}


 

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