用C语言写出孩子表示法的代码并实现
时间: 2024-02-12 08:04:15 浏览: 34
以下是使用 C 语言实现孩子表示法的代码:
```c
#include <stdio.h>
#include <stdlib.h>
// 树的节点结构体
typedef struct TreeNode {
int data; // 节点数据
struct TreeNode *firstChild; // 第一个孩子节点
struct TreeNode *nextSibling; // 下一个兄弟节点
} TreeNode;
// 孩子表示法的树结构体
typedef struct ChildTree {
TreeNode *root; // 根节点
} ChildTree;
// 创建一个孩子表示法的树
ChildTree *create_child_tree(int data) {
ChildTree *t = (ChildTree*)malloc(sizeof(ChildTree));
t->root = (TreeNode*)malloc(sizeof(TreeNode));
t->root->data = data;
t->root->firstChild = NULL;
t->root->nextSibling = NULL;
return t;
}
// 添加孩子节点
void add_child(TreeNode *parent, int data) {
if (parent->firstChild == NULL) {
parent->firstChild = (TreeNode*)malloc(sizeof(TreeNode));
parent->firstChild->data = data;
parent->firstChild->firstChild = NULL;
parent->firstChild->nextSibling = NULL;
} else {
TreeNode *p = parent->firstChild;
while (p->nextSibling != NULL) {
p = p->nextSibling;
}
p->nextSibling = (TreeNode*)malloc(sizeof(TreeNode));
p->nextSibling->data = data;
p->nextSibling->firstChild = NULL;
p->nextSibling->nextSibling = NULL;
}
}
// 打印树的结构
void print_tree(TreeNode *root) {
if (root == NULL) {
return;
}
printf("%d ", root->data);
print_tree(root->firstChild);
print_tree(root->nextSibling);
}
// 测试
int main() {
ChildTree *t = create_child_tree(1);
add_child(t->root, 2);
add_child(t->root, 3);
TreeNode *p = t->root->firstChild;
p->nextSibling = (TreeNode*)malloc(sizeof(TreeNode));
p->nextSibling->data = 4;
p->nextSibling->firstChild = NULL;
p->nextSibling->nextSibling = NULL;
add_child(p->nextSibling, 5);
print_tree(t->root); // 输出:1 2 3 4 5
return 0;
}
```
以上代码中,`TreeNode` 结构体表示树的节点,其中 `firstChild` 指向当前节点的第一个孩子节点,`nextSibling` 指向当前节点的下一个兄弟节点。`ChildTree` 结构体表示孩子表示法的树,其中 `root` 指向根节点。`create_child_tree` 函数用于创建一个根节点为 `data` 的树,`add_child` 函数用于向某个节点添加孩子节点,`print_tree` 函数用于打印整个树的结构。
以下是一个使用示例:
```c
ChildTree *t = create_child_tree(1);
add_child(t->root, 2);
add_child(t->root, 3);
TreeNode *p = t->root->firstChild;
p->nextSibling = (TreeNode*)malloc(sizeof(TreeNode));
p->nextSibling->data = 4;
p->nextSibling->firstChild = NULL;
p->nextSibling->nextSibling = NULL;
add_child(p->nextSibling, 5);
print_tree(t->root); // 输出:1 2 3 4 5
```
以上代码中,我们创建了一个根节点为 1 的孩子表示法的树,然后添加了两个孩子节点 2 和 3,又添加了一个孩子节点 4,这个孩子节点 4 又有一个孩子节点 5。最后我们打印了整个树的结构,输出为 `1 2 3 4 5`。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)