用c语言递归的方法实现以下算法: 1.以二叉链表表示二叉树,建立一棵二叉树(算法5.3); 2.输出二叉树的中序遍历结果(算法5.1); 3.输出二叉树的前序遍历结果(见讲稿); 4.输出二叉树的后序遍历结果(见讲稿); 5.计算二叉树的深度(算法5.5); 6.统计二叉树的结点个数(算法5.6); 7.统计二叉树的叶结点个数; 8.统计二叉树的度为1的结点个数;
时间: 2023-03-21 19:03:11 浏览: 334
好的,以下是您所提出的问题的回答:
1. 二叉树的建立算法,使用C语言递归实现:
```c
typedef struct BiTNode {
char data;
struct BiTNode *lchild, *rchild;
} BiTNode, *BiTree;
void CreateBiTree(BiTree *T) {
char c;
scanf("%c", &c);
if (c == '#') {
*T = NULL;
} else {
*T = (BiTree)malloc(sizeof(BiTNode));
(*T)->data = c;
CreateBiTree(&(*T)->lchild);
CreateBiTree(&(*T)->rchild);
}
}
```
2. 二叉树的中序遍历算法,使用C语言递归实现:
```c
void InOrderTraverse(BiTree T) {
if (T) {
InOrderTraverse(T->lchild);
printf("%c", T->data);
InOrderTraverse(T->rchild);
}
}
```
3. 二叉树的前序遍历算法,使用C语言递归实现:
```c
void PreOrderTraverse(BiTree T) {
if (T) {
printf("%c", T->data);
PreOrderTraverse(T->lchild);
PreOrderTraverse(T->rchild);
}
}
```
4. 二叉树的后序遍历算法,使用C语言递归实现:
```c
void PostOrderTraverse(BiTree T) {
if (T) {
PostOrderTraverse(T->lchild);
PostOrderTraverse(T->rchild);
printf("%c", T->data);
}
}
```
5. 二叉树的深度算法,使用C语言递归实现:
```c
int TreeDepth(BiTree T) {
if (!T) {
return 0;
} else {
int leftDepth = TreeDepth(T->lchild);
int rightDepth = TreeDepth(T->rchild);
return leftDepth > rightDepth ? leftDepth + 1 : rightDepth + 1;
}
}
```
6. 二叉树的结点个数算法,使用C语言递归实现:
```c
int NodeCount(BiTree T) {
if (!T) {
return 0;
} else {
int leftCount = NodeCount(T->lchild);
int rightCount = NodeCount(T->rchild);
return leftCount + rightCount + 1;
}
}
```
7. 统计二叉树的叶结点个数,使用C语言递归实现:
```c
int LeafCount(BiTree T) {
if (!T) {
return 0;
} else if (!T->lchild && !T->rchild) {
return 1;
} else {
int leftCount = LeafCount(T->lchild);
int rightCount = LeafCount(T->rchild);
return leftCount + rightCount;
}
}
```
8. 统计二叉树的度为1的结点个数,使用C语言递归实现:
```c
int OneDegreeCount(BiTree T) {
阅读全文
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![cpp](https://img-home.csdnimg.cn/images/20250102104920.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)