马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
二叉树
一. 树概念及结构
1. 树的概念
树是一种非线性的数据结构,它是由n(n>=0)个有限结点构成一个具有条理关系的集合。把它叫做树是因为它看起来像一棵倒挂的树,也就是说它是根朝上,而叶朝下的。
- 有一个特别的结点,称为根结点,根结点没有前驱结点
- 除根结点外,其余结点被分成M(M>0)个互不相交的集合 T 1 、 T 2 、 … … 、 T m T_1、T_2、……、T_m T1、T2、……、Tm,此中每一个集合 T i ( 1 < = i < = m ) T_i(1<= i<=m) Ti(1<=i<=m)又是一棵结构与树类似的子树。每棵子树的根结点有且只有一个前驱,可以有0个或多个后继
- 因此,树是递归界说的。
注意:树形结构中,子树之间不能有交集,否则就不是树形结构
2. 树的相关概念
- 结点的度:一个结点含有的子树的个数称为该结点的度; 如上图: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)棵互不相交的树的集合称为森林;
3. 树的表现
树结构相对线性表就比力复杂了,要存储表现起来就比力麻烦了,既要生存值域,也要生存结点和结点之间的关系,现实中树有很多种表现方式如:双亲表现法,孩子表现法、孩子双亲表现法以及孩子兄弟表现法等。我们这里就简朴的相识此中最常用的孩子兄弟表现法。
- typedef int DataType;
- struct Node
- {
- struct Node* firstChild1; // 第一个孩子结点
- struct Node* pNextBrother; // 指向其下一个兄弟结点
- DataType data; // 结点中的数据域
- };
复制代码
4. 树在现实中的运用(表现文件系统的目录树结构)
二. 二叉树概念及结构
1. 概念
一棵二叉树是结点的一个有限集合,该集合:
- 或者为空
- 由一个根结点加上两棵别称为左子树和右子树的二叉树构成
从上图可以看出:
- 二叉树不存在度大于2的结点
- 二叉树的子树有左右之分,次序不能颠倒,因此二叉树是有序树
注意:对于任意的二叉树都是由以下几种环境复合而成的:
2. 特别的二叉树
- 满二叉树:一个二叉树,如果每一个层的结点数都到达最大值,则这个二叉树就是满二叉树。也就是说,如果一个二叉树的层数为K,且结点总数是 2 k − 1 2^k -1 2k−1 ,则它就是满二叉树。
- 完全二叉树:完全二叉树是效率很高的数据结构,完全二叉树是由满二叉树而引出来的。对于深度为K的,有n个结点的二叉树,当且仅当其每一个结点都与深度为K的满二叉树中编号从1至n的结点逐一对应时称之为完全二叉树。 要注意的是满二叉树是一种特别的完全二叉树。
3. 二叉树的性子
- 若规定根结点的层数为1,则一棵非空二叉树的第 i i i层上最多有 2 ( i − 1 ) 2^{(i-1)} 2(i−1)个结点.
- 若规定根结点的层数为1,则深度为 h h 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
- /*
- * 假设二叉树有N个结点
- * 从总结点数角度考虑:N = n0 + n1 + n2 ①
- *
- * 从边的角度考虑,N个结点的任意二叉树,总共有N-1条边
- * 因为二叉树中每个结点都有双亲,根结点没有双亲,每个节点向上与其双亲之间存在一条边
- * 因此N个结点的二叉树总共有N-1条边
- * 因为度为0的结点没有孩子,故度为0的结点不产生边; 度为1的结点只有一个孩子,故每个度
- * 为1的结点产生一条边; 度为2的结点有2个孩子,故每个度为2的结点产生两条边,所以总边
- * 为:n1+2*n2
- * 故从边的角度考虑:N-1 = n1 + 2*n2 ②
- * 结合① 和 ②得:n0 + n1 + n2 = n1 + 2*n2 - 1
- * 即:n0 = n2 + 1
- */
复制代码
- 若规定根结点的层数为1,具有n个结点的满二叉树的深度, h = l o g 2 ( n + 1 ) h= log_2(n + 1) h=log2(n+1). (ps: h = l o g 2 ( n + 1 ) h= log_2(n + 1) h=log2(n+1)是log以2为底,n+1为对数)
- 对于具有n个结点的完全二叉树,如果按照从上至下从左至右的数组顺序对所有结点从0开始编号,则对于序号为i的结点有:
- 若i>0,i位置结点的双亲序号:(i-1)/2;i=0,i为根结点编号,无双亲结点
- 若2i+1<n,左孩子序号:2i+1,2i+1>=n否则无左孩子
- 若2i+2<n,右孩子序号:2i+2,2i+2>=n否则无右孩子
- 某二叉树共有 399 个结点,此中有 199 个度为 2 的结点,则该二叉树中的叶子结点数为( )
A 不存在如许的二叉树
B 200
C 198
D 199
- 下列数据结构中,不得当采用顺序存储结构的是( )
A 非完全二叉树
B 堆
C 队列
D 栈
- 在具有 2n 个结点的完全二叉树中,叶子结点个数为( )
A n
B n+1
C n-1
D n/2
- 一棵完全二叉树的结点数位为531个,那么这棵树的高度为( )
A 11
B 10
C 8
D 12
- 一个具有767个结点的完全二叉树,其叶子结点个数为()
A 383
B 384
C 385
D 386
答案:
1.B
2.A
3.A
4.B
5.B
4. 二叉树的存储结构
二叉树一般可以使用两种结构存储,一种顺序结构,一种链式结构。
- 顺序存储
顺序结构存储就是使用数组来存储,一般使用数组只得当表现完全二叉树,因为不是完全二叉树会有空间的浪费。而现实中使用中只有堆才会使用数组来存储,关于堆我们后面的章节会专门讲授。二叉树顺序存储在物理上是一个数组,在逻辑上是一颗二叉树。
- 链式存储
二叉树的链式存储结构是指,用链表来表现一棵二叉树,即用链来指示元素的逻辑关系。 通常的方法是链表中每个结点由三个域构成,数据域和左右指针域,左右指针分别用来给出该结点左孩子和右孩子地点的链结点的存储地址 。链式结构又分为二叉链和三叉链,当前我们学习中一般都是二叉链,后面学到高阶数据结构如红黑树等会用到三叉链。
- typedef int BTDataType;
- // 二叉链
- struct BinaryTreeNode
- {
- struct BinTreeNode* left; // 指向当前结点左孩子
- struct BinTreeNode* right; // 指向当前结点右孩子
- BTDataType data; // 当前结点值域
- }
- // 三叉链
- struct BinaryTreeNode
- {
- struct BinTreeNode* parent; // 指向当前结点的双亲
- struct BinTreeNode* left; // 指向当前结点左孩子
- struct BinTreeNode* right; // 指向当前结点右孩子
- BTDataType data; // 当前结点值域
- };
复制代码 三.二叉树链式结构的实现
1. 前置说明
在学习二叉树的基本操纵前,需先要创建一棵二叉树,然后才能学习其相关的基本操纵。由于现在大家对二叉树结构把握还不够深入,为了降低大家学习成本,此处手动快速创建一棵简朴的二叉树,快速进入二叉树操纵学习,等二叉树结构相识的差不多时,我们反过头再来研究二叉树真正的创建方式。
- typedef int BTDataType;
- typedef struct BinaryTreeNode
- {
- BTDataType _data;
- struct BinaryTreeNode* _left;
- struct BinaryTreeNode* _right;
- }BTNode;
- 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;
- }
复制代码 注意:上述代码并不是创建二叉树的方式,真正创建二叉树方式后序详解重点讲授。
再看二叉树基本操纵前,再回顾下二叉树的概念,二叉树是:
- 空树
- 非空:根结点,根结点的左子树、根结点的右子树构成的。
从概念中可以看出,二叉树界说是递归式的,因此后序基本操纵中基本都是按照该概念实现的。
2. 二叉树的遍历
2.1 前序、中序以及后序遍历
学习二叉树结构,最简朴的方式就是遍历。所谓二叉树遍历(Traversal)是按照某种特定的规则,依次对二叉树中的结点进行相应的操纵,而且每个结点只操纵一次。访问结点所做的操纵依靠于具体的应用问题。 遍历是二叉树上最重要的运算之一,也是二叉树上进行其它运算的基础。
按照规则,二叉树的遍历有:前序/中序/后序的递归结构遍历:
- 前序遍历(Preorder Traversal 亦称先序遍历)——访问根结点的操纵发生在遍历其左右子树之前。
- 中序遍历(Inorder Traversal)——访问根结点的操纵发生在遍历其左右子树之中(间)。
- 后序遍历(Postorder Traversal)——访问根结点的操纵发生在遍历其左右子树之后。
由于被访问的结点必是某子树的根,以是N(Node)、L(Left subtree)和R(Right subtree)又可解释为根、根的左子树和根的右子树。NLR、LNR和LRN分别又称为先根遍历、中根遍历和后根遍历。
- // 二叉树前序遍历
- void PreOrder(BTNode* root);
- // 二叉树中序遍历
- void InOrder(BTNode* root);
- // 二叉树后序遍历
- void PostOrder(BTNode* root);
复制代码 下面重要分析前序递归遍历,中序与后序图解类似,同砚们可自己动手绘制。
前序遍历递归图解:
前序遍历结果:1 2 3 4 5 6
中序遍历结果:3 2 1 5 4 6
后序遍历结果:3 2 5 6 4 1
2.1.1 前序遍历 BinaryTreePrevOrder
- void BinaryTreePrevOrder(BTNode* root)//前序遍历
- {
- if (root == NULL)
- {
- printf("NULL ");
- return;
- }
- printf("%d ", root->data);
- BinaryTreePrevOrder(root->left);
- BinaryTreePrevOrder(root->right);
- }
复制代码 2.1.2 中序遍历 BinaryTreeInOrder
- void BinaryTreeInOrder(BTNode* root)//中序遍历
- {
- if (root == NULL)
- {
- printf("NULL ");
- return;
- }
- BinaryTreeInOrder(root->left);
- printf("%d ", root->data);
- BinaryTreeInOrder(root->right);
- }
复制代码 2.1.3 后序遍历 BinaryTreePostOrder
- void BinaryTreePostOrder(BTNode* root)//后序遍历
- {
- if (root == NULL)
- {
- printf("NULL ");
- return;
- }
- BinaryTreePostOrder(root->left);
- BinaryTreePostOrder(root->right);
- printf("%d ", root->data);
- }
复制代码 2.2 层序遍历
层序遍历:除了先序遍历、中序遍历、后序遍历外,还可以对二叉树进行层序遍历。设二叉树的根结点地点层数为1,层序遍历就是从地点二叉树的根结点出发,起首访问第一层的树根结点,然后从左到右访问第2层上的结点,接着是第三层的结点,以此类推,自上而下,自左至右逐层访问树的结点的过程就是层序遍历。
- // 层序遍历
- void LevelOrder(BTNode* root);
复制代码 2.2.1 层序遍历 BinaryTreeLevelOrder
- typedef struct BinaryTreeNode* QDataType;
复制代码- #include "Queue.h"
- void BinaryTreeLevelOrder(BTNode* root)
- {
- Queue Q;
- QueueInit(&Q);
- if (root)
- QueuePush(&Q, root);
-
- while (!QueueEmpty(&Q))
- {
- BTNode* front = QueueFront(&Q);
- QueuePop(&Q);
- printf("%d ", front->data);
- if (front->left)
- QueuePush(&Q, front->left);
- if (front->right)
- QueuePush(&Q, front->right);
- }
- }
复制代码 练习:请写出下面的前序/中序/后序/层序遍历
选择题
- 某完全二叉树按条理输出(同一层从左到右)的序列为 ABCDEFGH 。该完全二叉树的前序序列为( )
A ABDHECFG
B ABCDEFGH
C HDBEAFCG
D HDEBFGCA
- 二叉树的先序遍历和中序遍历如下:先序遍历:EFHIGJK;中序遍历:HFIEJKG.则二叉树根结点为()
A E
B F
C G
D H
- 设一课二叉树的中序遍历序列:badce,后序遍历序列:bdeca,则二叉树前序遍历序列为____。
A adbce
B decab
C debac
D abcde
- 某二叉树的后序遍历序列与中序遍历序列雷同,均为 ABCDEF ,则按条理输出(同一层从左到右)的序列为
A FEDCBA
B CBAFED
C DEFCBA
D ABCDEF
选择题答案
3. 结点个数以及高度等
- // 二叉树结点个数
- int BinaryTreeSize(BTNode* root);
- // 二叉树叶子结点个数
- int BinaryTreeLeafSize(BTNode* root);
- // 二叉树第k层结点个数
- int BinaryTreeLevelKSize(BTNode* root, int k);
- // 二叉树查找值为x的结点
- BTNode* BinaryTreeFind(BTNode* root, BTDataType x);
复制代码 3.1 二叉树结点个数 BinaryTreeSize
- int BinaryTreeSize(BTNode* root)//节点个数
- {
- return root == NULL ? 0 : \
- 1 + BinaryTreeSize(root->left) + BinaryTreeSize(root->right);
- }
复制代码 3.2 二叉树叶子结点个数 BinaryTreeLeafSize
- 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);
- }
复制代码 3.3 二叉树第k层结点个数 BinaryTreeLevelKSize
- int BinaryTreeLevelKSize(BTNode* root, int k)
- {
- if (root == NULL)
- return 0;
- //if (k == 1 && root != NULL)
- if (k == 1)
- return 1;
- //if (k > 1 && root != NULL)
- return BinaryTreeLevelKSize(root->left, k - 1) + BinaryTreeLevelKSize(root->right, k - 1);
- }
复制代码 3.4 二叉树查找值为x的结点 BinaryTreeFind
- BTNode* BinaryTreeFind(BTNode* root, BTDataType x)
- {
- if(root == NULL)
- return NULL;
- if (root->data == x)
- return root;
- BTNode* left = BinaryTreeFind(root->left, x);
- if (left)
- return left;
- BTNode* right = BinaryTreeFind(root->right, x);
- if (right)
- return right;
- return NULL;
- }
复制代码 4. 二叉树基础oj练习
4.1 单值二叉树
单值二叉树。Oj链接
- /**
- * Definition for a binary tree node.
- * struct TreeNode {
- * int val;
- * struct TreeNode *left;
- * struct TreeNode *right;
- * };
- */
- bool isUnivalTree(struct TreeNode* root)
- {
- if(root == NULL)
- return true;
- if(root->left && root->left->val != root->val)
- return false;
-
- if(root->right && root->right->val != root->val)
- return false;
- return isUnivalTree(root->left) && isUnivalTree(root->right);
- }
复制代码 4.2 检查两颗树是否雷同
检查两颗树是否雷同。OJ链接
- /**
- * Definition for a binary tree node.
- * struct TreeNode {
- * int val;
- * struct TreeNode *left;
- * struct TreeNode *right;
- * };
- */
- bool isSameTree(struct TreeNode* p, struct TreeNode* q)
- {
- if(p == NULL && q == NULL)//都为空
- return true;
- if(p == NULL || q == NULL)//其中一个不为空
- return false;
- if(p->val != q->val)
- return false;
- return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
- }
复制代码 4.3 对称二叉树
对称二叉树。OJ链接
- /**
- * Definition for a binary tree node.
- * struct TreeNode {
- * int val;
- * struct TreeNode *left;
- * struct TreeNode *right;
- * };
- */
- bool _isSymmetric(struct TreeNode* p, struct TreeNode* q)
- {
- if(p == NULL && q == NULL)
- return true;
- if(p == NULL || q == NULL)
- return false;
- if(p->val != q->val)
- return false;
- return _isSymmetric(p->left, q->right) && _isSymmetric(p->right, q->left);
- }
- bool isSymmetric(struct TreeNode* root)
- {
- return _isSymmetric(root->left, root->right);
- }
复制代码 4.4 二叉树的前序遍历
二叉树的前序遍历。 OJ链接
- /**
- * Definition for a binary tree node.
- * struct TreeNode {
- * int val;
- * struct TreeNode *left;
- * struct TreeNode *right;
- * };
- */
- /**
- * Note: The returned array must be malloced, assume caller calls free().
- */
- int BinaryTreeSize(struct TreeNode* root)//节点个数
- {
- return root == NULL ? 0 : 1 + BinaryTreeSize(root->left) + BinaryTreeSize(root->right);
- }
- void BinaryTreePrevOrder(struct TreeNode* root, int* arr, int* num)//前序遍历
- {
- if (root == NULL)
- return;
- arr[(*num)++] = root->val;
- BinaryTreePrevOrder(root->left, arr, num);
- BinaryTreePrevOrder(root->right, arr, num);
- }
- int* preorderTraversal(struct TreeNode* root, int* returnSize)
- {
- *returnSize = BinaryTreeSize(root);
- int* arr = malloc(sizeof(int) * (*returnSize));
- int num = 0;
- BinaryTreePrevOrder(root, arr, &num);
- return arr;
- }
复制代码 4.5 二叉树中序遍历
二叉树中序遍历 。OJ链接
- /**
- * Definition for a binary tree node.
- * struct TreeNode {
- * int val;
- * struct TreeNode *left;
- * struct TreeNode *right;
- * };
- */
- /**
- * Note: The returned array must be malloced, assume caller calls free().
- */
- int BinaryTreeSize(struct TreeNode* root)//节点个数
- {
- return root == NULL ? 0 : 1 + BinaryTreeSize(root->left) + BinaryTreeSize(root->right);
- }
- void BinaryTreeInOrder(struct TreeNode* root, int* arr, int* num)//前序遍历
- {
- if (root == NULL)
- return;
-
- BinaryTreeInOrder(root->left, arr, num);
- arr[(*num)++] = root->val;
- BinaryTreeInOrder(root->right, arr, num);
- }
- int* inorderTraversal(struct TreeNode* root, int* returnSize)
- {
- *returnSize = BinaryTreeSize(root);
- int* arr = malloc(sizeof(int) * (*returnSize));
- int num = 0;
- BinaryTreeInOrder(root, arr, &num);
- return arr;
- }
复制代码 4.6 二叉树的后序遍历
二叉树的后序遍历 。OJ链接
- /**
- * Definition for a binary tree node.
- * struct TreeNode {
- * int val;
- * struct TreeNode *left;
- * struct TreeNode *right;
- * };
- */
- /**
- * Note: The returned array must be malloced, assume caller calls free().
- */
- int BinaryTreeSize(struct TreeNode* root)//节点个数
- {
- return root == NULL ? 0 : 1 + BinaryTreeSize(root->left) + BinaryTreeSize(root->right);
- }
- void BinaryTreePostOrder(struct TreeNode* root, int* arr, int* num)//前序遍历
- {
- if (root == NULL)
- return;
-
- BinaryTreePostOrder(root->left, arr, num);
- BinaryTreePostOrder(root->right, arr, num);
- arr[(*num)++] = root->val;
- }
- int* postorderTraversal(struct TreeNode* root, int* returnSize)
- {
- *returnSize = BinaryTreeSize(root);
- int* arr = malloc(sizeof(int) * (*returnSize));
- int num = 0;
- BinaryTreePostOrder(root, arr, &num);
- return arr;
- }
复制代码 4.7 另一颗树的子树
另一颗树的子树。OJ链接
- /**
- * Definition for a binary tree node.
- * struct TreeNode {
- * int val;
- * struct TreeNode *left;
- * struct TreeNode *right;
- * };
- */
- bool isSameTree(struct TreeNode* p, struct TreeNode* q)
- {
- if(p == NULL && q == NULL)//都为空
- return true;
- if(p == NULL || q == NULL)//其中一个不为空
- return false;
- if(p->val != q->val)
- return false;
- return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
- }
- bool isSubtree(struct TreeNode* root, struct TreeNode* subRoot){ if(root == NULL) return false; if(root->val == subRoot->val && isSameTree(root, subRoot)) return true; return isSubtree(root->left, subRoot) || isSubtree(root->right, subRoot);}
复制代码 5 二叉树的创建和销毁
- // 通过前序遍历的数组"ABD##E#H##CF##G##"构建二叉树
- BTNode* BinaryTreeCreate(BTDataType* a, int n, int* pi);
- // 二叉树销毁
- void BinaryTreeDestory(BTNode** root);
- // 判断二叉树是否是完全二叉树
- int BinaryTreeComplete(BTNode* root);
复制代码 1.二叉树的构建及遍历
二叉树的构建及遍历OJ链接
- #include <stdio.h>
- #include <stdlib.h>
- typedef char BTDataType;
- typedef struct BinaryTreeNode
- {
- BTDataType data;
- struct BinaryTreeNode* left;
- struct BinaryTreeNode* right;
- }BTNode;
- BTNode* CreatTree(char* arr, int* num)
- {
- if(arr[*num] == '#')
- {
- (*num)++;
- return NULL;
- }
- BTNode* root = malloc(sizeof(BTNode));
- if(root == NULL)
- {
- perror("malloc failed");
- return NULL;
- }
- root->data = arr[(*num)++];
- root->left = CreatTree(arr, num);
- root->right = CreatTree(arr, num);
- return root;
- }
- void BinaryTreeInOrder(BTNode* root)//中序遍历
- {
- if (root == NULL)
- {
- return;
- }
- BinaryTreeInOrder(root->left);
- printf("%c ", root->data);
- BinaryTreeInOrder(root->right);
- }
- int main()
- {
- char arr[100] = { 0 };
- scanf("%s", arr);
- int num = 0;
- BTNode* root = CreatTree(arr, &num);
- BinaryTreeInOrder(root);
- return 0;
- }
复制代码 2. 前序遍历构建二叉树 BinaryTreeCreate
通过前序遍历的数组"ABD##E#H##CF##G##"构建二叉树
- BTNode* BinaryTreeCreate(BTDataType* arr, int num, int* pi)
- {
- if (*pi >= num)
- {
- return NULL;
- }
- if (arr[*pi] == '#')
- {
- (*pi)++;
- return NULL;
- }
- BTNode* root = malloc(sizeof(BTNode));
- if (root == NULL)
- {
- perror("malloc failed");
- return NULL;
- }
- root->data = arr[(*pi)++];
- root->left = BinaryTreeCreate(arr, num, pi);
- root->right = BinaryTreeCreate(arr, num, pi);
- return root;
- }
复制代码 3. 二叉树销毁 BinaryTreeDestory
- void BinaryTreeDestory(BTNode** root)
- {
- if (*root == NULL)
- return;
- BinaryTreeDestory(&((*root)->left));
- BinaryTreeDestory(&((*root)->right));
- free(*root);
- *root = NULL;
- }
复制代码 4. 判断二叉树是否是完全二叉树 BinaryTreeComplete
- typedef struct BinaryTreeNode* QDataType;
复制代码- #include "Queue.h"
- bool BinaryTreeComplete(BTNode* root)
- {
- Queue Q;
- QueueInit(&Q);
- if (root)
- QueuePush(&Q, root);
- while (!QueueEmpty(&Q))
- {
- BTNode* front = QueueFront(&Q);
- QueuePop(&Q);
- //遇到第一个空,开始判断
- if (front == NULL)
- {
- break;
- }
- QueuePush(&Q, front->left);
- QueuePush(&Q, front->right);
- }
- while (!QueueEmpty(&Q))
- {
- BTNode* front = QueueFront(&Q);
- QueuePop(&Q);
- //如果有非空,就不是完全二叉树
- if (front)
- {
- QueueDestroy(&Q);
- return false;
- }
- }
- QueueDestroy(&Q);
- return true;
- }
复制代码 四. 参考代码
BinaryTree.h
- #pragma once
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- typedef int BTDataType;
- typedef struct BinaryTreeNode
- {
- BTDataType data;
- struct BinaryTreeNode* left;
- struct BinaryTreeNode* right;
- }BTNode;
- // 通过前序遍历的数组"ABD##E#H##CF##G##"构建二叉树
- BTNode* BinaryTreeCreate(BTDataType* arr, int num, int* pi);
- // 二叉树销毁
- 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);
- // 判断二叉树是否是完全二叉树
- bool BinaryTreeComplete(BTNode* root);
复制代码 BinaryTree.c
- #include "BinaryTree.h"BTNode* BinaryTreeCreate(BTDataType* arr, int num, int* pi)
- {
- if (*pi >= num)
- {
- return NULL;
- }
- if (arr[*pi] == '#')
- {
- (*pi)++;
- return NULL;
- }
- BTNode* root = malloc(sizeof(BTNode));
- if (root == NULL)
- {
- perror("malloc failed");
- return NULL;
- }
- root->data = arr[(*pi)++];
- root->left = BinaryTreeCreate(arr, num, pi);
- root->right = BinaryTreeCreate(arr, num, pi);
- return root;
- }
- void BinaryTreeDestory(BTNode** root)
- {
- if (*root == NULL)
- return;
- BinaryTreeDestory(&((*root)->left));
- BinaryTreeDestory(&((*root)->right));
- free(*root);
- *root = NULL;
- }
- int BinaryTreeSize(BTNode* root)//节点个数
- {
- return root == NULL ? 0 : \
- 1 + BinaryTreeSize(root->left) + BinaryTreeSize(root->right);
- }
- 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)
- {
- if (root == NULL)
- return 0;
- //if (k == 1 && root != NULL)
- if (k == 1)
- return 1;
- //if (k > 1 && root != NULL)
- 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* left = BinaryTreeFind(root->left, x);
- if (left)
- return left;
- BTNode* right = BinaryTreeFind(root->right, x);
- if (right)
- return right;
- return NULL;
- }
- 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);
- }
- #include "Queue.h"
- void BinaryTreeLevelOrder(BTNode* root)
- {
- Queue Q;
- QueueInit(&Q);
- if (root)
- QueuePush(&Q, root);
-
- while (!QueueEmpty(&Q))
- {
- BTNode* front = QueueFront(&Q);
- QueuePop(&Q);
- printf("%d ", front->data);
- if (front->left)
- QueuePush(&Q, front->left);
- if (front->right)
- QueuePush(&Q, front->right);
- }
- }
- bool BinaryTreeComplete(BTNode* root){ Queue Q; QueueInit(&Q); if (root) QueuePush(&Q, root); while (!QueueEmpty(&Q)) { BTNode* front = QueueFront(&Q); QueuePop(&Q); //遇到第一个空,开始判断 if (front == NULL) { break; } QueuePush(&Q, front->left); QueuePush(&Q, front->right); } while (!QueueEmpty(&Q)) { BTNode* front = QueueFront(&Q); QueuePop(&Q); //如果有非空,就不是完全二叉树 if (front) { QueueDestroy(&Q); return false; } } QueueDestroy(&Q); return true;}
复制代码 Queue.h
- #pragma once#include <stdio.h>#include <stdlib.h>#include <assert.h>#include <stdbool.h>typedef struct BinaryTreeNode* QDataType;
- typedef struct QueueNode{ struct QueueNode* next; QDataType val;}QNode;typedef struct Queue{ QNode* phead; QNode* ptail; int size;}Queue;队尾插入//void QueuePush(QNode** pphead, QNode** pptail, QDataType x);//队头删除//void QueuePop(QNode** pphead, QNode** pptail);//初始化void QueueInit(Queue* pq);//队尾插入void QueuePush(Queue* pq, QDataType x);//队头删除void QueuePop(Queue* pq);//取队头数据QDataType QueueFront(Queue* pq);//取队尾数据QDataType QueueBack(Queue* pq);//队列元素个数int QueueSize(Queue* pq);//判空_Bool QueueEmpty(Queue* pq);//队列的销毁void QueueDestroy(Queue* pq);
复制代码 Queue.c
- #include "Queue.h"
- void QueueInit(Queue* pq)
- {
- assert(pq);
- pq->phead = NULL;
- pq->ptail = NULL;
- pq->size = 0;
- }
- void QueuePush(Queue* pq, QDataType x)
- {
- QNode* newnode = (QNode*)malloc(sizeof(QNode));
- if (newnode == NULL)
- {
- perror("malloc failed");
- exit(1);
- }
- newnode->next = NULL;
- newnode->val = x;
- if (pq->ptail == NULL)
- {
- pq->phead = newnode;
- pq->ptail = newnode;
- }
- else
- {
- pq->ptail->next = newnode;
- pq->ptail = newnode;
- }
- pq->size++;
- }
- void QueuePop(Queue* pq)
- {
- assert(pq);
- assert(pq->size);
- QNode* next = pq->phead->next;
- free(pq->phead);
- pq->phead = next;
- if (pq->phead == NULL)//只有一个节点的情况
- {
- pq->ptail = NULL;
- }
- 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;
- }
- int QueueSize(Queue* pq)
- {
- assert(pq);
- return pq->size;
- }
- _Bool QueueEmpty(Queue* pq)
- {
- assert(pq);
- return pq->size == 0;
- }
- void QueueDestroy(Queue* pq)
- {
- assert(pq);
- QNode* pcur = pq->phead;
- while (pcur)
- {
- QNode* next = pcur->next;
- free(pcur);
- pcur = next;
- }
- pq->phead = NULL;
- pq->ptail = NULL;
- pq->size = 0;
- }
复制代码 test.c
- #define _CRT_SECURE_NO_WARNINGS 1
- //#include "vld.h"
- #include "BinaryTree.h"
- BTNode* BuyNode(BTDataType x)
- {
- BTNode* node = (BTNode*)malloc(sizeof(BTNode));
- if (node == NULL)
- {
- perror("malloc failed");
- return NULL;
- }
- node->data = 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;
- }
- //int TreeHeight(BTNode* root)
- //{
- // if (root == NULL)
- // return 0;
- //
- // return TreeHeight(root->left) > TreeHeight(root->right) ? \
- // TreeHeight(root->left) + 1 : TreeHeight(root->right) + 1;
- //}
- //求树的高度
- int TreeHeight(BTNode* root)
- {
- if (root == NULL)
- return 0;
- int leftHeight = TreeHeight(root->left);
- int rightHeight = TreeHeight(root->right);
- return leftHeight > rightHeight ? \
- leftHeight + 1 : rightHeight + 1;
- //return fmax(TreeHeight(root->left), TreeHeight(root->right));
- }
- void BTTest01()
- {
- BTNode* root = CreatBinaryTree();
- BinaryTreePrevOrder(root);
- printf("\n");
- BinaryTreeInOrder(root);
- printf("\n");
- BinaryTreePostOrder(root);
- printf("\n");
- printf("BinaryTreeSize:%d\n", BinaryTreeSize(root));
- printf("BinaryTreeSize:%d\n", BinaryTreeSize(root));
- printf("BinaryTreeLeafSize:%d\n", BinaryTreeLeafSize(root));
- printf("TreeHeight:%d\n", TreeHeight(root));
- printf("BinaryTreeLevelKSize(1):%d\n", BinaryTreeLevelKSize(root, 1));
- printf("BinaryTreeLevelKSize(2):%d\n", BinaryTreeLevelKSize(root, 2));
- printf("BinaryTreeLevelKSize(3):%d\n", BinaryTreeLevelKSize(root, 3));
- printf("BinaryTreeLevelKSize(4):%d\n", BinaryTreeLevelKSize(root, 4));
- printf("BinaryTreeFind(1):%d\n", BinaryTreeFind(root, 1)->data);
- printf("BinaryTreeFind(2):%d\n", BinaryTreeFind(root, 2)->data);
- printf("BinaryTreeFind(3):%d\n", BinaryTreeFind(root, 3)->data);
- printf("BinaryTreeFind(4):%d\n", BinaryTreeFind(root, 4)->data);
- printf("BinaryTreeFind(5):%d\n", BinaryTreeFind(root, 5)->data);
- printf("BinaryTreeFind(6):%d\n", BinaryTreeFind(root, 6)->data);
- printf("BinaryTreeFind(7):%p\n", BinaryTreeFind(root, 7));
- }
- void BTTest02()
- {
- char arr[100] = { 0 };
- scanf("%s", arr);
- size_t sz = sizeof(arr) / sizeof(arr[0]);
- int i = 0;
- BTNode* root = BinaryTreeCreate(arr, sz, &i);
- BinaryTreeInOrder(root);
- printf("\n");
- BinaryTreeDestory(&root);
- printf("%p\n", root);
- }
- BTNode* CreatBinaryTree02()//手动创建树
- {
- 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;
- node2->right = node5;
- node4->left = node6;
- return node1;
- }
- void BTTest03()
- {
- BTNode* root = CreatBinaryTree02();
- BinaryTreeLevelOrder(root);
- printf("\n");
- printf("BinaryTreeComplete:%d\n", BinaryTreeComplete(root));
- BinaryTreeDestory(&root);
- }
- int main()
- {
- BTTest03();
- return 0;
- }
复制代码 |