二叉树的常见遍历

本文深入探讨了二叉树的遍历方法,包括层次遍历使用队列实现,深度遍历非递归使用栈,以及前序、中序、后序遍历的非递归实现方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值