(c语言)树的层序遍历(包含测试用例源码)

本实验取材于浙江大学《数据结构》
除了先序、中序和后序三种基本的二叉树遍历方法外,有时还用到二叉树的层序遍历。层序遍历是按树的层次,从第1层的根结点开始向下逐层访问每个结点,对某一层中的结点是按从左到右的顺序访问。因此在进行层序遍历时,完成某一层结点的访问后,再按它们的访问次序依次访问各结点的左右孩子,这样一层一层进行下去,先遇到的结点先访问,这与队列的操作过程是吻合的。具体的算法实现可以设置一个队列结构,遍历从根节点开始,首先将根节点指针入队,然后开始执行下面三个操作:

  1. 从队列中取出一个元素;
  2. 访问该元素所指结点;
  3. 若该元素所指结点的左、右孩子结点非空,则将其左、右孩子的指针顺序入队。

不断执行这三步操作,直到队列为空,再无元素可取,二叉树的层序遍历就完成了。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

//树的层序遍历--采用队列模型
//设置一个队列结构,遍历从根节点开始,首先将根节点指针入队,然后开始执行下面三个操作:
//1、从队列中取出一个元素;
//2、访问该元素所指结点;
//3、若该元素所指结点的左右孩子结点非空,则将其左、右孩子的指针顺序入队
//不断执行这三部操作,直到队列为空,再无元素可取,二叉树的程序遍历就完成了。
#define ERROR -1
#define MaxSizel 10
//typedef struct SNode *PtrToSNode;
typedef int ElementType;
typedef struct TNode *Position;
typedef Position BinTree; /* 二叉树类型 */
struct TNode{ /* 树结点定义 */
    ElementType Data; /* 结点数据 */
    BinTree Left;     /* 指向左子树 */
    BinTree Right;    /* 指向右子树 */
};
struct QNode{
	ElementType *Data;
	int rear;
	int front;
	int MaxSize;
};
typedef struct QNode *Queue;
Queue CreateQueue(int MaxSize1);//生成长度为MaxSize的空队列
int IsFullQ(Queue Q);//判断队列Q是否已满
void AddQ(Queue Q,Position item);//将数据元素item插入队列Q中
int IsEmptyQ(Queue Q);//判断队列Q是否为空
ElementType DeleteQ(Queue Q);//将队头数据元素从队列中删除并返回
Queue CreateQueue(int MaxSize1){
	Queue Q = (Queue)malloc(sizeof(struct QNode));
	Q->Data = (ElementType *) malloc(MaxSize1 * sizeof(ElementType));
	Q->front = Q->rear = 0;
	Q->MaxSize = MaxSize1;
	return Q;
	
}
int IsFullQ(Queue Q){
	
	return ((Q->rear+1)%Q->MaxSize == Q->front);
}
void  AddQ(Queue Q,Position item){
	if(IsFullQ(Q)){
		printf("Queue is full\n");
		return ;
	}
	Q->rear = (Q->rear+1) %Q->MaxSize;
	Q->Data[Q->rear] = (int)item;
}
int IsEmptyQ(Queue Q){
	return (Q->front == Q->rear);
}
ElementType DeleteQ(Queue Q){
	if(IsEmptyQ(Q))
	{
		printf("Queue is empty\n");
		return ERROR;
		
	}else{
		Q->front = (Q->front+1)%Q->MaxSize;
		return Q->Data[Q->front];
	}
}
void LevelorderTraversal(BinTree BT)
{
	Queue Q;
	BinTree T;
	if(!BT) return;
	Q=CreateQueue(MaxSizel);
	AddQ(Q,BT);
	while(!IsEmptyQ(Q)){
		T=(struct TNode * )DeleteQ(Q);
		printf("%d ",T->Data);//访问取出队列的结点
		if(T->Left) AddQ(Q,T->Left);
		if(T->Right) AddQ(Q,T->Right);
	}
}
int main()
{
	
		BinTree Bt = (BinTree)malloc(sizeof(struct TNode));
	BinTree Bt1 = (BinTree)malloc(sizeof(struct TNode));
	BinTree Bt2 = (BinTree)malloc(sizeof(struct TNode));
	BinTree Bt3 = (BinTree)malloc(sizeof(struct TNode));
	BinTree Bt4 = (BinTree)malloc(sizeof(struct TNode));
	Bt->Data = 1;
	Bt1->Data=2;
	Bt2->Data=3;
	Bt3->Data=4;
	Bt4->Data=5;
	Bt->Left = Bt1;
	Bt->Right = Bt2;
	Bt1->Left = NULL;
	Bt1->Right = NULL;
	
	Bt2->Left = Bt3;
	Bt2->Right = Bt4;
	Bt3->Left = NULL;
	Bt3->Right = NULL;
	Bt4->Left = NULL;
	Bt4->Right = NULL;	
	printf("\nceng xu bian li\n");
	
	LevelorderTraversal(Bt);
	printf("\n");

	
	return 0;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值