#include"stdio.h" #include"iostream.h" #include"stdlib.h" #define OK 1 #define ERROR 0 #define OVERFLOW -2 typedef int Status; typedef struct BiTNode { char data; struct BiTNode *lchild, *rchild; }BiTNode, *BiTree; //BiTree是二叉链表的数据结构,其类型是结构体指针 ///////////////////////////////////////////////////////////////////// //这个函数的功能是以先序方式建立二叉链表, void CreateBiTree(BiTree &T) { char ch; cin>>ch; if (ch=='#')T=NULL; else { T=new BiTNode; T->data=ch; CreateBiTree(T->lchild); CreateBiTree(T->rchild); } }//CreateBiTree /////////////////////////////////////////////////////////////////// //实验1:先序遍历二叉树的递归算法PreOrderTraverse //////////////////////////////////////////////////////////// ////实验2:中序遍历二叉树的递归算法InOrderTraverse //////////////////////////////////////////////////////////// //实验3:后序遍历二叉树的递归算法PostOrderTraverse ///////////////////////////////////// //实验4:统计二叉树中结点的个数NodeCount ///////////////////////////////////// //实验5:统计二叉树中叶子结点的个数LeafCount ////////////////////////////////////////// int main() { BiTree root; int i; printf("准备以先序方式创建二叉树...,\n请输入各节点数据(如果没有左、右孩子,输入空格):\n"); CreateBiTree(root); printf("\n先序遍历的结果: "); PreOrderTraverse(root); printf("\n中序遍历的结果: "); InOrderTraverse(root); printf("\n后续遍历的结果: "); PostOrderTraverse(root); printf("\n"); printf("\n该树共有%d个结点。\n",NodeCout(root)); printf("\n该树共有%d个叶子。\n",LeafCout(root)); return 0; }
时间: 2023-08-22 10:04:18 浏览: 109
这段代码是一个实现二叉树的程序,主要包括了先序、中序、后序遍历二叉树的递归算法,以及统计二叉树中结点个数和叶子结点个数的函数。其中,CreateBiTree函数用于以先序方式建立二叉链表,PreOrderTraverse函数用于先序遍历二叉树,InOrderTraverse函数用于中序遍历二叉树,PostOrderTraverse函数用于后序遍历二叉树,NodeCount函数用于统计二叉树中结点的个数,LeafCount函数用于统计二叉树中叶子结点的个数。该程序主函数中通过用户输入数据来创建二叉树,然后对该二叉树进行各种遍历和统计操作,并输出结果。
相关问题
优化这段代码#include<stdio.h> #include<stdlib.h> #include<malloc.h> #include<conio.h> #define OK 1 #define error 0 #define MVNum 100 #define MAXSIZE 10 typedef int OtherInfo,QElemtype; typedef char VerTexType; //结构体定义 typedef struct ArcNode{ int adjvex;
首先,该段代码中包含了一些不必要的头文件,例如:conio.h,malloc.h等。建议只保留stdio.h和stdlib.h。
其次,可以将#define OK 1和#define error 0替换为枚举类型,使代码更加清晰易读。
最后,在结构体定义中,可以将OtherInfo和QElemtype合并为一个类型,避免定义过多类型。同时,也可以将结构体中的变量类型进行优化,例如将int类型的adjvex改为short或者char类型。
重构后的代码如下所示:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_VERTEX_NUM 100
#define MAX_ARC_NUM 10
typedef enum {
ERROR = 0,
OK = 1
} Status;
typedef char VertexType;
typedef int ArcType;
typedef struct ArcNode {
int adjvex;
struct ArcNode* nextarc;
ArcType* info;
} ArcNode;
typedef struct VNode {
VertexType data;
ArcNode* firstarc;
} VNode, AdjList[MAX_VERTEX_NUM];
typedef struct {
AdjList vertices;
int vexnum, arcnum;
} ALGraph;
```
希望可以帮到你。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![text/x-c](https://img-home.csdnimg.cn/images/20250102104920.png)
![application/x-gzip](https://img-home.csdnimg.cn/images/20250102104920.png)
![text/x-c](https://img-home.csdnimg.cn/images/20250102104920.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![exe](https://img-home.csdnimg.cn/images/20241231044909.png)