赞
踩
一、 实现链式结构二叉树
1.1 Tree.h
1.2 Tree.c 前中后序遍历
中序遍历
后续遍历
1.2 Tree.c 结点个数
1.3Tree.c 叶子节点个数
1.4 Tree.c 二叉树的高度
1.5 Tree.c 层序遍历
1.6 判断是否为完全二叉树
1.7 销毁二叉树
test.c
- #include<stdio.h>
- #include<stdlib.h>
- #include<assert.h>
- #include<stdbool.h>
-
- //定义二叉树的链式结构
- //二叉树结点的结构
- typedef int BTDataType;
- typedef struct BinaryTreeNode
- {
- int data;
- struct BinaryTreeNode* left;
- struct BinaryTreeNode* right;
- }BTNode;
-
- //前序遍历
- void PreOrder(BTNode* root);
- //中序遍历
- void InOrder(BTNode* root);
- //后序遍历
- void PostOrder(BTNode* root);
-
- // ⼆叉树结点个数
- int BinaryTreeSize(BTNode* root);
-
- // ⼆叉树叶⼦结点个数
- int BinaryTreeLeafSize(BTNode* root);
-
- // ⼆叉树第k层结点个数
- int BinaryTreeLevelKSize(BTNode* root, int k);
-
- //⼆叉树的深度/⾼度
- int BinaryTreeDepth(BTNode* root);
-
- // ⼆叉树查找值为x的结点
- BTNode* BinaryTreeFind(BTNode* root, BTDataType x);
-
- // ⼆叉树销毁
- void BinaryTreeDestory(BTNode** root);
-
- //层序遍历
- void LevelOrder(BTNode* root);
-
- //判断二叉树是否为完全二叉树
- bool BinaryTreeComplete(BTNode* root);

下面我们可以在下方 test.c 文件中手动创建一棵链式二叉树,方便对链式结构二叉树遍历方式以及各种方法的实现的研究:
- void PreOrder(BTNode* root)
- {
- if (root == NULL)
- {
- return;
- }
- printf("%d", root->data);
- PreOrder(root->left);
- PreOrder(root->right);
- }
输出结果为: 1 2 4 3
其他两种遍历方式的递归过程也是一样的做法,下面就只展示代码部分了。
- void InOrder(BTNode* root)
- {
- if (root == NULL)
- {
- return;
- }
- InOrder(root->left);
- printf("%d", root->data);
- InOrder(root->right);
- }
输出结果为: 4 2 1 3
- void PosOrder(BTNode* root)
- {
- if (root == NULL)
- {
- return;
- }
- PostOrder(root->left);
- PostOrder(root->right);
- printf("%d", root->data);
- }
输出结果为: 4 2 3 1
- int BinaryTreeSize(BTNode* root)
- {
- if (root == NULL);
- {
- return 0;
- }
- return 1 + BinaryTreeLeafSize(root->left) + BinaryTreeLeafSize(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 BinaryTreeDepth(BTNode* root)
- {
- if (root == NULL)
- {
- return 0;
- }
- int lefttree = BinaryTreeDepth(root->left);
- int righttree = BinaryTreeDepth(root->right);
-
- return lefttree > righttree ? lefttree + 1 : righttree + 1;
- }
在上述构建好的二叉树中,如果要对其进行层序遍历的话,期待输出的结果为 1 2 3 4 ,
此时我们可以借助队列这一数据结构实现,所以在编写代码时先要包括 “Queue.h” 的头文件,具体队列的实现可以参考 数据结构(队列)-CSDN博客。
- void LevelOrder(BTNode* root)
- {
- Queue q;
- QueueInit(&q);
- QueuePush(&q, root);
- while (!QueueEmpty(&q))
- {
- //取队头,打印
- BTNode* front = QueueFront(&q);
- printf("%d ", front->data);
- QueuePop(&q);
- //队头节点的左右孩子入队列
- if (front->left)
- QueuePush(&q, front->left);
- if (front->right)
- QueuePush(&q, front->right);
- }
- //队列为空
- QueueDestroy(&q);
- }

- bool BinaryTreeComplete(BTNode* root)
- {
- Queue q;
- QueueInit(&q);
- 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))
- {
- if (front != NULL)
- {
- QueueDestory(&q);
- return false;
- }
- }
- QueueDestory(&q);
- return true;
- }

- void BinaryTreeDestory(BTNode** root)
- {
- if (*root == NULL)
- {
- return;
- }
- BinaryTreeDestory(&((*root)->left));
- BinaryTreeDestory(&((*root)->right));
-
- free(*root);
- *root = NULL;
- }
- #include"Tree.h"
-
- BTNode* buyNode(BTDataType x)
- {
- //创建结点
- BTNode* newnode = (BTNode*)malloc(sizeof(BTDataType));
- if (newnode == NULL)
- {
- perror("malloc fail!");
- exit(1);
-
- }
- newnode->data = x;
- newnode->left = newnode->right = NULL;
-
- return newnode;
- }
-
- void treetest()
- {
- BTNode* node1 = buyNode(1);
- BTNode* node2 = buyNode(2);
- BTNode* node3 = buyNode(3);
- BTNode* node4 = buyNode(4);
-
- node1->left = node2;
- node1->right = node3;
- node2->light = node4;
- }
-
-
- int main()
- {
- treetest();
- return 0;
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。