文章目录
1. 树的概念及结构
1.1 树的概念
树是一种非线性的数据结构,它是由n(n>=0)个有限结点组成一个具有层次关系的集合。
- 有一个特殊的结点,称为根结点,根节点没有前驱结点。除根节点外,其余结点被分成M(M>0)个互不相交的集合T1、T2、……、Tm,其中每一个集合Ti(1<= i <= m)又是一棵结构与树类似的子树。
- 每棵子树的根结点有且只有一个前驱,可以有0个或多个后继
因此,树是递归定义的。
注意:树形结构中,子树之间不能有交集,否则就不是树形结构
1.2 树的结构
1.3 树的相关概念
名称 | 解释 |
---|---|
节点的度 | 一个节点含有的子树的个数称为该节点的度。 如上图:A的度为6 |
叶节点或终端节点 | 度为0的节点称为叶节点; 如上图:B、C、H、I…等节点为叶节点 |
非终端节点或分支节点 | 度不为0的节点; 如上图:D、E、F、G…等节点为分支节点 |
双亲节点或父节点 | 若一个节点含有子节点,则这个节点称为其子节点的父节点; 如上图:A是B的父节点 |
孩子节点或子节点 | 一个节点含有的子树的根节点称为该节点的子节点; 如上图:B是A的孩子节点 |
兄弟节点 | 具有相同父节点的节点互称为兄弟节点; 如上图:B、C是兄弟节点 |
树的度 | 一棵树中,最大的节点的度称为树的度; 如上图:树的度为6 |
节点的层次 | 从根开始定义起,根为第1层,根的子节点为第2层,以此类推 |
树的高度或深度 | 树中节点的最大层次; 如上图:树的高度为4 |
堂兄弟节点 | 双亲在同一层的节点互为堂兄弟;如上图:H、I互为兄弟节点 |
节点的祖先 | 从根到该节点所经分支上的所有节点;如上图:A是所有节点的祖先 |
子孙 | 以某节点为根的子树中任一节点都称为该节点的子孙。如上图:所有节点都是A的子孙 |
森林 | 由m(m>0)棵互不相交的树的集合称为森林; |
1.4 树的表示
树结构存储既要保存值域,也要保存结点和结点之间的关系,实际中树有很多种表示方式如:双亲表示法,孩子表示法、孩子双亲表示法以及孩子兄弟表示法等。我们这里就简单的了解其中最常用的孩子兄弟表示法。
typedef int DataType;
struct Node
{
struct Node* _firstChild1; // 第一个孩子结点
struct Node* _pNextBrother; // 指向其下一个兄弟结点
DataType _data; // 结点中的数据域
};
2. 二叉树的概念及结构
2.1 二叉树的概念
一棵二叉树是结点的一个有限集合,该集合:
- 或者为空
- 由一个根节点加上两棵别称为左子树和右子树的二叉树组成
从上图可以看出:
- 二叉树不存在度大于2的结点
- 二叉树的子树有左右之分,次序不能颠倒,因此二叉树是有序树
注意:对于任意的二叉树都是由以下几种情况复合而成的:
2.2 特殊的二叉树
- 满二叉树:一个二叉树,如果每一个层的结点数都达到最大值,则这个二叉树就是满二叉树。也就是说,如果一个二叉树的层数为K,且结点总数是 ,则它就是满二叉树。
- 完全二叉树:完全二叉树是效率很高的数据结构,完全二叉树是由满二叉树而引出来的。对于深度为K的,有n个结点的二叉树,当且仅当其每一个结点都与深度为K的满二叉树中编号从1至n的结点一一对应时称之为完全二叉树。 要注意的是满二叉树是一种特殊的完全二叉树
2.3 二叉树的性质
- 若规定根节点的层数为1,则一棵非空二叉树的第i层上最多有 2 ( i − 1 ) {2^{(i-1)}} 2(i−1)个结点
- 若规定根节点的层数为1,则深度为h的二叉树的最大结点数是 2 h − 1 {2^h-1} 2h−1
- 对任何一棵二叉树, 如果度为0其叶结点个数为 n 0 n_0 n0, 度为2的分支结点个数为 n 2 n_2 n2 ,则有 n 0 = n 2 + 1 n_0=n_2+1 n0=n2+1
- 若规定根节点的层数为1,具有n个结点的满二叉树的深度 h = l o g 2 ( h + 1 ) h=log_2^{(h+1)} h=log2(h+1)
- 对于具有n个结点的完全二叉树,如果按照从上至下从左至右的数组顺序对所有节点从0开始编号,则对于序号为i的结点有:
5.1 . 若i>0,i位置节点的双亲序号: ( i − 1 ) / 2 (i-1)/2 (i−1)/2;i=0,i为根节点编号,无双亲节点。
若 2 i + 1 < n 2i+1<n 2i+1<n,左孩子序号: 2 i + 1 2i+1 2i+1, 2 i + 1 ≥ n 2i+1 \geq n 2i+1≥n, 否则无左孩子
若 2 i + 2 < n 2i+2<n 2i+2<n,左孩子序号: 2 i + 2 2i+2 2i+2, 2 i + 2 ≥ n 2i+2 \geq n 2i+2≥n, 否则无右孩子
3. 二叉树的实现
Queue.h
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
typedef int QDataType;
typedef struct QueueNode
{
struct QueueNode* next;
QDataType data;
}QNode;
typedef struct Queue
{
QNode* head;
QNode* tail;
int size;
}Que;
void QueueInit(Que* pq);
void QueueDestroy(Que* pq);
void QueuePush(Que* pq, QDataType x);
void QueuePop(Que* pq);
QDataType QueueFront(Que* pq);
QDataType QueueBack(Que* pq);
bool QueueEmpty(Que* pq);
int QueueSize(Que* pq);
Queue,c
#define _CRT_SECURE_NO_WARNINGS 1
#include "Queue.h"
void QueueInit(Que* pq) {
assert(pq);
pq->head = pq->tail = NULL;
pq->size = 0;
}
void QueueDestroy(Que* pq) {
assert(pq && pq->size > 0);
QNode* cur = pq->head;
while (cur)
{
QNode* next = cur->next;
free(cur);
cur = next;
}
pq->head = pq->tail = NULL;
pq->size = 0;
}
void QueuePush(Que* pq, QDataType x) {
assert(pq);
QNode* newnode = (QNode*)malloc(sizeof(QNode));
if (newnode == NULL) {
perror("malloc failed!");
exit(-1);
}
newnode->data = x;
newnode->next = NULL;
if (pq->tail == NULL)
{
pq->head = pq->tail = newnode;
}
else
{
pq->tail->next = newnode;
pq->tail = newnode;
}
++(pq->size);
}
bool QueueEmpty(Que* pq) {
assert(pq);
return pq->head == NULL;
}
int QueueSize(Que* pq) {
assert(pq);
return pq->size;
}
QDataType QueueFront(Que* pq) {
assert(pq);
assert(!QueueEmpty(pq));
return pq->head->data;
}
QDataType QueueBack(Que* pq) {
assert(pq);
assert(!QueueEmpty(pq));
return pq->tail->data;
}
void QueuePop(Que* 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;
}
pq->size--;
}
使用队列是因为在进行层序遍历时需要使用;同时如果进行前序遍历构建应更改QDataType类型
BTree.h
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef int BTDataType;
typedef struct BinaryTreeNode
{
BTDataType _data;
struct BinaryTreeNode* _left;
struct BinaryTreeNode* _right;
}BTNode;
#include "Queue.h"
// 通过前序遍历的数组"ABD##E#H##CF##G##"构建二叉树
BTNode* BinaryTreeCreate(BTDataType* a, int* pi);
// 二叉树创建
BTNode* BuyNode(BTDataType* x);
// 二叉树销毁
void BinaryTreeDestory(BTNode* root);
// 二叉树节点个数
int BinaryTreeSize(BTNode* root);
// 二叉树叶子节点个数
int BinaryTreeLeafSize(BTNode* root);
// 二叉树第k层节点个数
int BinaryTreeLevelKSize(BTNode* root, int k);
// 二叉树查找值为x的节点
BTNode* BinaryTreeFind(BTNode* root, BTDataType x);
// 二叉树前序遍历
void BinaryTreePrevOrder(BTNode* root);
// 二叉树中序遍历
void BinaryTreeInOrder(BTNode* root);
// 二叉树后序遍历
void BinaryTreePostOrder(BTNode* root);
// 层序遍历
void BinaryTreeLevelOrder(BTNode* root);
// 判断二叉树是否是完全二叉树
int BinaryTreeComplete(BTNode* root);
BTree.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "BTree.h"
BTNode* BuyNode(BTDataType* x) {
BTNode* node = (BTNode*)malloc(sizeof(BTNode));
if (node == NULL) {
perror("malloc failed!");
exit(-1);
}
node->_data = x;
node->_left = NULL;
node->_right = NULL;
return node;
}
void BinaryTreePrevOrder(BTNode* root) {
if (root == NULL) {
printf("null ");
return;
}
printf("%d ", root->_data);
BinaryTreePrevOrder(root->_left);
BinaryTreePrevOrder(root->_right);
}
void BinaryTreeInOrder(BTNode* root) {
if (root == NULL) {
printf("null ");
return;
}
BinaryTreeInOrder(root->_left);
printf("%d ", root->_data);
BinaryTreeInOrder(root->_right);
}
void BinaryTreePostOrder(BTNode* root) {
if (root == NULL) {
printf("null ");
return;
}
BinaryTreePostOrder(root->_left);
BinaryTreePostOrder(root->_right);
printf("%d ", root->_data);
}
int BinaryTreeSize(BTNode* root) {
return root == NULL ? 0 : BinaryTreeSize(root->_left) + BinaryTreeSize(root->_right) + 1;
}
int BinaryTreeLeafSize(BTNode* root) {
if (root == NULL)
return 0;
if (root->_left == NULL && root->_right == NULL)
return 1;
return BinaryTreeLeafSize(root->_left) + BinaryTreeLeafSize(root->_right);
}
int BinaryTreeLevelKSize(BTNode* root, int k) {
assert(k > 0);
if (root == NULL)
return 0;
if (k == 1)
return 1;
return BinaryTreeLevelKSize(root->_left, k - 1) + BinaryTreeLevelKSize(root->_right, k - 1);
}
BTNode* BinaryTreeFind(BTNode* root, BTDataType x) {
if (root == NULL)
return NULL;
if (root->_data == x)
return root;
BTNode* ret = NULL;
ret = BinaryTreeFind(root->_left, x);
if (ret)
return ret;
ret = BinaryTreeFind(root->_right, x);
if (ret)
return ret;
return NULL;
}
void BinaryTreeLevelOrder(BTNode* root) {
Que q;
QueueInit(&q);
if (root) {
QueuePush(&q, root);
}
while (!QueueEmpty(&q)) {
BTNode* front = QueueFront(&q);
printf("%d ", front->_data);
if (root->_left)
QueuePush(&q, root->_left);
if (root->_right)
QueuePush(&q, root->_right);
QueuePop(&q);
}
printf("\n");
QueueDestroy(&q);
}
BTNode* BinaryTreeCreate(BTDataType* a, int* pi) {
if (a[*pi] == '#') {
(*pi)++;
return NULL;
}
BTNode* root = (BTNode*)malloc(sizeof(BTNode));
if (root == NULL)
{
perror("malloc failed");
exit(-1);
}
root->_data = a[*pi];
(*pi)++;
root->_left = BinaryTreeCreate(a, pi);
root->_right = BinaryTreeCreate(a, pi);
return root;
}
int BinaryTreeComplete(BTNode* root) {
Que q;
QueueInit(&q);
if (root)
QueuePush(&q, root);
while (!QueueEmpty(&q)) {
BTNode* front = QueueFront(&q);
if (front == NULL)
break;
QueuePush(&q, front->_left);
QueuePush(&q, front->_right);
QueuePop(&q);
}
// 已经遇到空节点,如果队列中后面的节点还有非空,就不是完全二叉树
while (!QueueEmpty(&q)) {
BTNode* front = QueueFront(&q);
QueuePop(&q);
if (front != NULL) {
QueueDestroy(&q);
return false;
}
}
QueueDestroy(&q);
return true;
}
void BinaryTreeDestory(BTNode* root) {
if (root == NULL)
return;
BinaryTreeDestory(root->_left);
BinaryTreeDestory(root->_right);
free(root);
}
Test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "BTree.h"
int main()
{
// 手动构建
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;
BinaryTreePrevOrder(node1);
printf("\n");
BinaryTreeInOrder(node1);
printf("\n");
BinaryTreePostOrder(node1);
printf("\n");
int allsize = BinaryTreeSize(node1);
printf("%d\n", allsize);
int leafsize = BinaryTreeLeafSize(node1);
printf("%d\n", leafsize);
int ksize = BinaryTreeLevelKSize(node1, 2);
printf("%d\n", ksize);
int ret = BinaryTreeComplete(node1);
printf("BinaryTreeComplete: %d\n", ret);
BinaryTreeDestory(node1);
node1 = NULL;
return 0;
}
总结
注意malloc时类型要统一,否则会出现如下问题