二叉树的遍历

43 篇文章 0 订阅
16 篇文章 0 订阅

这里为了方便,以二叉查找树为例:

#include<stdio.h>
#include<malloc.h>
#define M 25
struct BST
{
	int Data;
	BST *left;
	BST *right;
};
BST *stack[M];//栈
int flag[M];//后序遍历中需要用到的标记数组
bool AddNodeToBST(BST* &Root, int Value)
{
	if(Root == NULL)
	{
		BST *Node = (BST *)malloc(sizeof(BST));
		if(!Node)
		{
			printf("内存分配失败\n");
			return false;
		}
		Node->Data = Value;
		Node->left = NULL;
		Node->right = NULL;
		Root = Node;
		return true;
	}
	else if(Root->Data > Value)
		AddNodeToBST(Root->left,Value);
	else if(Root->Data < Value)
		AddNodeToBST(Root->right, Value);
	else
	{
		printf("重复加入节点\n");
		return false;
	}
}
void Destory(BST *Root)
{
	if(!Root)
		return;
	Destory(Root->left);
	Destory(Root->right);
	free(Root);
	Root = NULL;
}
//递归前序遍历,先根,再左,后右
void PreOrderRe(BST *Root)
{
	if(!Root)
		return;

	printf("%d ",Root->Data);
	PreOrderRe(Root->left);
	PreOrderRe(Root->right);
}
//递归的中序遍历,先左,再根,后右
void MidOrderRe(BST *Root)
{
	if(!Root)
		return;
	MidOrderRe(Root->left);
	printf("%d ",Root->Data);
	MidOrderRe(Root->right);
}
//递归的后序遍历,先左,再右,后根
void PostOrderRe(BST *Root)
{
	if(!Root)
		return;
	PostOrderRe(Root->left);
	PostOrderRe(Root->right);
	printf("%d ", Root->Data);
}
void PreOrder(BST *Root)
{
	if(!Root)
		return;

	BST *p;
	int top = -1;
	p = Root;

	while(p != NULL || top > -1)
	{
		while(p != NULL)
		{
			printf("%d ",p->Data);
			stack[++top] = p;
			p = p->left;
		}
		//由于叶节点的左右都为空,所以遇到叶节点的时候会弹出两次。遇到左空,弹本身,遇到右空,弹出父节点
		//三种遍历的思想是一样的
		if(top > -1)
		{
			p = stack[top]->right;
			--top;
		}
	}
	printf("\n");
}
void MidOrder(BST *Root)
{
	if(!Root)
		return;
	int top = -1;
	BST *p = Root;

	while(p != NULL || top > -1)
	{
		while(p != NULL)
		{
			stack[++top] = p;
			p = p->left;
		}
		if(top > -1)
		{
			printf("%d ", stack[top]->Data);
			p = stack[top]->right;
			--top;
		}
	}
	printf("\n");
}
void PostOrder(BST *Root)
{
	int top = -1;
	BST *p = Root;

	while(p != NULL || top > -1)
	{
		while(p != NULL)
		{
			stack[++top] = p;
			flag[top] = 0;
			p = p->left;
		}
		while(top > -1 && flag[top])
		{
			printf("%d ", stack[top]->Data);
			--top;
		}
		if(top > -1)
		{
			flag[top] = 1;
			p = stack[top]->right;
		}
	}
	printf("\n");
}
void main()
{
	BST *Root = NULL;
	int Value;
	//输入数据
	while(scanf("%d", &Value))
	{
		if(Value <= 0)
			break;
		AddNodeToBST(Root, Value);
	}

	PreOrderRe(Root);
	printf("\n");
	PreOrder(Root);

	MidOrderRe(Root);
	printf("\n");
	MidOrder(Root);

	PostOrderRe(Root);
	printf("\n");
	PostOrder(Root);

	Destory(Root);
}
运行结果:


PS:二叉树的层次遍历,这里的stack数组如上定义,实际上是用的队列。

void LevelOrder(BST *Root)
{
	if(!Root)
		return;
	int head = 1;
	int tail = 1;
	stack[tail] = Root;
	flag[0] = 0;
	flag[1] = 1;
	while(head <= tail)
	{
		if(flag[head - 1] < flag[head])
			printf("\n");
		printf("%d ", stack[head]->Data);
		if(stack[head]->left)
		{
			stack[++tail] = stack[head]->left;
			flag[tail] = flag[head] + 1;
		}
		if(stack[head]->right)
		{
			stack[++tail] = stack[head]->right;
			flag[tail] = flag[head] + 1;
		}
		++head;
	}
	printf("\n");
}

结果:
10
5 15
3 8 14 17
1 4 7 9 16 18

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值