[Data Structure] Detailed Explanation of Binary Tree (1)

⭐️ 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.
    • 左子树 -> 右子树 -> 根

insert image description here


PreOrdercode:

// 前序遍历递归 根 -> 左子树 -> 右子树
void PreOrder(BinaryTreeNode* root) {
    
    
	if (root == NULL) {
    
    
		printf("# ");
		return;
	}

	printf("%d ", root->value);
	PreOrder(root->left);
	PreOrder(root->right);
}

Preorder recursive flow chart:
insert image description here

前序递归遍历顺序为:1 2 3 # # # 4 5 # # 6 # #

InOrdercode:

// 中序遍历递归 左子树 -> 根 -> 右子树
void InOrder(BinaryTreeNode* root) {
    
    
	if (root == NULL) {
    
    
		printf("# ");
		return;
	}

	InOrder(root->left);
	printf("%d " , root->value);
	InOrder(root->right);
}

Inorder recursive flow chart:

insert image description here

中序递归遍历顺序为:# 3 # 2 # 1 # 5 # 4 # 6 #

PostOrdercode:

// 后序遍历递归 左子树 -> 右子树 -> 根
void PostOrder(BinaryTreeNode* root) {
    
    
	if (root == NULL) {
    
    
		printf("# ");
		return;
	}

	PostOrder(root->left);
	PostOrder(root->right);
	printf("%d " , root->value);
}

Postorder recursive flow chart:
insert image description here
后序递归遍历顺序为:# # 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);

BinaryTreeSizecode:

// 节点的数量
int BinaryTreeSize(BinaryTreeNode* root) {
    
    
	if (root == NULL) {
    
    
		return 0;
	}

	return 1 + BinaryTreeSize(root->left) + BinaryTreeSize(root->right);
}

BinaryTreeSizeRecursive flowchart:
insert image description here


BinaryTreeLeafSizecode:

// 叶子节点的数量
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);
}

BinaryTreeLeafSizeRecursive flowchart:
insert image description here


BinaryTreeKLevelSizecode:

// 求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);
}

BinaryTreeKLevelSizeRecursive flowchart:
insert image description here


BinaryTreeFindcode:

// 二叉树中查找某个元素
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;
}

BinaryTreeFindRecursive flowchart:

insert image description here


Guess you like

Origin blog.csdn.net/cccyi7/article/details/131788672