1.设计程序,利用二叉链表结构,建立一棵二叉树。并能递归实现二叉树的先序遍历、中序遍历和后序遍历三种遍历算法,能用队列实现二叉树的层次遍历算法,并按层次输出(标出层号),并能统计树叶数,结点数,层高等信息。用C语言实现
时间: 2023-10-09 12:06:52 浏览: 84
好的,以下是用C语言实现建立二叉树、遍历二叉树、层次遍历二叉树等功能的代码:
```
#include <stdio.h>
#include <stdlib.h>
// 二叉树结点的结构体
typedef struct TreeNode {
int data; // 结点的值
struct TreeNode* left; // 左子树指针
struct TreeNode* right; // 右子树指针
} TreeNode;
// 创建一个新的结点
TreeNode* createNode(int data) {
TreeNode* node = (TreeNode*) malloc(sizeof(TreeNode));
node->data = data;
node->left = NULL;
node->right = NULL;
return node;
}
// 递归先序遍历二叉树
void preorderTraversal(TreeNode* root) {
if (root == NULL) {
return;
}
printf("%d ", root->data);
preorderTraversal(root->left);
preorderTraversal(root->right);
}
// 递归中序遍历二叉树
void inorderTraversal(TreeNode* root) {
if (root == NULL) {
return;
}
inorderTraversal(root->left);
printf("%d ", root->data);
inorderTraversal(root->right);
}
// 递归后序遍历二叉树
void postorderTraversal(TreeNode* root) {
if (root == NULL) {
return;
}
postorderTraversal(root->left);
postorderTraversal(root->right);
printf("%d ", root->data);
}
// 层次遍历二叉树
void levelTraversal(TreeNode* root) {
if (root == NULL) {
return;
}
// 创建一个队列并将根结点入队
TreeNode* queue[100];
int front = 0, rear = 0;
queue[rear++] = root;
int level = 1; // 记录当前结点的层次
while (front != rear) {
int size = rear - front;
printf("Level %d: ", level);
while (size--) {
TreeNode* node = queue[front++];
printf("%d ", node->data);
if (node->left != NULL) {
queue[rear++] = node->left;
}
if (node->right != NULL) {
queue[rear++] = node->right;
}
}
printf("\n");
level++;
}
}
// 统计二叉树的叶子结点数
int countLeaves(TreeNode* root) {
if (root == NULL) {
return 0;
}
if (root->left == NULL && root->right == NULL) {
return 1;
}
int leftCount = countLeaves(root->left);
int rightCount = countLeaves(root->right);
return leftCount + rightCount;
}
// 统计二叉树的结点数
int countNodes(TreeNode* root) {
if (root == NULL) {
return 0;
}
int leftCount = countNodes(root->left);
int rightCount = countNodes(root->right);
return leftCount + rightCount + 1;
}
// 计算二叉树的层高
int getHeight(TreeNode* root) {
if (root == NULL) {
return 0;
}
int leftHeight = getHeight(root->left);
int rightHeight = getHeight(root->right);
return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;
}
int main() {
// 创建一棵二叉树
TreeNode* root = createNode(1);
root->left = createNode(2);
root->right = createNode(3);
root->left->left = createNode(4);
root->left->right = createNode(5);
root->right->left = createNode(6);
root->right->right = createNode(7);
// 先序遍历
printf("先序遍历:");
preorderTraversal(root);
printf("\n");
// 中序遍历
printf("中序遍历:");
inorderTraversal(root);
printf("\n");
// 后序遍历
printf("后序遍历:");
postorderTraversal(root);
printf("\n");
// 层次遍历
printf("层次遍历:\n");
levelTraversal(root);
// 统计叶子结点数
printf("叶子结点数:%d\n", countLeaves(root));
// 统计结点数
printf("结点数:%d\n", countNodes(root));
// 计算层高
printf("层高:%d\n", getHeight(root));
return 0;
}
```
运行以上代码,输出如下:
```
先序遍历:1 2 4 5 3 6 7
中序遍历:4 2 5 1 6 3 7
后序遍历:4 5 2 6 7 3 1
层次遍历:
Level 1: 1
Level 2: 2 3
Level 3: 4 5 6 7
叶子结点数:4
结点数:7
层高:3
```
阅读全文