C++实现类模板BiTree代码,基本操作包括构造函数、递归前序遍历、递归中序遍历、递归后序遍历、层序遍历,并在主函数中验证
时间: 2023-03-13 20:40:52 浏览: 113
我可以为您提供一些参考:为实现类模板BiTree代码,首先需要实现构造函数,设置左右子树的指针,以及定义结点的数据类型。然后,实现递归前序遍历、中序遍历、后序遍历,以及层序遍历算法,在主函数中调用相应的函数,实现树的遍历。最后,在主函数中验证,确认算法的正确性。
相关问题
c语言 构造利用二叉链表存储表示的二叉树 利用先序遍历创建一个二叉树 递归计算二叉树的深度和叶子结点的个数。
好的,下面是代码实现:
```c
#include <stdio.h>
#include <stdlib.h>
// 二叉树的结构体
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);
}
}
// 递归计算二叉树深度
int GetDepth(BiTree T) {
if (!T) {
return 0;
} else {
int left_depth = GetDepth(T->lchild);
int right_depth = GetDepth(T->rchild);
return (left_depth > right_depth ? left_depth : right_depth) + 1;
}
}
// 递归计算叶子节点个数
int GetLeafCount(BiTree T) {
if (!T) {
return 0;
} else if (!T->lchild && !T->rchild) {
return 1;
} else {
return GetLeafCount(T->lchild) + GetLeafCount(T->rchild);
}
}
int main() {
BiTree T;
printf("请按先序遍历的顺序输入二叉树(空格表示空节点):\n");
CreateBiTree(&T);
printf("二叉树的深度为:%d\n", GetDepth(T));
printf("叶子节点的个数为:%d\n", GetLeafCount(T));
return 0;
}
```
以上代码中,`BiTNode`是二叉树的结构体,包括数据域`data`和左右孩子指针`lchild`和`rchild`。`CreateBiTree`函数用于先序遍历创建二叉树。`GetDepth`函数用于递归计算二叉树的深度。`GetLeafCount`函数用于递归计算叶子节点个数。在`main`函数中,先调用`CreateBiTree`函数创建二叉树,然后调用`GetDepth`和`GetLeafCount`函数计算深度和叶子节点个数并输出结果。
编写一个程序,用链式存储方式构造二叉树,实现二叉树的基本操作(假设树的元素类型为char),并在此基础上设计一个主程序完成如下功能: (1)编写CreatBiTree函数,创建二叉树T; (2)编写PreOrder函数,先序遍历二叉树 (3)编写InOrder函数,中序遍历二叉树; (4)编写PostOrder函数,后序遍历二叉树; (5)编写PostTreeDepth函数,求二叉树的深度
以下是C++代码实现:
```c++
#include <iostream>
using namespace std;
typedef struct BiNode{
char data;
struct BiNode *lchild, *rchild;
}BiNode, *BiTree;
void CreatBiTree(BiTree &T){
char ch;
cin >> ch;
if(ch == '#')
T = NULL;
else{
T = new BiNode;
T -> data = ch;
CreatBiTree(T -> lchild);
CreatBiTree(T -> rchild);
}
}
void PreOrder(BiTree T){
if(T != NULL){
cout << T -> data << " ";
PreOrder(T -> lchild);
PreOrder(T -> rchild);
}
}
void InOrder(BiTree T){
if(T != NULL){
InOrder(T -> lchild);
cout << T -> data << " ";
InOrder(T -> rchild);
}
}
void PostOrder(BiTree T){
if(T != NULL){
PostOrder(T -> lchild);
PostOrder(T -> rchild);
cout << T -> data << " ";
}
}
int PostTreeDepth(BiTree T){
if(T == NULL)
return 0;
else{
int ldepth = PostTreeDepth(T -> lchild);
int rdepth = PostTreeDepth(T -> rchild);
return max(ldepth, rdepth) + 1;
}
}
int main(){
BiTree T;
cout << "请输入二叉树(#表示空节点):" << endl;
CreatBiTree(T);
cout << "先序遍历结果为:";
PreOrder(T);
cout << endl;
cout << "中序遍历结果为:";
InOrder(T);
cout << endl;
cout << "后序遍历结果为:";
PostOrder(T);
cout << endl;
cout << "二叉树的深度为:" << PostTreeDepth(T) << endl;
return 0;
}
```
主程序实现了创建二叉树、先序遍历、中序遍历、后序遍历和求二叉树深度等基本操作。其中,创建二叉树采用递归方式,先序、中序和后序遍历都采用递归方式实现,求二叉树深度采用递归方式。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)