0


C语言实现二叉树(初阶数据结构)

树的概念及结构

树的概念

树是一种非线性的数据结构,它是由n(n>=0)个有限结点组成一个具有层次关系的集合。把它叫做树是因为它看起来像一棵倒挂的树,也就是说它是根朝上,而叶朝下的。
有一个特殊的结点,称为根结点,根节点没有前驱结点。
除根节点外,其余结点被分成M(M>0)个互不相交的集合T1、T2、……、Tm,其中每一个集合Ti(1<= i<= m)又是一棵结构与树类似的子树。每棵子树的根结点有且只有一个前驱,可以有0个或多个后继因此,树是递归定义的。

  1. ![](https://img-blog.csdnimg.cn/eb44b8c476e44bda8339fbeb5c8411f5.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5pq06LWw55qE5qmZ5a2Q772e,size_11,color_FFFFFF,t_70,g_se,x_16) ​​​​

注意:树形结构中,子树之间不能有交集,否则就不是树形结构。

  1. ![](https://img-blog.csdnimg.cn/38944218aa1648c2bfcb164b7e450ab2.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5pq06LWw55qE5qmZ5a2Q772e,size_11,color_FFFFFF,t_70,g_se,x_16)

树的相关概念

  1. ![](https://img-blog.csdnimg.cn/581479c447c4477d9abe0b0dd995865b.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5pq06LWw55qE5qmZ5a2Q772e,size_12,color_FFFFFF,t_70,g_se,x_16)

节点的度:一个节点含有的子树的个数称为该节点的度; 如上图: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. 或者为空
  2. 由一个根节点加上两棵别称为左子树和右子树的二叉树组成

从上图可以看出:

  1. 二叉树不存在度大于2的结点
  2. 二叉树的子树有左右之分,次序不能颠倒,因此二叉树是有序树

注意:对于任意的二叉树都是由以下几种情况复合而成的:

现实中的二叉树

特殊的二叉树

  1. 满二叉树:一个二叉树,如果每一个层的结点数都达到最大值,则这个二叉树就是满二叉 树。也就是说,如果一个二叉树的层数为K,且结点总数是 ,则它就是满二叉树。
  2. 完全二叉树:完全二叉树是效率很高的数据结构,完全二叉树是由满二叉树而引出来的。 对于深度为K的,有n个结点的二叉树,当且仅当其每一个结点都与深度为K的满二叉树中 编号从1至n的结点一一对应时称之为完全二叉树。 要注意的是满二叉树是一种特殊的完 全二叉树。

具体图形展示:

二叉树的性质

  1. 若规定根节点的层数为1,则一棵非空二叉树的第i层上最多有 2^(i-1)个结点.
  2. 若规定根节点的层数为1,则深度为h的二叉树的最大结点数是 2^h-1.
  3. 对任何一棵二叉树, 如果度为0其叶结点个数为n0 , 度为2的分支结点个数为n2 , 则有n0=n2+1
  4. 若规定根节点的层数为1,具有n个结点的满二叉树的深度,h= log2(n+1)(解释:是log以2
    为底,n+1为对数)
  5. 对于具有n个结点的完全二叉树,如果按照从上至下从左至右的数组顺序对所有节点从0开 始编号,则对于序号为i的结点有:
    1. 若i>0,i位置节点的双亲序号:(i-1)/2;i=0,i为根节点编号,无双亲节点
    2. 若2i+1<n,左孩子序号:2i+1,2i+1>=n否则无左孩子
    3. 若2i+2<n,右孩子序号:2i+2,2i+2>=n否则无右孩子

有了以上的概念,接下来我们来用代码简单实现一下二叉树吧!

结构体初始化

  1. typedef char BTDataType;
  2. typedef struct BinaryTreeNode
  3. {
  4. struct BinaryTreeNode* left;
  5. struct BinaryTreeNode* right;
  6. BTDataType data;
  7. }BTNode;

函数接口

BTNode* BuyNode(BTDataType x)

  1. BTNode* BuyNode(BTDataType x)
  2. {
  3. BTNode* newnode = (BTNode*)malloc(sizeof(BTNode));
  4. if (newnode == NULL)
  5. {
  6. printf("malloc fail\n");
  7. exit(-1);
  8. }
  9. newnode->left = NULL;
  10. newnode->right = NULL;
  11. newnode->data = x;
  12. return newnode;
  13. }

//创建树结点

BTNode* BuyNode(BTDataType x) //返回值为BTNode,返回开辟好的树节点的地址
{
BTNode
newnode = (BTNode*)malloc(sizeof(BTNode));//malloc一个结构体
if (newnode == NULL)
{
printf("malloc fail\n");
exit(-1);
}
newnode->left = NULL; //左子树置空
newnode->right = NULL;//右子树置空
newnode->data = x;//节点2上存数据
return newnode;//返回节点的地址
}

BTNode* CreatBinaryTree()

  1. BTNode* CreatBinaryTree()
  2. {
  3. BTNode* nodeA = BuyNode('A');
  4. BTNode* nodeB = BuyNode('B');
  5. BTNode* nodeC = BuyNode('C');
  6. BTNode* nodeD = BuyNode('D');
  7. BTNode* nodeE = BuyNode('E');
  8. BTNode* nodeF = BuyNode('F');
  9. BTNode* nodeG = BuyNode('G');
  10. nodeA->left = nodeB;
  11. nodeA->right = nodeC;
  12. nodeB->left = nodeD;
  13. nodeC->left = nodeE;
  14. nodeC->right = nodeF;
  15. nodeF->left = nodeG;
  16. return nodeA;
  17. }

//创建一个树,以下面创建的树为例,并作为测试用例

BTNode* CreatBinaryTree()
{
BTNode* nodeA = BuyNode('A');
BTNode* nodeB = BuyNode('B');
BTNode* nodeC = BuyNode('C');
BTNode* nodeD = BuyNode('D');
BTNode* nodeE = BuyNode('E');
BTNode* nodeF = BuyNode('F');
BTNode* nodeG = BuyNode('G');
nodeA->left = nodeB;
nodeA->right = nodeC;
nodeB->left = nodeD;
nodeC->left = nodeE;
nodeC->right = nodeF;
nodeF->left = nodeG;
return nodeA;
}

经过上面树的构建之后,二叉树的示意图:

  1. ![](https://img-blog.csdnimg.cn/e8d9c3c3896d4367961e7520a1635ebc.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5pq06LWw55qE5qmZ5a2Q772e,size_12,color_FFFFFF,t_70,g_se,x_16)

void PrevOrder(BTNode* root)

  1. void PrevOrder(BTNode* root)
  2. {
  3. if (root == NULL) //这里不用assert,否则遇到空时,就过不去了
  4. {
  5. printf("NULL ");
  6. return;
  7. }
  8. printf("%c ", root->data);
  9. PrevOrder(root->left);
  10. PrevOrder(root->right);
  11. }

前序遍历(Preorder Traversal 亦称先序遍历)——访问根结点的操作发生在遍历其左右子树之前

//前序遍历二叉树

void PrevOrder(BTNode* root)
{
if (root == NULL) //这里不用assert,否则遇到空时,就过不去了
{
printf("NULL ");
return;
}
printf("%c ", root->data);//打印根节点的数据
PrevOrder(root->left); //递归左子树
PrevOrder(root->right);//递归右子树
}

部分递归展开图:

void InOrder(BTNode* root)

  1. void InOrder(BTNode* root)
  2. {
  3. if (root == NULL)
  4. {
  5. printf("NULL ");
  6. return;
  7. }
  8. InOrder(root->left);
  9. printf("%c ", root->data);
  10. InOrder(root->right);
  11. }

中序遍历(Inorder Traversal)——访问根结点的操作发生在遍历其左右子树之中(间)

//中序遍历二叉树

void InOrder(BTNode* root)
{
if (root == NULL)
{
printf("NULL ");
return;
}
InOrder(root->left);//遍历左子树
printf("%c ", root->data);//打印根节点的数据
InOrder(root->right);//遍历右子树
}

部分递归展开图:

  1. ![](https://img-blog.csdnimg.cn/305ca27a88744740bdd3f3ceb7d10c18.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5pq06LWw55qE5qmZ5a2Q772e,size_16,color_FFFFFF,t_70,g_se,x_16)

void PostOrder(BTNode* root)

  1. void PostOrder(BTNode* root)
  2. {
  3. if (root == NULL)
  4. {
  5. printf("NULL ");
  6. return;
  7. }
  8. PostOrder(root->left);
  9. PostOrder(root->right);
  10. printf("%c ", root->data);
  11. }

后序遍历(Postorder Traversal)——访问根结点的操作发生在遍历其左右子树之后

//后序遍历二叉树

void PostOrder(BTNode* root)
{
if (root == NULL)
{
printf("NULL ");
return;
}
PostOrder(root->left);//遍历左子树
PostOrder(root->right);//遍历右子树
printf("%c ", root->data);//打印根节点
}

部分递归展开图:

  1. ![](https://img-blog.csdnimg.cn/85ce8201006a4a729107681e2e40463a.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5pq06LWw55qE5qmZ5a2Q772e,size_16,color_FFFFFF,t_70,g_se,x_16)

int BinaryTreeSize(BTNode* root)

  1. int BinaryTreeSize(BTNode* root)
  2. {
  3. //if (root == NULL)
  4. //{
  5. // return 0;
  6. //}
  7. //return BinaryTreeSize(root->left) + BinaryTreeSize(root->right) + 1;
  8. return root == NULL ? 0 : BinaryTreeSize(root->left) + BinaryTreeSize(root->right) + 1;
  9. }

//计算二叉树节点个数

int BinaryTreeSize(BTNode* root)
{
//if (root == NULL)
//{
// return 0;
//}
//return BinaryTreeSize(root->left) + BinaryTreeSize(root->right) + 1;
return root == NULL ? 0 : BinaryTreeSize(root->left) + BinaryTreeSize(root->right) + 1;
}//如果root为空,则返回0,如果不为空,递归左子树和右子树并且+1,这个1就相当于加上了当前结点的个数。

部分递归展开图:

int BinaryTreeLeafSize(BTNode* root)

  1. int BinaryTreeLeafSize(BTNode* root)
  2. {
  3. if (root == NULL) //这个条件不能忘
  4. {
  5. return 0;
  6. }
  7. if (root->left == NULL && root->right == NULL)
  8. {
  9. return 1;
  10. }
  11. return BinaryTreeLeafSize(root->left) + BinaryTreeLeafSize(root->right);
  12. }

//计算二叉树的叶子节点

int BinaryTreeLeafSize(BTNode* root)
{
if (root == NULL) //这个条件不能忘,在这里根节点为空,说明就没有节点,就返回0
{
return 0;
}
if (root->left == NULL && root->right == NULL) //一个节点的左子树和右子树都为空,则 该节点为叶子结点
{
return 1;
}
return BinaryTreeLeafSize(root->left) + BinaryTreeLeafSize(root->right);//递归左子树和 右子树得到返回值
}

int BinaryTreeLevelKSize(BTNode* root, int k)

  1. //二叉树第K层结点个数
  2. int BinaryTreeLevelKSize(BTNode* root, int k)
  3. {
  4. assert(k >= 1);
  5. if (root == NULL)
  6. {
  7. return 0;
  8. }
  9. if (k == 1)
  10. {
  11. return 1;
  12. }
  13. int left = BinaryTreeLevelKSize(root->left, k - 1);
  14. int right = BinaryTreeLevelKSize(root->right, k - 1);
  15. return left + right;
  16. //return BinaryTreeLevelKSize(root->left, k - 1) + BinaryTreeLevelKSize(root->right, k - 1);
  17. }

//二叉树第K层结点个数
int BinaryTreeLevelKSize(BTNode* root, int k)
{
assert(k >= 1);//判断第K层是否具有合法性,不能缺少
if (root == NULL) //当前递归到的结点为空时,返回0,这个条件必须放到下面判断条件前面
{
return 0;
}
if (k == 1) //当递归到K==1时,则当前结点是第K层的结点,则返回1
{
return 1;
}
int left = BinaryTreeLevelKSize(root->left, k - 1);//遍历左子树,每遍历一层,k-1
int right = BinaryTreeLevelKSize(root->right, k - 1);//遍历右子树,每遍历一层,k-1
return left + right;
//return BinaryTreeLevelKSize(root->left, k - 1) + BinaryTreeLevelKSize(root->right, k - 1);
}

**递归展开图: **

int BinaryTreeDepth(BTNode* root)

  1. //二叉树的深度/高度
  2. int BinaryTreeDepth(BTNode* root)
  3. {
  4. if (root == NULL)
  5. {
  6. return 0;
  7. }
  8. int leftDepth = BinaryTreeDepth(root->left);
  9. int rightDepth = BinaryTreeDepth(root->right);
  10. return leftDepth > rightDepth ? leftDepth + 1 : rightDepth + 1;
  11. }

//二叉树的深度/高度
int BinaryTreeDepth(BTNode* root)
{
if (root == NULL)
{
return 0;
}
int leftDepth = BinaryTreeDepth(root->left);//记录左子树的高度
int rightDepth = BinaryTreeDepth(root->right);//记录右子树的高度
return leftDepth > rightDepth ? leftDepth + 1 : rightDepth + 1;//左子树和右子树大的那一 个加1
}

部分递归展开图:

  1. ![](https://img-blog.csdnimg.cn/ef986ac6bc524c359be929ba8e07686e.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5pq06LWw55qE5qmZ5a2Q772e,size_15,color_FFFFFF,t_70,g_se,x_16)

BTNode* BinaryTreeFind(BTNode* root, BTDataType x)

  1. BTNode* BinaryTreeFind(BTNode* root, BTDataType x)
  2. {
  3. if (root == NULL)
  4. {
  5. return NULL;
  6. }
  7. if (root->data == x)
  8. {
  9. return root;
  10. }
  11. BinaryTreeFind(root->left, x);
  12. BinaryTreeFind(root->right, x);
  13. return NULL;
  14. }

//查找某一个节点,并返回该节点的地址

BTNode* BinaryTreeFind(BTNode* root, BTDataType x)
{
if (root == NULL) //如果节点为空,则返回空,表示没有找到
{
return NULL;
}
if (root->data == x)//找到节点时
{
return root; //返回结点的地址
}
BinaryTreeFind(root->left, x);//递归遍历左子树
BinaryTreeFind(root->right, x);//递归遍历右子树
return NULL;
}

void BinaryTreeLevelOrder(BTNode* root)

  1. void BinaryTreeLevelOrder(BTNode* root)
  2. {
  3. if (root == NULL)
  4. {
  5. return;
  6. }
  7. Queue q;
  8. QueueInit(&q);
  9. QueuePush(&q, root);
  10. while (!QueueEmpty(&q))
  11. {
  12. BTNode* front = QueueHead(&q);
  13. QueuePop(&q);//Pop掉的是存放树节点的地址,而不是把树节点Pop掉了
  14. printf("%c ", front->data);
  15. if (front->left)
  16. {
  17. QueuePush(&q, front->left);
  18. }
  19. if (front->right)
  20. {
  21. QueuePush(&q, front->right);
  22. }
  23. }
  24. printf("\n");
  25. QueueDestroy(&q);
  26. }

void BinaryTreeLevelOrder(BTNode* root)
{
if (root == NULL)//root为空时,说明二叉树第一个结点为空,则二叉树为空,不用遍历
{
return;
}
Queue q;//利用队列的性质来遍历二叉树
QueueInit(&q);
QueuePush(&q, root);//首先,把二叉树头结点放入队列中


while (!QueueEmpty(&q))
{
BTNode* front = QueueHead(&q);
QueuePop(&q);//Pop掉的是存放树节点的地址,而不是把树节点Pop掉了
printf("%c ", front->data);
if (front->left)
{
QueuePush(&q, front->left);
}
if (front->right)
{
QueuePush(&q, front->right);
}
}


printf("\n");
QueueDestroy(&q);
}


** 条件while (!QueueEmpty(&q)):依次类推,当队列为空时就终止遍历。**

在这里队列的实现就不再赘述,详细请看博客:

队列的模拟实现(单链链表模拟)_暴走的橙子~的博客-CSDN博客

只不过在这里队列在头文件进行这样的修改。

  1. struct BinaryTreeNode;//声明出来节点的类型
  2. typedef struct BinaryTreeNoide* QDataType;//每个data存放的是每个节点的地址
  3. typedef struct QueueNode
  4. {
  5. QDataType data;
  6. struct QueueNode* next;
  7. }QNode;
  8. typedef struct Queue
  9. {
  10. QNode* head;
  11. QNode* tail;
  12. }Queue;

bool BinaryTreeComplete(BTNode* root)

  1. bool BinaryTreeComplete(BTNode* root)
  2. {
  3. Queue q;
  4. QueueInit(&q);
  5. QueuePush(&q, root);
  6. while (!QueueEmpty(&q))
  7. {
  8. BTNode* front = QueueHead(&q);//front是存放树节点的指针,树节点为空,front不一定为空
  9. QueuePop(&q);
  10. if (front == NULL)
  11. {
  12. break;
  13. }
  14. else
  15. {
  16. //NULL也放入
  17. QueuePush(&q, front->left);
  18. QueuePush(&q, front->right);
  19. }
  20. }
  21. while (!QueueEmpty(&q))
  22. {
  23. BTNode* front = QueueHead(&q);
  24. QueuePop(&q);
  25. if (front)
  26. {
  27. QueueDestroy(&q);
  28. return false;
  29. }
  30. }
  31. QueueDestroy(&q);
  32. return true;
  33. }

//判断是不是完全二叉树

//思想就是用一个队列来存放二叉树的结点地址,空地址也用data存放,直到遍历到data==NULL时,就终止第一次循环。接着遍历剩下队列的data,如果剩下的data全部为空,则返回true,说明时完全二叉树;反之,返回false,说明不是完全二叉树。

bool BinaryTreeComplete(BTNode* root)
{
Queue q;
QueueInit(&q);
QueuePush(&q, root);
while (!QueueEmpty(&q))
{


}
while (!QueueEmpty(&q))
{

/ /遍历剩下队列中的data地址。
BTNode* front = QueueHead(&q);
QueuePop(&q);
if (front)
{
QueueDestroy(&q);
return false;
}
}
QueueDestroy(&q);
return true;
}

void BinaryTreeDestroy(BTNode* root)

  1. void BinaryTreeDestroy(BTNode* root)
  2. {
  3. if (root == NULL)
  4. {
  5. return;
  6. }
  7. //后序遍历
  8. BinaryTreeDestroy(root->left);
  9. BinaryTreeDestroy(root->right);
  10. free(root);
  11. root = NULL;
  12. //前序遍历
  13. /*BTNode* lf = root->left;
  14. BTNode* rt = root->right;
  15. free(root);
  16. root = NULL;
  17. BinaryTreeDestroy(lf);
  18. BinaryTreeDestroy(rt);*/
  19. }

在这里推荐后序遍历方式销毁空间。代码的可读性会更高,另外,root是会被置空的。

而前序遍历销毁空间是,root在这里还没有置空,还需要在函数外面置空,比较麻烦。

完整代码

Binary.h

  1. #pragma once
  2. #include<stdio.h>
  3. #include<assert.h>
  4. #include<stdlib.h>
  5. #include<stdbool.h>
  6. typedef char BTDataType;
  7. typedef struct BinaryTreeNode
  8. {
  9. struct BinaryTreeNode* left;
  10. struct BinaryTreeNode* right;
  11. BTDataType data;
  12. }BTNode;
  13. BTNode* BuyNode(BTDataType x);
  14. BTNode* CreatBinaryTree();
  15. void PrevOrder(BTNode* root);
  16. void InOrder(BTNode* root);
  17. void PostOrder(BTNode* root);
  18. int BinaryTreeSize(BTNode* root);
  19. int BinaryTreeLeafSize(BTNode* root);
  20. //二叉树第K层结点个数
  21. int BinaryTreeLevelKSize(BTNode* root, int k);
  22. //二叉树的深度/高度
  23. int BinaryTreeDepth(BTNode* root);
  24. BTNode* BinaryTreeFind(BTNode* root, BTDataType x);
  25. //层序遍历
  26. void BinaryTreeLevelOrder(BTNode* root);
  27. //判断是不是完全二叉树
  28. bool BinaryTreeComplete(BTNode* root);
  29. void BinaryTreeDestroy(BTNode* root);

Queue.h

  1. #pragma once
  2. #include<stdio.h>
  3. #include<assert.h>
  4. #include<stdlib.h>
  5. #include<stdbool.h>
  6. struct BinaryTreeNode;
  7. typedef struct BinaryTreeNoide* QDataType;
  8. typedef struct QueueNode
  9. {
  10. QDataType data;
  11. struct QueueNode* next;
  12. }QNode;
  13. typedef struct Queue
  14. {
  15. QNode* head;
  16. QNode* tail;
  17. }Queue;
  18. void QueueInit(Queue* q);
  19. void QueueDestroy(Queue* q);
  20. void QueuePush(Queue* q, QDataType x);
  21. void QueuePop(Queue* q);
  22. bool QueueEmpty(Queue* q);
  23. int QueueSize(Queue* q);
  24. QDataType QueueTail(Queue* q);
  25. QDataType QueueHead(Queue* q);

Binary.c

  1. #define _CRT_SECURE_NO_WARNINGS 1
  2. #include"Binary.h"
  3. #include"Queue.h"
  4. BTNode* BuyNode(BTDataType x)
  5. {
  6. BTNode* newnode = (BTNode*)malloc(sizeof(BTNode));
  7. if (newnode == NULL)
  8. {
  9. printf("malloc fail\n");
  10. exit(-1);
  11. }
  12. newnode->left = NULL;
  13. newnode->right = NULL;
  14. newnode->data = x;
  15. return newnode;
  16. }
  17. BTNode* CreatBinaryTree()
  18. {
  19. BTNode* nodeA = BuyNode('A');
  20. BTNode* nodeB = BuyNode('B');
  21. BTNode* nodeC = BuyNode('C');
  22. BTNode* nodeD = BuyNode('D');
  23. BTNode* nodeE = BuyNode('E');
  24. BTNode* nodeF = BuyNode('F');
  25. BTNode* nodeG = BuyNode('G');
  26. nodeA->left = nodeB;
  27. nodeA->right = nodeC;
  28. nodeB->left = nodeD;
  29. nodeC->left = nodeE;
  30. nodeC->right = nodeF;
  31. nodeF->left = nodeG;
  32. return nodeA;
  33. }
  34. void PrevOrder(BTNode* root)
  35. {
  36. if (root == NULL) //这里不用assert,否则遇到空时,就过不去了
  37. {
  38. printf("NULL ");
  39. return;
  40. }
  41. printf("%c ", root->data);
  42. PrevOrder(root->left);
  43. PrevOrder(root->right);
  44. }
  45. void InOrder(BTNode* root)
  46. {
  47. if (root == NULL)
  48. {
  49. printf("NULL ");
  50. return;
  51. }
  52. InOrder(root->left);
  53. printf("%c ", root->data);
  54. InOrder(root->right);
  55. }
  56. void PostOrder(BTNode* root)
  57. {
  58. if (root == NULL)
  59. {
  60. printf("NULL ");
  61. return;
  62. }
  63. PostOrder(root->left);
  64. PostOrder(root->right);
  65. printf("%c ", root->data);
  66. }
  67. int BinaryTreeSize(BTNode* root)
  68. {
  69. //if (root == NULL)
  70. //{
  71. // return 0;
  72. //}
  73. //return BinaryTreeSize(root->left) + BinaryTreeSize(root->right) + 1;
  74. return root == NULL ? 0 : BinaryTreeSize(root->left) + BinaryTreeSize(root->right) + 1;
  75. }
  76. int BinaryTreeLeafSize(BTNode* root)
  77. {
  78. if (root == NULL) //这个条件不能忘
  79. {
  80. return 0;
  81. }
  82. if (root->left == NULL && root->right == NULL)
  83. {
  84. return 1;
  85. }
  86. return BinaryTreeLeafSize(root->left) + BinaryTreeLeafSize(root->right);
  87. }
  88. //二叉树第K层结点个数
  89. int BinaryTreeLevelKSize(BTNode* root, int k)
  90. {
  91. assert(k >= 1);
  92. if (root == NULL)
  93. {
  94. return 0;
  95. }
  96. if (k == 1)
  97. {
  98. return 1;
  99. }
  100. int left = BinaryTreeLevelKSize(root->left, k - 1);
  101. int right = BinaryTreeLevelKSize(root->right, k - 1);
  102. return left + right;
  103. //return BinaryTreeLevelKSize(root->left, k - 1) + BinaryTreeLevelKSize(root->right, k - 1);
  104. }
  105. //二叉树的深度/高度
  106. int BinaryTreeDepth(BTNode* root)
  107. {
  108. if (root == NULL)
  109. {
  110. return 0;
  111. }
  112. int leftDepth = BinaryTreeDepth(root->left);
  113. int rightDepth = BinaryTreeDepth(root->right);
  114. return leftDepth > rightDepth ? leftDepth + 1 : rightDepth + 1;
  115. }
  116. BTNode* BinaryTreeFind(BTNode* root, BTDataType x)
  117. {
  118. if (root == NULL)
  119. {
  120. return NULL;
  121. }
  122. if (root->data == x)
  123. {
  124. return root;
  125. }
  126. BinaryTreeFind(root->left, x);
  127. BinaryTreeFind(root->right, x);
  128. return NULL;
  129. }
  130. //层序遍历
  131. void BinaryTreeLevelOrder(BTNode* root)
  132. {
  133. if (root == NULL)
  134. {
  135. return;
  136. }
  137. Queue q;
  138. QueueInit(&q);
  139. QueuePush(&q, root);
  140. while (!QueueEmpty(&q))
  141. {
  142. BTNode* front = QueueHead(&q);
  143. QueuePop(&q);//Pop掉的是存放树节点的地址,而不是把树节点Pop掉了
  144. printf("%c ", front->data);
  145. if (front->left)
  146. {
  147. QueuePush(&q, front->left);
  148. }
  149. if (front->right)
  150. {
  151. QueuePush(&q, front->right);
  152. }
  153. }
  154. printf("\n");
  155. QueueDestroy(&q);
  156. }
  157. //判断是不是完全二叉树
  158. bool BinaryTreeComplete(BTNode* root)
  159. {
  160. Queue q;
  161. QueueInit(&q);
  162. QueuePush(&q, root);
  163. while (!QueueEmpty(&q))
  164. {
  165. BTNode* front = QueueHead(&q);//front是存放树节点的指针,树节点为空,front不一定为空
  166. QueuePop(&q);
  167. if (front == NULL)
  168. {
  169. break;
  170. }
  171. else
  172. {
  173. //NULL也放入
  174. QueuePush(&q, front->left);
  175. QueuePush(&q, front->right);
  176. }
  177. }
  178. while (!QueueEmpty(&q))
  179. {
  180. BTNode* front = QueueHead(&q);
  181. QueuePop(&q);
  182. if (front)
  183. {
  184. QueueDestroy(&q);
  185. return false;
  186. }
  187. }
  188. QueueDestroy(&q);
  189. return true;
  190. }
  191. void BinaryTreeDestroy(BTNode* root)
  192. {
  193. if (root == NULL)
  194. {
  195. return;
  196. }
  197. //后序遍历
  198. BinaryTreeDestroy(root->left);
  199. BinaryTreeDestroy(root->right);
  200. free(root);
  201. root = NULL;
  202. //前序遍历
  203. /*BTNode* lf = root->left;
  204. BTNode* rt = root->right;
  205. free(root);
  206. root = NULL;
  207. BinaryTreeDestroy(lf);
  208. BinaryTreeDestroy(rt);*/
  209. }

Queue.c

  1. #define _CRT_SECURE_NO_WARNINGS 1
  2. #include"Queue.h"
  3. void QueueInit(Queue* q)
  4. {
  5. assert(q);
  6. q->head = NULL;
  7. q->tail = NULL;
  8. }
  9. void QueueDestroy(Queue* q)
  10. {
  11. assert(q);
  12. QNode* cur = q->head;
  13. while (cur)
  14. {
  15. QNode* next = cur->next;
  16. free(cur);
  17. cur = next;
  18. }
  19. q->head=q->tail = NULL;//在这里头结点和尾结点要置空
  20. }
  21. //队尾插入数据
  22. void QueuePush(Queue* q, QDataType x)
  23. {
  24. assert(q);
  25. QNode* newnode = (QNode*)malloc(sizeof(QNode));
  26. if (newnode == NULL)
  27. {
  28. printf("malloc fail\n");
  29. exit(-1);
  30. }
  31. newnode->data = x;
  32. newnode->next = NULL;
  33. if (q->head == NULL)
  34. {
  35. q->head = q->tail = newnode;
  36. }
  37. else
  38. {
  39. q->tail->next = newnode;
  40. q->tail = newnode;
  41. }
  42. }
  43. //队头删除数据
  44. void QueuePop(Queue* q)
  45. {
  46. assert(q);
  47. assert(!QueueEmpty(q));
  48. if (q->head == q->tail)
  49. {
  50. free(q->head);
  51. q->head = q->tail = NULL;
  52. }
  53. else
  54. {
  55. QNode* next = q->head->next;
  56. free(q->head);
  57. q->head = next;
  58. }
  59. //方法二
  60. //QNode* next = q->head->next;
  61. //free(q->head);
  62. //q->head = next;
  63. //if (q->head == NULL)
  64. //{
  65. // q->tail = NULL;
  66. //}
  67. }
  68. bool QueueEmpty(Queue* q)
  69. {
  70. assert(q);
  71. return q->head == NULL;
  72. }
  73. int QueueSize(Queue* q)
  74. {
  75. assert(q);
  76. int size = 0;
  77. QNode* cur = q->head;
  78. while (cur)
  79. {
  80. size++;
  81. cur = cur->next;
  82. }
  83. return size;
  84. }
  85. QDataType QueueTail(Queue* q)
  86. {
  87. assert(q);
  88. assert(!QueueEmpty(q));
  89. return q->tail->data;
  90. }
  91. QDataType QueueHead(Queue* q)
  92. {
  93. assert(q);
  94. assert(!QueueEmpty(q));
  95. return q->head->data;
  96. }

测试代码test.c

  1. #define _CRT_SECURE_NO_WARNINGS 1
  2. #include"Binary.h"
  3. void BinaryTest1()
  4. {
  5. BTNode* root = CreatBinaryTree();
  6. PrevOrder(root);
  7. printf("\n");
  8. InOrder(root);
  9. printf("\n");
  10. PostOrder(root);
  11. printf("\n");
  12. int size = BinaryTreeSize(root);//节点个数
  13. printf("%d\n", size);
  14. int LeafSize = BinaryTreeLeafSize(root);
  15. printf("%d\n", LeafSize);
  16. int sizeK = BinaryTreeLevelKSize(root, 3);
  17. printf("%d\n", sizeK);
  18. int depth = BinaryTreeDepth(root);
  19. printf("%d\n", depth);
  20. BinaryTreeLevelOrder(root);
  21. printf("%d\n",BinaryTreeComplete(root));
  22. BinaryTreeDestroy(root);
  23. root = NULL;
  24. }
  25. int main()
  26. {
  27. BinaryTest1();
  28. return 0;
  29. }

测试结果


本文转载自: https://blog.csdn.net/qq_58724706/article/details/122262311
版权归原作者 暴走的橙子~ 所有, 如有侵权,请联系我们删除。

“C语言实现二叉树(初阶数据结构)”的评论:

还没有评论