#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 09:04:18 浏览: 121
这段代码是一个实现二叉树的程序,主要包括了先序、中序、后序遍历二叉树的递归算法,以及统计二叉树中结点个数和叶子结点个数的函数。其中,CreateBiTree函数用于以先序方式建立二叉链表,PreOrderTraverse函数用于先序遍历二叉树,InOrderTraverse函数用于中序遍历二叉树,PostOrderTraverse函数用于后序遍历二叉树,NodeCount函数用于统计二叉树中结点的个数,LeafCount函数用于统计二叉树中叶子结点的个数。该程序主函数中通过用户输入数据来创建二叉树,然后对该二叉树进行各种遍历和统计操作,并输出结果。
相关问题
#include <stdio.h> #include <stdlib.h> #define MAXSIZE 100 typedef int ElemType; typedef struct
这段代码是C语言中的头文件引用和一些定义的示例。其中,`#include <stdio.h>`和`#include <stdlib.h>`是用来引入标准库函数的头文件。`stdio.h`包含了输入输出相关的函数,如`printf`和`scanf`;`stdlib.h`包含了一些常用的函数,如内存分配函数`malloc`和随机数生成函数`rand`。
`#define MAXSIZE 100`是一个宏定义,用来定义一个常量`MAXSIZE`,它的值为100。
`typedef int ElemType;`是用来给`int`类型起一个别名`ElemType`,方便在代码中使用。
至于代码中的结构体部分,由于你没有提供完整的代码,所以无法给出具体的解释。但一般来说,结构体用于定义一种自定义的数据类型,可以包含多个不同类型的成员变量。
补全#include<stdio.h> #include<malloc.h> #define MaxSize 50 typedef int ElemType; typedef struct { Elem
#include<stdio.h>
#include<malloc.h>
#define MaxSize 50
typedef int ElemType;
typedef struct
{
ElemType data[MaxSize];
int length;
}SqList;
这段代码是一个C语言的头文件补全,包含了stdio.h和malloc.h两个头文件,并定义了一个常量MaxSize为50。同时,定义了一个类型别名ElemType为int,并声明了一个结构体SqList,其中包含了一个数组data和一个整型变量length。
阅读全文
相关推荐















