哪位大神知道二叉树的层次遍历,怎么一直循环
#include <stdio.h>#include <malloc.h>
#define MAXSIZE 20
typedef char datatype;
typedef struct Node{
datatype data;
struct Node *Lchild;
struct Node *Rchild;
}BT;
typedef struct {
datatype data[MAXSIZE];
int front,rear;
}Squeue;
BT *CreatBT();
/*int leafnum(BT *root);
void PrintTree(BT *root,int h);
void visit(BT *root);
void PreOrder(BT *root);
void InOrder(BT *root);
void PostOrder(BT *root);
int PostTreeDepth(BT *root);
void select(BT *root);
void f(int i,BT *root);
void save_file(int h);
int read_file();*/
void visit(BT *root);
Squeue *Initqueue();
void InSqueue(Squeue *s,datatype x);
Squeue *OutSqueue(Squeue *s,datatype *x);
int Empty_Squeue(Squeue *s);
void Findk(BT *root);
void main()
{
BT *root;
printf("请输入二叉树:\n");
root=CreatBT();
//select(root);
Findk(root);
}
BT *CreatBT() //建立二叉树
{
BT *root;
char ch;fflush(stdin);
scanf("%c",&ch);
if(ch=='#')
{
root=NULL;
}
else
{
root=(BT*)malloc(sizeof(BT));
root->data=ch;
root->Lchild=CreatBT();
root->Rchild=CreatBT();
}
return(root);
}
/*void select(BT *root)
{
int i,h,H,num=0;
printf("\n");
printf("\n");
printf("\t ************************ \n");
printf("\t 欢迎使用 \n");
printf("\t ************************ \n");
printf("\t 1----------先序遍历 \n");
printf("\t 2----------中序遍历 \n");
printf("\t 3----------后序遍历 \n");
printf("\t 4----------计算二叉树的高度 \n");
printf("\t 5----------统计叶子节点 \n");
printf("\t 6----------k层叶子总数 \n");
printf("\t 7----------打印二叉树 \n");
printf("\t 8----------结束 \n");
printf("\n");
printf("请输入选项:");
scanf("%d",&i);
switch(i){
case(1):PreOrder(root);f(i,root);break;
case(2):InOrder(root);f(i,root);break;
case(3):PostOrder(root);f(i,root);break;
case(4):h=PostTreeDepth(root);printf("二叉树的高度为%d\n",h);f(i,root);break;
case(5):num=leafnum(root);printf("二叉树的叶子节点总数为%d\n",num);f(i,root);break;
case(6):PreOrder(root);f(i,root);break;
case(7): H=read_file();PrintTree(root,H);f(i,root);break;
default:printf("结束\n");
}
}
void f(int i,BT *root)
{
if(i!=8)
{
select(root);
}
else
{
printf("结束\n");
}
}
void PreOrder(BT *root)//先序遍历
{
if(root!=NULL)
{
visit(root);
PreOrder(root->Lchild);
PreOrder(root->Rchild);
}
}
void InOrder(BT *root)//中序
{
if(root)
{
InOrder(root->Lchild);
visit(root);
InOrder(root->Rchild);
}
}
void PostOrder(BT *root)//后序
{
if(root)
{
PostOrder(root->Lchild);
PostOrder(root->Rchild);
visit(root);
}
}*/
void visit(BT *root)
{
printf("%3c",root->data);
}
/*int PostTreeDepth(BT *root)//求树的高度
{
int h1,h2,h;
if(root==NULL)
return(0);
else
{
h1=PostTreeDepth(root->Lchild);
h2=PostTreeDepth(root->Rchild);
h=h1>h2?h1:h2;
h=h+1;
save_file(h);
return(h);
}
}
void PrintTree(BT *root,int h)//打印二叉树
{
int i;
if(root==NULL)
return;
PrintTree(root->Rchild,h+1);
for(i=0;i<h;i++)
printf(" ");
printf("%2c\n",root->data);
PrintTree(root->Lchild,h+1);
}
int leafnum(BT *root)//求叶节点总数
{
int n1,n2;
if(root==NULL)
return(0);
if(root->Lchild==NULL&&root->Rchild==NULL)
return(1);
n1=leafnum(root->Lchild);
n2=leafnum(root->Rchild);
return(n1+n2);
}*/
/*void leafnum(BT *root)//求节点数目另一种方式
{
if(root!=NULL)
{
leafnum(root->Lchild);
leafnum(root->Rchild);
if(root->Lchild==NULL&&root->Rchild==NULL)
num++;
}
}*/
Squeue *Initqueue()//初始化队列
{
Squeue *s;
s=(Squeue *)malloc(sizeof(Squeue));
s->front=s->rear=MAXSIZE;
return(s);
}
void InSqueue(Squeue *s,datatype *x)//入队
{
s->rear=(s->rear+1)%MAXSIZE;
[s->rear]=x;
}
Squeue *OutSqueue(Squeue *s, datatype *x)//出队
{
s->front=(s->front+1)%MAXSIZE;
*x=s->data[s->front];
return(x);
}
int Empty_Squeue(Squeue *s)//判队空
{
if(s->front==s->rear==-1)
return(1);
else
return(0);
}
void Findk(BT *root)//寻找k层叶子总数
{
Squeue *s;
BT *p;
p=root;
s=Initqueue();
InSqueue(s,root->data);
while(!Empty_Squeue(s))
{
visit(p);
p=OutSqueue(s,p);
if(p->Lchild!=NULL)
InSqueue(s,p->Lchild->data);
if(p->Rchild!=NULL)
InSqueue(s,p->Rchild->data);
}
}
void save_file(int h)//保存文件
{
FILE *fp;
int i=1;
if((fp = fopen("data.txt","w")) == NULL) {
printf("不能打开数据文件。\n");
}
fprintf(fp,"%d",h);
fclose(fp);
}
int read_file()//读文件
{
int H;
FILE *fp;
if((fp = fopen("data.txt","r")) == NULL) {
return(-1);
}
fscanf(fp,"%d",&H);
fclose(fp);
return(H);
}
前面注释掉的不用看了,只看队列就行,蟹蟹啦