当前位置:网站首页>二叉树的层序遍历(C语言实现)
二叉树的层序遍历(C语言实现)
2022-07-26 13:44:00 【ndream的三水】
层序遍历代码思路
借助一个辅助队列。
- 先将二叉树的根结点入队,
- 然后出队,访问出队结点,
- 若它有左子树,则将左子树根节点入队;
- 若它有右子树,则将右子树根节点入队。
- 然后出队,
- 访问出队结点……如此反复,直到队列为空
void LevelOrder(BiTree T)
{
InitQueue(&Q); // 初始化辅助队列
BiTree p;
EnQueue(&Q, T); // 将根节点入队
while(!IsEmpty(Q)) // 队列不空则循环
{
DeQueue(Q, p); // 队头结点出队
visit(p);
if (p->lchild != NULL)
{
EnQueue(Q, p->lchild); // 左子树不空,则左子树根节点入队
}
if (p->rchild != NULL)
{
EnQueue(Q, p->rchild); // 右子树不空,则右子树根节点入队
}
}
}
实现过程
根据上面的代码思路,需要实现两个数据结构,一个二叉树,一个队列,需要实现的功能有
- 二叉树、队列的数据结构定义
- 二叉树的创建、队列的初始化
- 二叉树的层序遍历、队列的出队、入队操作
代码实现

#include <stdio.h>
#include <stdlib.h>
using namespace std;
typedef char TElemType;
const int MAXSIZES = 50;
// 二叉树的存储结构
typedef struct BiTNode {
TElemType data;
struct BiTNode *lchild, *rchild;
}BiTNode, *BiTree;
typedef BiTree QElemType;
// 队列的存储结构
typedef struct {
QElemType data[MAXSIZES];
int front;
int rear;
}SqQueue;
// 通过前序遍历创建二叉树
void CreateBiTree(BiTree *T)
{
TElemType ch;
scanf("%c", &ch);
if (ch == '#')
{
*T = NULL;
}
else
{
*T = (BiTree) malloc(sizeof(BiTNode));
if (!T)
{
exit(0);
}
(*T)->data = ch;
CreateBiTree(&(*T)->lchild);
CreateBiTree(&(*T)->rchild);
}
}
// 初始化队列
void InitQueue(SqQueue *Q)
{
Q->front = Q->rear = 0;
}
// 辅助队列的入队操作
void EnQueue(SqQueue *Q, QElemType q)
{
if ((Q->rear + 1) % MAXSIZES == Q->front)
return;
Q->data[Q->rear] = q;
Q->rear = (Q->rear + 1) % MAXSIZES;
}
// 辅助队列的出队操作
void DeQueue(SqQueue *Q, QElemType *q)
{
if (Q->front == Q->rear)
return;
*q = Q->data[Q->front];
Q->front = (Q->front + 1) % MAXSIZES;
}
// 二叉树的中序遍历
void InOrderTraverse(BiTree T)
{
if (!T)
{
return;
}
InOrderTraverse(T->lchild);
printf("%c", T->data);
InOrderTraverse(T->rchild);
}
// 二叉树的层序遍历
void LevelOrder(BiTree T)
{
SqQueue Q;
InitQueue(&Q); // 初始化辅助队列
BiTree p;
EnQueue(&Q, T); // 将根节点入队
while(Q.front != Q.rear) // 队列不空则循环
{
DeQueue(&Q, &p); // 队头结点出队
printf("%c", p->data);
if (p->lchild != NULL)
{
EnQueue(&Q, p->lchild); // 左子树不空,则左子树根节点入队
}
if (p->rchild != NULL)
{
EnQueue(&Q, p->rchild); // 右子树不空,则右子树根节点入队
}
}
}
int main()
{
BiTree t;
printf("请按照前序扩展二叉树进行输入:(#表示空结点)\n");
CreateBiTree(&t);
printf("中序遍历: ");
InOrderTraverse(t);
printf("\n层序遍历: ");
LevelOrder(t);
return 0;
}
参考:
层序遍历思路来《王道数据结构》
边栏推荐
- Pytoch learning notes (I) installation and use of common functions
- Videojs to canvas pause, play, switch video
- Pytorch学习笔记(二)神经网络的使用
- I. creation and constraint of MySQL table
- [turn] judge the relationship between two geometries in ArcGIS
- 万字长文,浅谈企业数字化建模蓝图
- Click El dropdown item/@click.native
- See you tomorrow at the industrial session of cloud intelligence technology forum!
- WPS凭什么拒绝广告?
- Flutter multi-channel packaging operation
猜你喜欢

Multi objective optimization series 1 --- explanation of non dominated sorting function of NSGA2

Detailed explanation of factory mode

Probability theory and mathematical statistics

Abstract factory and its improvement examples

万字长文,浅谈企业数字化建模蓝图

Brief introduction of reflection mechanism

Ultimate doll 2.0 | cloud native delivery package

【C语言学习者必会的题目集锦1】巩固基础,稳步提高

The serialization class in unity is in JSON format
![[oauth2] v. oauth2loginauthenticationfilter](/img/54/3c3f02511e30c301a5cea6f6ddd4c2.png)
[oauth2] v. oauth2loginauthenticationfilter
随机推荐
Pass parameters to the routing component
Abstract factory and its improvement examples
How to write the introduction of GIS method journals and papers?
Analysis on the current situation and optimization strategy of customer experience management in banking industry
B+ tree index use (9) grouping, back to table, overlay index (21)
421. 数组中两个数的最大异或值
重押海外:阿里、京东、顺丰再拼“内力”
天津市应急局与驻津央企签署协议深化应急联动机制建设
Algorithm -- continuous sequence (kotlin)
Cavans realizes Static Rolling barrage
万字长文,浅谈企业数字化建模蓝图
Pytoch learning notes (II) the use of neural networks
天翼云Web应用防火墙(边缘云版)支持检测和拦截Apache Spark shell命令注入漏洞
飞盘,2022年“黑红”顶流
B+ tree index use (6) leftmost principle -- MySQL from entry to proficiency (18)
B+ tree (5) introduction to MyISAM -- MySQL from getting started to mastering (17)
Brief introduction of reflection mechanism
「中高级试题」:MVCC实现原理是什么?
Golang port scanning design
最新战报:十项认证,五项最佳实践