用C实现1.编写函数,输入字符序列,建立二叉树的二叉链表 2.编写函数,实现二叉树的中序递归遍历算法。 3.编写函数,实现二叉树的中序非递归遍历算法 4.编写函数,借助队列实现二叉树的层次遍历算法 5.编写函数,求二叉树的高度 6.编写函数,求二叉树的结点个数 7.编写函数,求二叉树的叶子个数 8.编写函数,交换二叉树每个结点的左子树和右子树 9.编写一个主函数,在主函数中设计一个简单的菜单,分别调试上述算法

时间: 2023-06-25 11:04:04 浏览: 44
1. 输入字符序列,建立二叉树的二叉链表的函数实现: ```c #include <stdio.h> #include <stdlib.h> typedef struct TreeNode { char data; struct TreeNode *left; struct TreeNode *right; } TreeNode, *Tree; // 递归建立二叉树 Tree createTree() { char c; scanf("%c", &c); if (c == '#') { return NULL; } else { Tree root = (Tree)malloc(sizeof(TreeNode)); root->data = c; root->left = createTree(); root->right = createTree(); return root; } } ``` 2. 实现二叉树的中序递归遍历算法的函数实现: ```c void inorderTraversal(Tree root) { if (root != NULL) { inorderTraversal(root->left); printf("%c ", root->data); inorderTraversal(root->right); } } ``` 3. 实现二叉树的中序非递归遍历算法的函数实现: ```c #include <stdbool.h> #include <stddef.h> #define MAX_SIZE 100 typedef struct Stack { Tree data[MAX_SIZE]; int top; } Stack; void initStack(Stack *s) { s->top = -1; } bool isEmpty(Stack *s) { return s->top == -1; } bool isFull(Stack *s) { return s->top == MAX_SIZE - 1; } void push(Stack *s, Tree t) { if (!isFull(s)) { s->data[++s->top] = t; } } Tree pop(Stack *s) { if (!isEmpty(s)) { return s->data[s->top--]; } return NULL; } void inorderTraversalWithoutRecursion(Tree root) { Stack s; initStack(&s); Tree p = root; while (p != NULL || !isEmpty(&s)) { while (p != NULL) { push(&s, p); p = p->left; } if (!isEmpty(&s)) { p = pop(&s); printf("%c ", p->data); p = p->right; } } } ``` 4. 借助队列实现二叉树的层次遍历算法的函数实现: ```c #include <stdbool.h> #include <stddef.h> #define MAX_SIZE 100 typedef struct Queue { Tree data[MAX_SIZE]; int front, rear; } Queue; void initQueue(Queue *q) { q->front = q->rear = 0; } bool isEmpty(Queue *q) { return q->front == q->rear; } bool isFull(Queue *q) { return (q->rear + 1) % MAX_SIZE == q->front; } void enqueue(Queue *q, Tree t) { if (!isFull(q)) { q->data[q->rear] = t; q->rear = (q->rear + 1) % MAX_SIZE; } } Tree dequeue(Queue *q) { if (!isEmpty(q)) { Tree t = q->data[q->front]; q->front = (q->front + 1) % MAX_SIZE; return t; } return NULL; } void levelOrderTraversal(Tree root) { Queue q; initQueue(&q); if (root != NULL) { enqueue(&q, root); } while (!isEmpty(&q)) { Tree t = dequeue(&q); printf("%c ", t->data); if (t->left != NULL) { enqueue(&q, t->left); } if (t->right != NULL) { enqueue(&q, t->right); } } } ``` 5. 求二叉树的高度的函数实现: ```c int treeHeight(Tree root) { if (root == NULL) { return 0; } else { int leftHeight = treeHeight(root->left); int rightHeight = treeHeight(root->right); return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1; } } ``` 6. 求二叉树的结点个数的函数实现: ```c int countNodes(Tree root) { if (root == NULL) { return 0; } else { return countNodes(root->left) + countNodes(root->right) + 1; } } ``` 7. 求二叉树的叶子个数的函数实现: ```c int countLeaves(Tree root) { if (root == NULL) { return 0; } else if (root->left == NULL && root->right == NULL) { return 1; } else { return countLeaves(root->left) + countLeaves(root->right); } } ``` 8. 交换二叉树每个结点的左子树和右子树的函数实现: ```c void swapChild(Tree root) { if (root != NULL) { Tree tmp = root->left; root->left = root->right; root->right = tmp; swapChild(root->left); swapChild(root->right); } } ``` 9. 一个简单的菜单程序的实现: ```c int main() { Tree root = createTree(); int choice = 0; do { printf("\n1. 中序递归遍历\n"); printf("2. 中序非递归遍历\n"); printf("3. 层次遍历\n"); printf("4. 求高度\n"); printf("5. 求结点个数\n"); printf("6. 求叶子个数\n"); printf("7. 交换子树\n"); printf("0. 退出\n"); printf("请选择操作:"); scanf("%d", &choice); switch (choice) { case 1: inorderTraversal(root); break; case 2: inorderTraversalWithoutRecursion(root); break; case 3: levelOrderTraversal(root); break; case 4: printf("二叉树的高度为:%d\n", treeHeight(root)); break; case 5: printf("二叉树的结点个数为:%d\n", countNodes(root)); break; case 6: printf("二叉树的叶子个数为:%d\n", countLeaves(root)); break; case 7: swapChild(root); break; case 0: break; default: printf("输入错误,请重新选择!\n"); break; } } while (choice != 0); return 0; } ```

相关推荐

最新推荐

recommend-type

数据结构 建立二叉树二叉链表存储结构实现有关操作 实验报告

建立二叉树的二叉链表存储结构实现以下操作(选择其中的两个做) (1)输出二叉树 (2)先序遍历二叉树 (3) 中序遍历二叉树 (4)后序遍历二叉树 (5)层次遍历二叉树
recommend-type

C++ 数据结构二叉树(前序/中序/后序递归、非递归遍历)

主要介绍了C++ 数据结构二叉树(前序/中序/后序递归、非递归遍历)的相关资料,这里提供实例代码来帮助大家理解掌握二叉树,需要的朋友可以参考下
recommend-type

数据结构综合课设二叉树的建立与遍历.docx

从键盘接受输入(先序),以二叉链表作为存储结构,建立二叉树(以先序来建立),并采用递归算法对其进行遍历(先序、中序、后序),将遍历结果打印输出。 3.测试要求: ABCффDEфGффFффф(其中ф表示空格...
recommend-type

六首页数字藏品NFT交易网React NextJS网站模板 六首页数字藏品nft交易网反应NextJS网站模板

六首页数字藏品NFT交易网React NextJS网站模板 六首页数字藏品nft交易网反应NextJS网站模板
recommend-type

wireshark安装教程入门

wireshark安装教程入门
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。