主题:急!!!帮我看下我的层次遍历二叉树错在哪?
[code=c]
[/code][code=c]
[/code][code=c]
#include<stdio.h>
#include<stdlib.h>
struct node //树结构体
{
char data;
struct node *lchild,*rchild;
};
struct line //队结构体
{
struct node*ch;
struct line *next;
};
struct link //队首,队尾
{
struct line *front;
struct line *rear;
};
void createbitree(struct node*T) //建树
{
char ch;
scanf("%c",&ch);
if(ch==' ')
T=NULL;
else
{
if(!(T=(struct node*)malloc(sizeof(struct node))))
exit(0);
T->data=ch;
createbitree(T->lchild);
createbitree(T->rchild);
}
}
void initqueue( struct link*q) //初始化队
{
q->front=q->rear=(struct line*)malloc(sizeof(struct line));
if(!q->front)
exit(0);
q->front->next=NULL;
}
void enqueue( struct link*q, struct node*e) //入队
{
struct line*p;
p=(struct line*)malloc(sizeof(struct line));
p->ch=e;
p->next=NULL;
q->rear->next=p;
q->rear=p;
}
void dequeue(struct link*q,struct node*e) //出队
{
char data;
struct line*p;
p=q->front->next;
e=p->ch;
data=e->data;
q->front->next=p->next;
if(q->rear==p)
q->rear=q->front;
free(q);
printf("%c ",data);
}
int queueempty(struct link q) //判断队是否为空
{
if(q.front->next==NULL)
return 1;
else return 0;
}
void main()
{
struct link*q;
struct node*p;
truct node*T;
createbitree(T);
initqueue(q);
p=T;
enqueue(q,p);
while(queueempty(*q)!=1)
{
dequeue(q,p);
if(p->lchild!=NULL)
enqueue(q,p->lchild);
if(p->rchild!=NULL)
enqueue(q,p->rchild);
}
printf("\n");
printf("按层次遍历树中结点其输出顺序为: \n");
}
[/code][code=c]
[/code][code=c]
#include<stdio.h>
#include<stdlib.h>
struct node //树结构体
{
char data;
struct node *lchild,*rchild;
};
struct line //队结构体
{
struct node*ch;
struct line *next;
};
struct link //队首,队尾
{
struct line *front;
struct line *rear;
};
void createbitree(struct node*T) //建树
{
char ch;
scanf("%c",&ch);
if(ch==' ')
T=NULL;
else
{
if(!(T=(struct node*)malloc(sizeof(struct node))))
exit(0);
T->data=ch;
createbitree(T->lchild);
createbitree(T->rchild);
}
}
void initqueue( struct link*q) //初始化队
{
q->front=q->rear=(struct line*)malloc(sizeof(struct line));
if(!q->front)
exit(0);
q->front->next=NULL;
}
void enqueue( struct link*q, struct node*e) //入队
{
struct line*p;
p=(struct line*)malloc(sizeof(struct line));
p->ch=e;
p->next=NULL;
q->rear->next=p;
q->rear=p;
}
void dequeue(struct link*q,struct node*e) //出队
{
char data;
struct line*p;
p=q->front->next;
e=p->ch;
data=e->data;
q->front->next=p->next;
if(q->rear==p)
q->rear=q->front;
free(q);
printf("%c ",data);
}
int queueempty(struct link q) //判断队是否为空
{
if(q.front->next==NULL)
return 1;
else return 0;
}
void main()
{
struct link*q;
struct node*p;
truct node*T;
createbitree(T);
initqueue(q);
p=T;
enqueue(q,p);
while(queueempty(*q)!=1)
{
dequeue(q,p);
if(p->lchild!=NULL)
enqueue(q,p->lchild);
if(p->rchild!=NULL)
enqueue(q,p->rchild);
}
printf("\n");
printf("按层次遍历树中结点其输出顺序为: \n");
}