二叉树层次遍历问题,高手帮忙啊~

来源:百度知道 编辑:UC知道 时间:2024/07/08 00:34:33
建立包含10个结点的二叉树,完成二叉树从左至右,从上至下层次遍历程序。哪位高手帮忙把程序写出来啊~自己写的老出错,运行不了啊

#include<stdio.h>
#include<malloc.h>
//二叉链表的结构类型定义
const int maxsize=1024;
typedef char datatype;
typedef struct node
{
datatype data;
struct node *lchild,*rchild;
}bitree;

bitree*creattree();
void preorder(bitree*);

void main()
{
bitree*pb;
pb=creattree();
preorder(pb);
printf("\n");
}

//二叉树的建立
bitree*creattree()
{
char ch;
bitree*Q[maxsize];
int front,rear;
bitree*root,*s;
root=NULL;
front=1;rear=0;
printf("按层次输入二叉树,虚结点输入'@',以'#'结束输入:\n");
while((ch=getchar())!='#')
{
s=NULL;
if(ch!='@')
{
s=(bitree*)malloc(sizeof(bitree));
s->data=ch;
s->lchild=NULL;
s->rchild=NULL;
}
rear++;
Q[rear]=s;
if(rear==1)root=s;
else
{
if(s&&Q