C语言实现采用链式存储结构建立二叉树,并按先序输入二叉树的节点序列,实现二叉树的建立、先序遍历、中序遍历、后序遍历,主函数中可设计一个选项菜单,可选择执行建立二叉树,先序、中序、后序遍历二叉树.
时间: 2024-04-08 09:31:00 浏览: 49
下面是一个使用链式存储结构建立二叉树,并实现先序、中序和后序遍历的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* left;
struct Node* right;
};
// 创建新节点
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("内存分配失败!\n");
exit(1);
}
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
// 建立二叉树
struct Node* buildTree() {
int data;
struct Node* root = NULL;
printf("请输入节点的值(输入-1表示空节点):");
scanf("%d", &data);
if (data == -1) {
return NULL;
} else {
root = createNode(data);
printf("请输入节点 %d 的左子树:\n", data);
root->left = buildTree();
printf("请输入节点 %d 的右子树:\n", data);
root->right = buildTree();
return root;
}
}
// 先序遍历
void preOrder(struct Node* root) {
if (root != NULL) {
printf("%d ", root->data);
preOrder(root->left);
preOrder(root->right);
}
}
// 中序遍历
void inOrder(struct Node* root) {
if (root != NULL) {
inOrder(root->left);
printf("%d ", root->data);
inOrder(root->right);
}
}
// 后序遍历
void postOrder(struct Node* root) {
if (root != NULL) {
postOrder(root->left);
postOrder(root->right);
printf("%d ", root->data);
}
}
int main() {
struct Node* root = NULL;
int choice;
while (1) {
printf("\n---- 选项菜单 ----\n");
printf("1. 建立二叉树\n");
printf("2. 先序遍历\n");
printf("3. 中序遍历\n");
printf("4. 后序遍历\n");
printf("0. 退出\n");
printf("请选择操作:");
scanf("%d", &choice);
switch (choice) {
case 1:
root = buildTree();
break;
case 2:
printf("先序遍历结果:");
preOrder(root);
printf("\n");
break;
case 3:
printf("中序遍历结果:");
inOrder(root);
printf("\n");
break;
case 4:
printf("后序遍历结果:");
postOrder(root);
printf("\n");
break;
case 0:
printf("程序已退出。\n");
exit(0);
default:
printf("无效的选项,请重新选择。\n");
break;
}
}
return 0;
}
```
在主函数中,通过一个选项菜单,可以选择执行建立二叉树、先序遍历、中序遍历或后序遍历操作。建立二叉树时,按照先序输入二叉树的节点序列,输入-1表示空节点。然后,根据选择的操作,对建立的二叉树进行相应的遍历。你可以根据需要修改菜单选项及节点数据,然后编译运行该代码。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![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)