二叉树的层序遍历(c)

我们先来了解一下什么是二叉树的层序遍历!

层序遍历的理解

下图是一棵二叉树,

层序遍历这棵二叉树的顺序是:

1→2→3→4→5→6→7,

看图也很简单易懂,

第一层→第二层→第三层。

层序遍历的实现

如果二叉树的结点以数组的方式储存,那就是打印一遍数组就完成了层序遍历。

但是,我们用的是链式结构,难度就会蹭蹭往上涨......

我们实现层析遍历需要使用队列(queue).

队列的特点是先进先出,观察下图的橙色箭头→:

只要入队列的顺序是1234567,那么出队列的顺序也是1234567.

我们就实现了层序遍历。

先上队列的代码,

头文件Queue.h

cpp 复制代码
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>

typedef int QDataType;

typedef struct QueueNode
{
	struct QueueNode* next;
	QDataType val;
}QNode;

typedef struct Queue
{
//指向队列头的指针
	QNode* phead;
//指向队列尾的指针
	QNode* ptail;
	int size;
}Queue;

//队列初始化
void QueueInit(Queue* pq);
//销毁队列
void QueueDestroy(Queue* pq);
//在队尾插入元素
void QueuePush(Queue* pq, QDataType x);
//删除队头元素
void QueuePop(Queue* pq);
//队列长度
int QueueSize(Queue* pq);
//返回队头元素的值
QDataType QueueFront(Queue* pq);
//返回队尾元素的值
QDataType QueueBack(Queue* pq);
//判断队列是否为空
bool QueueEmpty(Queue* pq);

源文件Queue.c

cpp 复制代码
#include"Queue.h"

void QueueInit(Queue* pq)
{
	pq->phead = NULL;
	pq->ptail = NULL;
	pq->size = 0;
}

void QueueDestroy(Queue* pq)
{
	assert(pq);
	
	QNode* cur = pq->phead;
	while (cur) {
		QNode* temp = cur->next;
		free(cur);
		cur = temp;
	}
	pq->phead = pq->ptail = NULL;
	pq->size = 0;
}

void QueuePush(Queue* pq, QDataType x)
{
	assert(pq);
	QNode* temp = (QNode*)malloc(sizeof(QNode));
	if (temp == NULL) {
		perror("malloc fail");
		return;
	}
	temp->next = NULL;
	temp->val = x;
	if (pq->ptail == NULL) {
		pq->phead = pq->ptail = temp;
	}
	else
	{
		pq->ptail->next = temp;
		pq->ptail = temp;
	}
	pq->size++;
	
	
}
void QueuePop(Queue* pq)
{
	assert(pq);
	assert(pq->size!=0);
	QNode* temp = pq->phead->next;
	free(pq->phead);
	pq->phead = temp;
	if (pq->phead == NULL) {
		pq->ptail = NULL;
	}
	pq->size--;
}
int QueueSize(Queue* pq) {
	assert(pq);
	return pq->size;
}
QDataType QueueFront(Queue* pq)
{
	assert(pq);
	assert(pq->phead);
	return pq->phead->val;
}
QDataType QueueBack(Queue* pq)
{
	assert(pq);
	assert(pq->ptail);
	return pq->ptail->val;
}
bool QueueEmpty(Queue* pq) {
	assert(pq);
	return pq->size == 0;
}

然后是层序遍历的实现。

cpp 复制代码
void TreeLevelOrder(BTNode* root)
{
	Queue pq;
	QueueInit(&pq);

//根结点先入队列
	if (root)
	{
		QueuePush(&pq, root);
	}

	while (!QueueEmpty(&pq))
	{
//存着队列头的结点
		BTNode* front = QueueFront(&pq);
//删除队列头的节点
		QueuePop(&pq);
//打印队列头结点的值
		printf("%d ", front->data);
//左右节点先后入队列
		if(front->left)
		QueuePush(&pq, front->left);
		if(front->right)
		QueuePush(&pq, front->right);
	}

	QueueDestroy(&pq);
	
}
相关推荐
好家伙VCC36 分钟前
数学建模模型 全网最全 数学建模常见算法汇总 含代码分析讲解
大数据·嵌入式硬件·算法·数学建模
liulilittle2 小时前
IP校验和算法:从网络协议到SIMD深度优化
网络·c++·网络协议·tcp/ip·算法·ip·通信
bkspiderx4 小时前
C++经典的数据结构与算法之经典算法思想:贪心算法(Greedy)
数据结构·c++·算法·贪心算法
中华小当家呐5 小时前
算法之常见八大排序
数据结构·算法·排序算法
沐怡旸5 小时前
【算法--链表】114.二叉树展开为链表--通俗讲解
算法·面试
tju新生代魔迷6 小时前
数据结构:双向链表
数据结构·链表
一只懒洋洋6 小时前
K-meas 聚类、KNN算法、决策树、随机森林
算法·决策树·聚类
方案开发PCBA抄板芯片解密7 小时前
什么是算法:高效解决问题的逻辑框架
算法
songx_997 小时前
leetcode9(跳跃游戏)
数据结构·算法·游戏
学c语言的枫子7 小时前
数据结构——双向链表
c语言·数据结构·链表