
一个森林是0棵或多棵不相交(非空)树的集合,通常是一个有序的集合。换句话说,森林由多个树组成,这些树之间没有交集,且可以按照一定的次序排列。在森林中,每棵树都是独立的,具有根节点和子树,树与树之间没有直接的连接关系。 森林是树的扩展概念,它是由多个树组成的集合。在计算机科学中,森林也被广泛应用于数据结构和算法设计中,特别是在图论和网络分析等领域。

参照前文:【数据结构】树与二叉树(一):树(森林)的基本概念:父亲、儿子、兄弟、后裔、祖先、度、叶子结点、分支结点、结点的层数、路径、路径长度、结点的深度、树的深度
选择合适的树的存储结构通常取决于具体应用的需求。 Father链接结构适合于查找父节点的操作频繁,而儿子链表链接结构和左儿子右兄弟链接结构适用于频繁查找子节点的情况。

【数据结构】树与二叉树(十八):树的存储结构——Father链接结构、儿子链表链接结构
左儿子右兄弟链接结构通过使用每个节点的三个域(FirstChild、Data、NextBrother)来构建一棵树,同时使得树具有二叉树的性质。具体来说,每个节点包含以下信息:
通过这样的结构,整棵树可以用左儿子右兄弟链接结构表示成一棵二叉树。这种表示方式有时候被用于一些特殊的树结构,例如二叉树、二叉树的森林等。这种结构的优点之一是它更紧凑地表示树,而不需要额外的指针来表示兄弟关系。

A
/|\
B C D
/ \
E FA
|
B -- C -- D
|
E -- F即:
A
/
B
\
C
/ \
E D
\
F
typedef struct TreeNode {
char data;
struct TreeNode* firstChild;
struct TreeNode* nextBrother;
} TreeNode;TreeNode* createNode(char data) {
TreeNode* newNode = (TreeNode*)malloc(sizeof(TreeNode));
if (newNode != NULL) {
newNode->data = data;
newNode->firstChild = NULL;
newNode->nextBrother = NULL;
}
return newNode;
}TreeNode* convertToBinaryTree(TreeNode* root) {
if (root == NULL) {
return NULL;
}
// 处理第一个子节点
TreeNode* binaryFirstChild = convertToBinaryTree(root->firstChild);
// 处理下一个兄弟节点
TreeNode* binaryNextBrother = convertToBinaryTree(root->nextBrother);
// 构建二叉树
root->firstChild = binaryFirstChild;
root->nextBrother = binaryNextBrother;
return root;
}【数据结构】树与二叉树(十二):二叉树的递归创建(算法CBT)
【数据结构】树与二叉树(七):二叉树的遍历(先序、中序、后序及其C语言实现)
void printBinaryTree(TreeNode* root) {
if (root != NULL) {
printf("%c ", root->data);
printBinaryTree(root->firstChild);
printBinaryTree(root->nextBrother);
}
}void freeTree(TreeNode* root) {
if (root != NULL) {
freeTree(root->firstChild);
freeTree(root->nextBrother);
free(root);
}
}int main() {
// 构建左儿子右兄弟链接结构的树
TreeNode* A = createNode('A');
TreeNode* B = createNode('B');
TreeNode* C = createNode('C');
TreeNode* D = createNode('D');
TreeNode* E = createNode('E');
TreeNode* F = createNode('F');
A->firstChild = B;
B->nextBrother = C;
C->nextBrother = D;
C->firstChild = E;
E->nextBrother = F;
// 转化为二叉树
TreeNode* binaryRoot = convertToBinaryTree(A);
// 打印二叉树
printf("Binary Tree (Preorder): ");
printBinaryTree(binaryRoot);
printf("\n");
// 释放树节点
freeTree(binaryRoot);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
// 定义树节点
typedef struct TreeNode {
char data;
struct TreeNode* firstChild;
struct TreeNode* nextBrother;
} TreeNode;
// 创建树节点
TreeNode* createNode(char data) {
TreeNode* newNode = (TreeNode*)malloc(sizeof(TreeNode));
if (newNode != NULL) {
newNode->data = data;
newNode->firstChild = NULL;
newNode->nextBrother = NULL;
}
return newNode;
}
// 将左儿子右兄弟链接结构转化为二叉树
TreeNode* convertToBinaryTree(TreeNode* root) {
if (root == NULL) {
return NULL;
}
// 处理第一个子节点
TreeNode* binaryFirstChild = convertToBinaryTree(root->firstChild);
// 处理下一个兄弟节点
TreeNode* binaryNextBrother = convertToBinaryTree(root->nextBrother);
// 构建二叉树
root->firstChild = binaryFirstChild;
root->nextBrother = binaryNextBrother;
return root;
}
// 打印二叉树(前序遍历)
void printBinaryTree(TreeNode* root) {
if (root != NULL) {
printf("%c ", root->data);
printBinaryTree(root->firstChild);
printBinaryTree(root->nextBrother);
}
}
// 释放树节点及其子树
void freeTree(TreeNode* root) {
if (root != NULL) {
freeTree(root->firstChild);
freeTree(root->nextBrother);
free(root);
}
}
int main() {
// 构建左儿子右兄弟链接结构的树
TreeNode* A = createNode('A');
TreeNode* B = createNode('B');
TreeNode* C = createNode('C');
TreeNode* D = createNode('D');
TreeNode* E = createNode('E');
TreeNode* F = createNode('F');
A->firstChild = B;
B->nextBrother = C;
C->nextBrother = D;
C->firstChild = E;
E->nextBrother = F;
// 转化为二叉树
TreeNode* binaryRoot = convertToBinaryTree(A);
// 打印二叉树
printf("Binary Tree (Preorder): ");
printBinaryTree(binaryRoot);
printf("\n");
// 释放树节点
freeTree(binaryRoot);
return 0;
}
由上述转换,可以看到,任何一个森林都对应一棵二叉树,逆转这个过程,任何一棵二叉树对应一个唯一的森林。称这个变换为森林与二叉树之间的自然对应。