文章目录
- 前言
- 一、二叉树链结构
- 二、二叉树遍历
-
- (一)前、中、后序遍历的基本概念
- (二)实现前中后序遍历
-
- 1、二叉树的前序遍历
- 二、二叉树中序遍历
- 3.二叉树的后序遍历
- (三)其他相关函数接口
-
- 1.要求所有结点的数量
- 2.叶结点的数量
- 3.要求K层的结点数
- 四、二叉树的深度
- 5.二叉树搜索值为x的结点
- 销毁二叉树 - 后序遍历
- (四)层序遍历
-
- 1、void LevelOrder(BTNode* root)
- 2、Queue.h
- 3、Queue.c
- 4、test.c
- 5.判断二叉树是否完全是二叉树(*)
前言
- 本博客主要介绍了二叉树链结构及相关界面函数的实现,包括前、中、后顺序的遍历,并运用分治的思路编写相关函数
- 重点:
- 代码:C语言
一、二叉树链结构
链式存储结构是指用链表表示二叉树,即用链表示元素的逻辑关系。 。链式结构分为二叉链和三叉链。目前,二叉链通常在我们的学习中,以下课程 学习红黑树等高级数据结构将使用三叉链。
- :在学习二叉树的基本操作之前,你需要创建一棵二叉树,然后学习它的相关基本操作。因为现在我们对二叉树的结构还不够深入,为了降低学习成本,,快速进入二叉树的操作学习,当二叉树的结构几乎被理解时,我们新研究二叉树的真正创造方法。:
typedef int BTDateType; typedef struct BinaryTreeNode {
struct BinaryTreeNode* left; struct BinaryTreeNode* right; BTDateType date; }BTNode; BTNode* BuyNode(BTDateType x) {
BTNode* node = (BTNode*)malloc(sizeof(BTNode)); assert(node); node->date = x; node->left = NULL; node->right = NULL; return node; } BTNode* CreatBinaryTree() {
BTNode* node1 = BuyNode(1); BTNode* node2 = BuyNode(2);
BTNode* node3 = BuyNode(3);
BTNode* node4 = BuyNode(4);
BTNode* node5 = BuyNode(5);
BTNode* node6 = BuyNode(6);
node1->left = node2;
node1->right = node4;
node2->left = node3;
node4->left = node5;
node4->right = node6;
return node1;
}
- 注意:上述代码并不是创建二叉树的方式,真正创建二叉树方式后序详解重点讲解。该代码创建出的二叉树如下图所示,后序操作均围绕该树:
- 看二叉树基本操作前,再:
- 从概念中可以看出,二。
二、二叉树的遍历
(一)、前中后序遍历的基本概念
学习二叉树结构,最简单的方式就是遍历。。访问结点所做的操作依赖于具体的应用问题。 遍历是二叉树上最重要的运算之一,也是二叉树上进行其它运算的基础。
:
- 前序遍历(Preorder Traversal 亦称先序遍历)——访问的操作发生在遍历其左右子树。
- 中序遍历(Inorder Traversal)——访问的操作发生在遍历其左右子树之。
- 后序遍历(Postorder Traversal)——访问的操作发生在遍历其左右子树。
由于被访问的结点必是某子树的根,所以。NLR、LNR和LRN分别又称为先根遍历、中根遍历和后根遍历。 前中后序遍历的函数接口如下:
// 二叉树前序遍历
void PreOrder(BTNode* root);
// 二叉树中序遍历
void InOrder(BTNode* root);
// 二叉树后序遍历
void PostOrder(BTNode* root);
下面,中序与后序图解类似,这里就不过多赘述。
- 主要是利用分治思想,转换成左子树和右子树的遍历,然后再继续分治下去,直到边界
(二)、前中后序遍历的实现
1、二叉树的前序遍历
void PreOrder(BTNode* root)
{
if (root == NULL)
{
printf("# ");
return;
}
printf("%d ", root->date);
PreOrder(root->left);
PreOrder(root->right);
}
2、二叉树的中序遍历
void InOrder(BTNode* root)
{
if (root == NULL)
{
printf("# ");
return;
}
InOrder(root->left);
printf("%d ", root->date);
InOrder(root->right);
}
3、二叉树的后序遍历
void PosOrder(BTNode* root)
{
if (root == NULL)
{
printf("# ");
return;
}
PosOrder(root->left);
PosOrder(root->right);
printf("%d ", root->date);
}
(三)、其他相关的函数接口
运用,还有以下几个相关的函数接口:
//用分治思想 - 求所有结点的数量
int TreeSize(BTNode* root)
//求叶子结点的数量 - 分治
int TreeLeafSize(BTNode* root)
//求第K层的结点个数 K >= 1
int TreeKLevel(BTNode* root, int k)
//求二叉树的深度
int TreeDepth(BTNode* root)
//二叉树查找值为x的结点
BTNode* TreeFind(BTNode* root, BTDateType x)
1、求所有结点的数量
int TreeSize(BTNode* root)
{
return root == NULL ? 0 :
TreeSize(root->left) + TreeSize(root->right) + 1;
}
2、求叶子结点的数量
int TreeLeafSize(BTNode* root)
{
if (root == NULL)
return 0;
if (root->left == NULL && root->right == NULL)
return 1;
return TreeLeafSize(root->left) + TreeLeafSize(root->right);
}
3、求第K层的结点个数
int TreeKLevel(BTNode* root, int k)
{
assert(k >= 1);
if (root == NULL)
return 0;
if (k == 1)
return 1;
return TreeKLevel(root->left, k - 1) + TreeKLevel(root->right, k - 1);
}
4、求二叉树的深度
int TreeDepth(BTNode* root)
{
if (root == NULL)
return 0;
int leftDepth = TreeDepth(root->left);
int rightDepth = TreeDepth(root->right);
return leftDepth > rightDepth ? leftDepth + 1 : rightDepth + 1;
}
5、二叉树查找值为x的结点
BTNode* TreeFind(BTNode* root, BTDateType x)
{
if (root == NULL)
return NULL;
if (root->date == x)
return root;
BTNode* ret1 = TreeFind(root->left, x);
if (ret1)
return ret1;
BTNode* ret2 = TreeFind(root->right, x);
if (ret2)
return ret2;
return NULL;
}
6、二叉树的销毁 - 后序遍历
void TreeDestroy(BTNode* root)
{
if (root == NULL)
{
return;
}
TreeDestroy(root->left);
TreeDestroy(root->right);
free(root);
}
(四)、层序遍历
- :除了先序遍历、中序遍历、后序遍历外,还可以对二叉树进行层序遍历。设二叉树的根节点所在层数为1,层序遍历就是从所在二叉树的根节点出发,首先访问第一层的树根节点,然后从左到右访问第2层上的节点,接着是第三层的节点,以此类推,。 层序遍历一般,函数接口如下:
// 层序遍历
void LevelOrder(BTNode* root);
1、void LevelOrder(BTNode* root)
// 层序遍历
//队列的数据应该存结点的指针
void LevelOrder(BTNode* root)
{
Queue q;
QueueInit(&q);
if (root)
{
//进队列
QueuePush(&q, root);
}
while (!QueueEmpty(&q))
{
//出对头的数据
BTNode* front = QueueFront(&q);
printf("%d ", front->date);
QueuePop(&q);//释放的是队列的结点,而不是树的结点
//每一层从左至右,依次入队
if (front->left)
{
QueuePush(&q, front->left);
}
if (front->right)
{
QueuePush(&q, front->right);
}
}
printf("\n");
QueueDestroy(&q);
}
2、Queue.h
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
//链式结构:表示队列
//前置声明
struct BinaryTreeNode;
typedef struct BinaryTreeNode* QDateType;
typedef struct QueueNode
{
struct QueueNode* next;
QDateType date;
}QNode;
//队列的结构
typedef struct Queue
{
//int size;
QNode* head;
QNode* tail;
}Queue;
//初始化队列
void QueueInit(Queue* pq);
//销毁队列
void QueueDestroy(Queue* pq);
//队尾入队列
void QueuePush(Queue* pq, QDateType x);
//队头出队列
void QueuePop(Queue* pq);
//获取队列头部元素
QDateType QueueFront(Queue* pq);
//获取队列队尾元素
QDateType QueueBack(Queue* pq);
//检测队列是否为空
bool QueueEmpty(Queue* pq);
//获取队列的长度(队列中存放数据的个数)
int QueueSize(Queue* pq);
3、Queue.c
#define _CRT_SECURE_NO_WARNINGS 1
#pragma warning(disable:6031)
#include "Queue.h"
void QueueInit(Queue* pq)
{
assert(pq);
pq->head = NULL;
pq->tail = NULL;
}
void QueueDestroy(Queue* pq)
{
assert(pq);
QNode* cur = pq->head;
while (cur)
{
QNode* next = cur->next;
free(cur);
cur = next;
}
pq->head = pq->tail = NULL;
}
void QueuePush(Queue* pq, QDateType x)
{
assert(pq);
QNode* newnode = (QNode*)malloc(sizeof(QNode));
if (newnode == NULL)
{
printf("malloc fail");
exit(-1);
}
newnode->date = x;
newnode->next = NULL;
if (pq->tail == NULL)
{
pq->head = pq->tail = newnode;
}
else
{
pq->tail->next = newnode;
pq->tail = newnode;
}
}
void QueuePop(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
if (pq->head->next == NULL)//只剩下一个结点
{
free(pq->head);
pq->head = pq->tail = NULL;
}
else//多个结点
{
QNode* next = pq->head->next;
free(pq->head);
pq->head = next;
}
}
QDateType QueueFront(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
return pq->head->date;
}
QDateType QueueBack(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
return pq->tail->date;
}
bool QueueEmpty(Queue* pq)
{
assert(pq);
return pq->head == NULL;
}
int QueueSize(Queue* pq)
{
assert(pq);
int size = 0;
QNode* cur = pq->head;
while (cur)
{
size++;
cur = cur->next;
}
return size;
}
4、test.c
#define _CRT_SECURE_NO_WARNINGS 1 #pragma warning(disable:6031) #include "Heap.h" #include "Queue.h" typedef int BTDateType; typedef struct BinaryTreeNode { struct BinaryTreeNode* left; struct BinaryTreeNode* right; BTDateType date; }BTNode; BTNode* BuyNode(BTDateType x) { BTNode* node = (BTNode*)malloc(sizeof(BTNode)); assert(node); node->date = x; node->left = NULL; node->right = NULL; return node; } BTNode* CreatBinaryTree() { BTNode* node1 = BuyNode(1); BTNode* node2 = BuyNode(2); BTNode* node3 = BuyNode(3); BTNode* node4 = BuyNode(4); BTNode* node5 = BuyNode(5); BTNode* node6 = BuyNode(6); node1->left = node2; node1->right = node4; node2->left = node3; node4->left = node5; node4->right = node6; return node1; } // 层序遍历 //队列的数据应该存结点的指针 void LevelOrder(BTNode* root) { Queue q; QueueInit(&q); if (root) { //进队列 QueuePush(&q, root); } while (!QueueEmpty(&q)) { //出对头的数据 BTNode* front = QueueFront(&q); printf("%d ", front->date); QueuePop(&q); //每一层从左至右,依次入队 if (front->left) { QueuePush(&q, front->left); } if (front->right) { QueuePush(&q, front->right); } } printf("\n"); QueueDestroy(&q); } int main() { BTNode* root = CreatBinaryTree(); LevelOrder(root); return