• 【数据结构】二叉树


    1.二叉树的遍历

    前序,中序,后序遍历

    学习二叉树结构,最简单的方式就是遍历。所谓二叉树遍历(Traversal)是按照某种特定的规则,依次对二叉树中的节点进行相应的操作,并且每个节点只操作一次。访问结点所做的操作依赖于具体的应用问题。 遍历是二叉树上最重要的运算之一,也是二叉树上进行其它运算的基础。

    1. 前序遍历——访问根结点的操作发生在遍历其左右子树之前。
    2. 中序遍历——访问根结点的操作发生在遍历其左右子树之中(间)。
    3. 后序遍历——访问根结点的操作发生在遍历其左右子树之后。

    在这里插入图片描述

    1.前序遍历:根,左子树,右子树
    2.中序遍历:左子树,根,右子树
    3.后序遍历:左子树,右子树,根.
    前序遍历访问数据为1,2, 3,NULL(3的左子树),NULL(3的右子树),NULL(2的右子树),4,5,NULL(5的左数),NULL(5的右树),6,NULL(6的左树),NULL(6的右树).
    中序遍历访问数据为NULL(3的左子树),3,NULL(3的右子树),2,NULL(2的右子树),1,NULL(5的左子树),5,NULL(5的右子树),4,NULL(6的左子树),6,NULL(6的右子树).
    后序遍历访问数据为NULL(3的左子树),NULL(3的右子树),3,NULL(2的右子树),2,NULL(5的左子树),NULL(5的右子树),5,NULL(6的左子树),NULL(6的右子树),6,4,1 .
    前序遍历的实现

    // 二叉树前序遍历
    void PreOrder(BTNode* root)
    {
    	if (root == NULL)
    	{
    		printf("NULL  ");
    		return;
    	}
    	printf("%d  ",root->data);
    	PreOrder(root->left);
    	PreOrder(root->right);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    前序递归打印数据
    在这里插入图片描述
    二叉树中序遍历

    // 二叉树中序遍历
    void InOrder(BTNode* root)
    {
    	if (root == NULL)
    	{
    		printf("NULL  ");
    		return;
    	}
    	
    	PreOrder(root->left);
    	printf("%d  ", root->data);
    	PreOrder(root->right);
    
    
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    二叉树后序遍历

    void PostOrder(BTNode* root)
    {
    	if (root == NULL)
    	{
    		printf("NULL  ");
    		return;
    	}
    
    	PostOrder(root->left);
    	
    	PostOrder(root->right);
    	printf("%d  ", root->data);
    
    
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    代码实现

    include<stdio.h>
    #include 
    #include 
    typedef struct BinaryTreeNode
    {
    	int data;
    	struct BinaryTreeNode* left;
    	struct BinaryTreeNode* right;
    }BTNode;
    BTNode* BuyNode(int x)
    {
    	BTNode* node = (BTNode*)malloc(sizeof(BTNode));
    	if (node == NULL)
    	{
    		perror("malloc fail");
    		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;
    }
    // 二叉树前序遍历
    void PreOrder(BTNode* root)
    {
    	if (root == NULL)
    	{
    		printf("NULL  ");
    		return;
    	}
    	printf("%d  ",root->data);
    	PreOrder(root->left);
    	PreOrder(root->right);
    }
    // 二叉树中序遍历
    void InOrder(BTNode* root)
    {
    	if (root == NULL)
    	{
    		printf("NULL  ");
    		return;
    	}
    	
    	InOrder(root->left);
    	printf("%d  ", root->data);
    	InOrder(root->right);
    
    
    
    }
    // 二叉树后序遍历
    void PostOrder(BTNode* root)
    {
    	if (root == NULL)
    	{
    		printf("NULL  ");
    		return;
    	}
    
    	PostOrder(root->left);
    	
    	PostOrder(root->right);
    	printf("%d  ", root->data);
    
    
    
    }
    int main()
    {
    	BTNode* bk=CreatBinaryTree();
    
    	PreOrder(bk);
    	printf("\n");
    	InOrder(bk);
    	printf("\n");
    	PostOrder(bk);
    	printf("\n");
    
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96

    编译运行
    在这里插入图片描述

    二叉树结点的个数

    int size = 0;//要统计每次递归的次数也就是二叉树结点的个数,定义	全局变量
    
    // 二叉树节点个数
    void BinaryTreeSize(BTNode* root)
    {
    
    	if (root == NULL)
    	{
    		return;
    
    
    	}
    	
    		++size;
    	
    	BinaryTreeSize(root->left);
    	BinaryTreeSize(root->right);
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    求二叉树结点的个数的另一个方法,采用分治的思想。
    在这里插入图片描述

    统计学校的人数,院长问导员,导员问老师,老师问班长,班长统计各班的人数。如果遇到空,返回0,其他情况都返回左子树的人数加右子树的人数加上自己,比如老师说要向导员报告人数,老师必须向导员汇报老师左子树的学生,以及右子树的学生加上自己。

    int BinaryTreeSize(BTNode* root)
    {
    
    	if (root == NULL)
    	{
    		return 0;
    
    
    	}
    
    	
    
    	return BinaryTreeSize(root->left) + BinaryTreeSize(root->right) + 1;
    
    	
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    代码简化

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

    二叉树叶结点的个数

    叶结点的判断,如果该结点的左节点和右节点都为空的话,这个结点就是叶子结点,并且将该值返回

    int BinaryTreeLeafSize(BTNode* root)
    {
    	if (root == NULL)
    	{
    		return 0;
    	}
    	else if (root->left == NULL && root->right == NULL)
    	{
    		return 1;
    	}
    	
    	return BinaryTreeLeafSize(root->left) + BinaryTreeLeafSize(root->right);
    
    
    
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    二叉树第k层结点个数

    二叉树第k层结点个数

    子问题:转换成左子树的第k-1层和右子树的第k-1层

    结束条件:k==1且结点不为空

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

    在这里插入图片描述

    二叉树的深度

    返回左右子树中高的+1;
    在这里插入图片描述

    int BTreeHeight(BTNode* root)
    {
    	if (root == NULL)
    	{
    		return 0;
    	}
    	return   BTreeHeight(root->left) > BTreeHeight(root->right) ? BTreeHeight(root->left) + 1 : BTreeHeight(root->right) + 1;
    
    
    
    
    
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    方法2:

    int BTreeHeight(BTNode* root)
    {
    	if (root == NULL)
    	{
    		return 0;
    	}
    	int left = BTreeHeight(root->left);
    	int right = BTreeHeight(root->right);
    	return left > right ? left + 1 : right + 1;
    
    
    
    
    
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    比较方法1和方法2,方法一效率要慢一些,因为方法一比较完左右子树返回的值后(这里算递归了一次)还必须再次递归左子树或者右子树,递归左右子树中大的那一个,时间上会翻倍。

    整体代码实现

    #include
    #include 
    #include 
    typedef struct BinaryTreeNode
    {
    	int data;
    	struct BinaryTreeNode* left;
    	struct BinaryTreeNode* right;
    }BTNode;
    BTNode* BuyNode(int x)
    {
    	BTNode* node = (BTNode*)malloc(sizeof(BTNode));
    	if (node == NULL)
    	{
    		perror("malloc fail");
    		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;
    }
    // 二叉树前序遍历
    void PreOrder(BTNode* root)
    {
    	if (root == NULL)
    	{
    		printf("NULL  ");
    		return;
    	}
    	printf("%d  ",root->data);
    	PreOrder(root->left);
    	PreOrder(root->right);
    }
    // 二叉树中序遍历
    void InOrder(BTNode* root)
    {
    	if (root == NULL)
    	{
    		printf("NULL  ");
    		return;
    	}
    	
    	InOrder(root->left);
    	printf("%d  ", root->data);
    	InOrder(root->right);
    
    
    
    }
    // 二叉树后序遍历
    void PostOrder(BTNode* root)
    {
    	if (root == NULL)
    	{
    		printf("NULL  ");
    		return;
    	}
    
    	PostOrder(root->left);
    	
    	PostOrder(root->right);
    	printf("%d  ", root->data);
    
    
    
    }
    int size = 0;
    // 二叉树节点个数
    //void BinaryTreeSize(BTNode* root)
    //{
    //
    //	if (root == NULL)
    //	{
    //		return;
    //
    //
    //	}
    //	
    //		++size;
    //	
    //	BinaryTreeSize(root->left);
    //	BinaryTreeSize(root->right);
    //
    //}
    //if (root == NULL)
    //{
    //	return 0;
    //
    //
    //}
    //
    //
    //
    //return BinaryTreeSize(root->left) + BinaryTreeSize(root->right) + 1;
    int BinaryTreeSize(BTNode* root)
    {
    
    return root == 0 ? NULL : BinaryTreeSize(root->left) + BinaryTreeSize(root->right) + 1;
    
    
    	
    
    }
    // 二叉树叶子节点个数
    int BinaryTreeLeafSize(BTNode* root)
    {
    	if (root == NULL)
    	{
    		return 0;
    	}
    	else if (root->left == NULL && root->right == NULL)
    	{
    		return 1;
    	}
    	
    	return BinaryTreeLeafSize(root->left) + BinaryTreeLeafSize(root->right);
    
    
    
    
    }
    // 二叉树第k层节点个数
    int BinaryTreeLevelKSize(BTNode* root, int k)
    {
    	assert(k >= 1);
    		if (root == NULL)
    		{
    			return 0;
    
    		}
    		if (k == 1)
    		{
    			return 1;
    		}
    		return  BinaryTreeLevelKSize(root->left,k-1)+ BinaryTreeLevelKSize(root->right,k -1);
    
    }
    
    //求二叉树的高度
    int BTreeHeight(BTNode* root)
    {
    	if (root == NULL)
    	{
    		return 0;
    	}
    	int left = BTreeHeight(root->left);
    	int right = BTreeHeight(root->right);
    	return left > right ? left + 1 : right + 1;
    
    
    
    
    
    
    }
    int main()
    {
    	BTNode* bk=CreatBinaryTree();
    	BinaryTreeSize(bk);
    
    	PreOrder(bk);
    	printf("\n");
    	InOrder(bk);
    	printf("\n");
    	PostOrder(bk);
    	printf("\n");
    	printf("二叉树结点个数%d \n", BinaryTreeSize(bk));
    	printf("二叉树叶子结点个数%d \n",BinaryTreeLeafSize(bk));
    	printf("二叉树第k层结点数%d ", BinaryTreeLevelKSize(bk,3));
    	printf("二叉树深度%d ", BTreeHeight(bk));
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188

    编译运行
    在这里插入图片描述

  • 相关阅读:
    Matlab绘制双坐标轴图示例函数yyaxis
    Python 3.10里面的Match-Case语法详解
    【初认Redis】
    多模型知识图谱
    Linux内核源码分析 (B.6)从内核源码看 slab 内存池的创建初始化流程
    linux的shell script判断用户输入的字符串,判断主机端口开通情况
    Mac电脑远程连接Windows桌面Microsoft Remote Desktop
    AWS 网络
    972信息检索 | 第六章 专业性书目信息检索系统
    docker学习记录(一)
  • 原文地址:https://blog.csdn.net/yyqzjw/article/details/134535594