⭐️ Preface
✨Conceptual properties of binary trees
⭐️ Implementation of binary tree chain structure
Structure definition:
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef int BinaryTreeDataType;
typedef struct BinaryTreeNode {
BinaryTreeDataType value;
struct BinaryTreeNode* left;
struct BinaryTreeNode* right;
}BinaryTreeNode;
⭕️ Binary tree traversal
According to the rules, the traversal of the binary tree is divided into: recursive traversal of pre-order/in-order/post-order.
- Preorder traversal (PreOrder Traversal): The operation of visiting the root node occurs before traversing its left and right subtrees.
根 -> 左子树 -> 右子树
- Inorder traversal (InOrder Traversal): The operation of visiting the root node occurs between traversing the left and right subtrees.
左子树 -> 根 -> 右子树
- Postorder traversal (PostOrder Traversal): The operation of visiting the root node occurs after traversing its left and right subtrees.
左子树 -> 右子树 -> 根
PreOrder
code:
// 前序遍历递归 根 -> 左子树 -> 右子树
void PreOrder(BinaryTreeNode* root) {
if (root == NULL) {
printf("# ");
return;
}
printf("%d ", root->value);
PreOrder(root->left);
PreOrder(root->right);
}
Preorder recursive flow chart:
前序递归遍历顺序为:1 2 3 # # # 4 5 # # 6 # #
InOrder
code:
// 中序遍历递归 左子树 -> 根 -> 右子树
void InOrder(BinaryTreeNode* root) {
if (root == NULL) {
printf("# ");
return;
}
InOrder(root->left);
printf("%d " , root->value);
InOrder(root->right);
}
Inorder recursive flow chart:
中序递归遍历顺序为:# 3 # 2 # 1 # 5 # 4 # 6 #
PostOrder
code:
// 后序遍历递归 左子树 -> 右子树 -> 根
void PostOrder(BinaryTreeNode* root) {
if (root == NULL) {
printf("# ");
return;
}
PostOrder(root->left);
PostOrder(root->right);
printf("%d " , root->value);
}
Postorder recursive flow chart:
后序递归遍历顺序为:# # 3 # 2 # # 5 # # 6 4 1
注:# 代表空树
⭕️ Other interfaces of binary tree
// 节点的数量
int BinaryTreeSize(BinaryTreeNode* root);
// 叶子节点的数量
int BinaryTreeLeafSize(BinaryTreeNode* root);
// 求k层节点的个数
int BinaryTreeKLevelSize(BinaryTreeNode* root , int k);
// 二叉树中查找某个元素
BinaryTreeNode* BinaryTreeFind(BinaryTreeNode* root , BinaryTreeDataType x);
BinaryTreeSize
code:
// 节点的数量
int BinaryTreeSize(BinaryTreeNode* root) {
if (root == NULL) {
return 0;
}
return 1 + BinaryTreeSize(root->left) + BinaryTreeSize(root->right);
}
BinaryTreeSize
Recursive flowchart:
BinaryTreeLeafSize
code:
// 叶子节点的数量
int BinaryTreeLeafSize(BinaryTreeNode* root) {
if (root == NULL) {
return 0;
}
if (root->left == NULL && root->right == NULL) {
return 1;
}
return BinaryTreeLeafSize(root->left) + BinaryTreeLeafSize(root->right);
}
BinaryTreeLeafSize
Recursive flowchart:
BinaryTreeKLevelSize
code:
// 求k层节点的个数
int BinaryTreeKLevelSize(BinaryTreeNode* root , int k) {
assert(k >= 1);
if (root == NULL) {
return 0;
}
if (k == 1) {
return 1;
}
return BinaryTreeKLevelSize(root->left , k - 1) + BinaryTreeKLevelSize(root->right , k - 1);
}
BinaryTreeKLevelSize
Recursive flowchart:
BinaryTreeFind
code:
// 二叉树中查找某个元素
BinaryTreeNode* BinaryTreeFind(BinaryTreeNode* root , BinaryTreeDataType x) {
if (root == NULL) {
return NULL;
}
if (root->value == x) {
return root;
}
BinaryTreeNode* left = BinaryTreeFind(root->left , x);
if (left != NULL) {
return left;
}
BinaryTreeNode* right = BinaryTreeFind(root->right , x);
if (right != NULL) {
return right;
}
return NULL;
}
BinaryTreeFind
Recursive flowchart: