用c语言编写一个程序,实现功能具体如下:1.以二叉链表表示二叉树,建立一棵二叉树:2.输出二叉树的中序遍历结果:3.输出二叉树的前序遍历结果:4.输出二叉树的后序遍历结果:5.计算二叉树的深度;6.统计二叉树
时间: 2024-06-18 21:06:09 浏览: 144
你好,用C语言编写一个程序,实现以上功能需要进行如下步骤:
1. 定义二叉树的结构体,包括左右子树指针和节点值等信息。
2. 编写函数实现二叉树的建立,可以采用递归或非递归方式进行建立。
3. 编写函数实现二叉树的中序遍历,可以采用递归或非递归方式进行遍历。
4. 编写函数实现二叉树的前序遍历,可以采用递归或非递归方式进行遍历。
5. 编写函数实现二叉树的后序遍历,可以采用递归或非递归方式进行遍历。
6. 编写函数实现计算二叉树的深度,可以采用递归方式进行计算。
7. 编写函数实现统计二叉树节点数,可以采用递归方式进行统计。
以下是示例代码:
```
#include <stdio.h>
#include <stdlib.h>
// 定义二叉树结构体
typedef struct TreeNode {
int val;
struct TreeNode* left;
struct TreeNode* right;
} TreeNode;
// 建立二叉树
void createTree(TreeNode** root) {
int val;
scanf("%d", &val);
if (val == -1) {
*root = NULL;
} else {
*root = (TreeNode*)malloc(sizeof(TreeNode));
(*root)->val = val;
createTree(&(*root)->left);
createTree(&(*root)->right);
}
}
// 中序遍历
void inorderTraversal(TreeNode* root) {
if (root == NULL) {
return;
}
inorderTraversal(root->left);
printf("%d ", root->val);
inorderTraversal(root->right);
}
// 前序遍历
void preorderTraversal(TreeNode* root) {
if (root == NULL) {
return;
}
printf("%d ", root->val);
preorderTraversal(root->left);
preorderTraversal(root->right);
}
// 后序遍历
void postorderTraversal(TreeNode* root) {
if (root == NULL) {
return;
}
postorderTraversal(root->left);
postorderTraversal(root->right);
printf("%d ", root->val);
}
// 计算深度
int maxDepth(TreeNode* root) {
if (root == NULL) {
return 0;
}
int leftDepth = maxDepth(root->left);
int rightDepth = maxDepth(root->right);
return (leftDepth > rightDepth) ? leftDepth + 1 : rightDepth + 1;
}
// 统计节点数
int countNodes(TreeNode* root) {
if (root == NULL) {
return 0;
}
return countNodes(root->left) + countNodes(root->right) + 1;
}
int main() {
TreeNode* root;
// 建立二叉树
createTree(&root);
// 中序遍历
printf("Inorder Traversal: ");
inorderTraversal(root);
printf("\n");
// 前序遍历
printf("Preorder Traversal: ");
preorderTraversal(root);
printf("\n");
// 后序遍历
printf("Postorder Traversal: ");
postorderTraversal(root);
printf("\n");
// 计算深度
int depth = maxDepth(root);
printf("Depth of the tree is: %d\n", depth);
// 统计节点数
int count = countNodes(root);
printf("Number of nodes in the tree is: %d\n", count);
return 0;
}
```
阅读全文
相关推荐
![](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)