任务要求
问题描述:已知一个按先序序列输入的字符序列,abc,,de,g,,f,,,(其中逗号表示空结点)。请编写C程序,建立二叉树并按中序和后序方式遍历二叉树,最后求出叶子节点个数和二叉树深度。要求:
1、设计一个虚拟界面,让用户选择操作(根据提示输入数据)。
2、编写main函数和若干子函数(实现功能),建立二叉树并按中序和后序方式遍历二叉树,最后求出叶子节点个数和二叉树深度。
/****************************************************************************************
这里用多行注释创建一个内容显示区,用来说明程序的功能,便于程序员之间交流。
程序功能:编程实现单链表的创建、逆置、打印等操作。
建议:先运行程序,根据提示进行操作,再来读懂代码,特别是应该书写足够多的注释。
*****************************************************************************************/
/*##########################################################################################
*##########################################################################################*/
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
typedef char datatype;
datatype a[100];
int count=0,w;
typedef struct BiTNode
{
datatype data;
struct BiTNode *Lchild; //左子树
struct BiTNode *Rchild; //右子树
}BiTNode,*Bitree;
void printStar() //连续输入几十个*,用来设计虚拟界面的边框
{
int i;
for(i=0;i<70;i++)
printf("*");
printf("\n");
}
void menu() //因为C语言无法创建可视化界面,因此这里创建一个虚拟的界面,方便用户操作
{
printStar();
printf("\t\t\t欢迎进入二叉树操作界面!\n");
printf("请从键盘输入数字,完成对应的操作(1到5为有效操作,输入其他退出程序)\n");
printf("\t 1.将已知的先序字符序列输入建立二叉树\n");
printf("\t 2.中序遍历二叉树并输出遍历字符序列\n");
printf("\t 3.后序遍历二叉树并输出遍历字符序列\n");
printf("\t 4.求叶子结点的个数\n");
printf("\t 5.求二叉树的深度\n");
printStar();
}
Bitree creat() //建立二叉树
{
Bitree bt;
datatype ch;
ch=a[w];
w++;
if(ch==','||ch==',') //防止用户输入中英文的逗号不一致
{
return NULL;
}
else
{
//<------将此处代码补充完整
bt=(Bitree *)malloc(sizeof(BiTNode));//为新节点申请空间
bt->data = ch;
bt->Lchild=creat();//递归左子树
bt->Rchild=creat();//递归右子树
}
return bt;
}
void Inorder(Bitree bt) //中序遍历二叉树并输出遍历字符序列
{
if(bt!=NULL)
{
//<------将此处代码补充完整
Inorder(bt->Lchild);
printf("%c ",bt->data);
Inorder(bt->Rchild);
}
else return;
}
void Postorder(Bitree bt) //后序遍历二叉树并输出遍历的字符序列
{
if(bt!=NULL)
{
//<------将此处代码补充完整
Postorder(bt->Lchild);
Postorder(bt->Rchild);
printf("%c ",bt->data);
}
else return;
}
int leafcount(Bitree bt) //求叶子结点的个数
{
if(bt!=NULL)
{
//<------将此处代码补充完整
//如果我们的结点的左右指针全部都为空,那就是我们的叶子结点
if(bt->Lchild==NULL&&bt->Rchild==NULL)
{
return 1;
}
//返回我们左子树中的叶子结点和右子树中的叶子结点之和
return leafcount(bt->Lchild)+leafcount(bt->Rchild);
}
}
int Treedepth(Bitree bt) //求二叉树的深度
{
//<------将此处代码补充完整
//如果我们的根节点为空的话,我们就直接返回
if(bt==NULL)
{
return 0;
}
//如果我们找到了我们的叶子结点,我们就返回1
if(bt->Rchild==NULL&&bt->Lchild==NULL)
{
return 1;
}
//递归调用,如果我们的左子树的高度大于右子树的高度,我们返回的数据就是左子树的高度再加1
//这里的+1就是我们当前层的结点
//如果我们的右子树的高度大于我们左子树的高度,我们返回的数据就是右子树的高度再加1
return (Treedepth(bt->Lchild)>=Treedepth(bt->Rchild) ? Treedepth(bt->Lchild)+1 :Treedepth(bt->Rchild)+1);
}
void main()
{
int select,maxDepth,flag=1;
Bitree p;
p=(Bitree)malloc(sizeof(BiTNode));
menu();
while(flag==1){
printf("您选择的操作是:");
fflush(stdin);
scanf("%d",&select);
switch(select){
case 1:
printf("请输入已知的先序字符序列,如“abc,,de,g,,f,,,”:");
fflush(stdin);
gets(a); //从输入读一行字符串,并存入到数组a中
w=0;
p=creat(); //建立二叉树
printf("建立二叉树成功!\n\n");
break;
case 2:
printf("中序遍历二叉树并输出遍历的字符序列为:");
Inorder(p); //中序遍历二叉树并输出遍历字符序列
printf("\n\n");
break;
//<------将此处代码补充完整
case 3:
printf("后序遍历二叉树并输出遍历的字符序列为:");
Postorder(p);
printf("\n\n");
break;
case 4:
printf("叶子结点个数为:%d",leafcount(p));
printf("\n\n");
break;
case 5:
printf("二叉树的深度为:%d",Treedepth(p));
printf("\n\n");
break;
}
}
printf("\n");
system("pause");
}
运行结果