使用C语言编写如下程序:自定义结点结构,以二叉链表为存储结构,完成以下功能,包括定义主函数、菜单界面: 1)以先序递归方式创建一棵二叉树;2)输出二叉树的先序、中序和后序递归或非递归遍历下的结点访问次序; 3)输出二叉树所有的叶子节点和叶子节点个数; 4)输出二叉树的高度; 5)使用队列,输出二叉树的按层次遍历序列 6)输出二叉树的形状
时间: 2024-02-09 17:08:57 浏览: 38
好的,下面是使用C语言编写的程序,实现了你所需的功能:
```c
#include <stdio.h>
#include <stdlib.h>
// 自定义二叉树结点结构
typedef struct TreeNode {
int data;
struct TreeNode *left;
struct TreeNode *right;
} TreeNode;
// 创建二叉树,以先序递归方式创建
TreeNode *createTree() {
TreeNode *root = NULL;
int data;
scanf("%d", &data);
if (data != -1) {
root = (TreeNode*)malloc(sizeof(TreeNode));
root->data = data;
root->left = createTree();
root->right = createTree();
}
return root;
}
// 先序遍历递归实现
void preOrderRecursion(TreeNode *root) {
if (root == NULL) {
return;
}
printf("%d ", root->data);
preOrderRecursion(root->left);
preOrderRecursion(root->right);
}
// 中序遍历递归实现
void inOrderRecursion(TreeNode *root) {
if (root == NULL) {
return;
}
inOrderRecursion(root->left);
printf("%d ", root->data);
inOrderRecursion(root->right);
}
// 后序遍历递归实现
void postOrderRecursion(TreeNode *root) {
if (root == NULL) {
return;
}
postOrderRecursion(root->left);
postOrderRecursion(root->right);
printf("%d ", root->data);
}
// 先序遍历非递归实现,使用栈
void preOrder(TreeNode *root) {
if (root == NULL) {
return;
}
TreeNode *stack[100] = {NULL};
int top = -1;
stack[++top] = root;
while (top >= 0) {
TreeNode *node = stack[top--];
printf("%d ", node->data);
if (node->right != NULL) {
stack[++top] = node->right;
}
if (node->left != NULL) {
stack[++top] = node->left;
}
}
}
// 中序遍历非递归实现,使用栈
void inOrder(TreeNode *root) {
if (root == NULL) {
return;
}
TreeNode *stack[100] = {NULL};
int top = -1;
TreeNode *p = root;
while (p != NULL || top >= 0) {
while (p != NULL) {
stack[++top] = p;
p = p->left;
}
if (top >= 0) {
p = stack[top--];
printf("%d ", p->data);
p = p->right;
}
}
}
// 后序遍历非递归实现,使用栈
void postOrder(TreeNode *root) {
if (root == NULL) {
return;
}
TreeNode *stack1[100] = {NULL};
TreeNode *stack2[100] = {NULL};
int top1 = -1;
int top2 = -1;
stack1[++top1] = root;
while (top1 >= 0) {
TreeNode *node = stack1[top1--];
stack2[++top2] = node;
if (node->left != NULL) {
stack1[++top1] = node->left;
}
if (node->right != NULL) {
stack1[++top1] = node->right;
}
}
while (top2 >= 0) {
TreeNode *node = stack2[top2--];
printf("%d ", node->data);
}
}
// 计算二叉树的高度
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;
}
// 输出二叉树的叶子节点
void printLeaves(TreeNode *root, int *count) {
if (root == NULL) {
return;
}
if (root->left == NULL && root->right == NULL) {
printf("%d ", root->data);
(*count)++;
}
printLeaves(root->left, count);
printLeaves(root->right, count);
}
// 输出二叉树的按层次遍历序列,使用队列
void levelOrder(TreeNode *root) {
if (root == NULL) {
return;
}
TreeNode *queue[100] = {NULL};
int front = 0, rear = 0;
queue[rear++] = root;
while (front < rear) {
TreeNode *node = queue[front++];
printf("%d ", node->data);
if (node->left != NULL) {
queue[rear++] = node->left;
}
if (node->right != NULL) {
queue[rear++] = node->right;
}
}
}
// 输出二叉树的形状,使用中序遍历
void printShape(TreeNode *root) {
if (root == NULL) {
return;
}
if (root->left != NULL) {
printf("(");
printShape(root->left);
printf(")");
}
printf("%d", root->data);
if (root->right != NULL) {
printf("(");
printShape(root->right);
printf(")");
}
}
int main() {
// 创建二叉树
printf("请输入二叉树的先序遍历序列(-1表示空节点):\n");
TreeNode *root = createTree();
printf("\n");
// 输出二叉树的先序、中序、后序遍历序列
printf("二叉树的先序遍历序列(递归):");
preOrderRecursion(root);
printf("\n");
printf("二叉树的中序遍历序列(递归):");
inOrderRecursion(root);
printf("\n");
printf("二叉树的后序遍历序列(递归):");
postOrderRecursion(root);
printf("\n");
printf("二叉树的先序遍历序列(非递归):");
preOrder(root);
printf("\n");
printf("二叉树的中序遍历序列(非递归):");
inOrder(root);
printf("\n");
printf("二叉树的后序遍历序列(非递归):");
postOrder(root);
printf("\n");
// 输出二叉树的叶子节点和叶子节点个数
int count = 0;
printf("二叉树的所有叶子节点:");
printLeaves(root, &count);
printf("\n");
printf("二叉树的叶子节点个数为:%d\n", count);
// 输出二叉树的高度
printf("二叉树的高度为:%d\n", getHeight(root));
// 输出二叉树的按层次遍历序列
printf("二叉树的按层次遍历序列:");
levelOrder(root);
printf("\n");
// 输出二叉树的形状
printf("二叉树的形状为:");
printShape(root);
printf("\n");
return 0;
}
```
程序运行时,会先提示用户输入二叉树的先序遍历序列,程序将以递归方式创建二叉树。之后,程序将输出二叉树的先序、中序、后序遍历序列,以及它们的非递归实现。接着,程序将输出二叉树的叶子节点和叶子节点个数、高度、按层次遍历序列和形状。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)