# include <stdio.h>
# include <malloc.h>
typedef struct tree
{
int data;
struct tree* lchild;
struct tree* rchild;
}Tree,*Ptree;
typedef Ptree element;//这一步太重要了,分割了树和树的地址
typedef struct queen
{
element* base;
int f,r;
}Queen,*Pqueen;
void init (Pqueen Q);
void enqueen(Pqueen Q,Ptree T);
Ptree outqueen(Pqueen Q);
bool isempty(Pqueen Q);
void travel(Ptree T);
void main()
{
Ptree a=(Ptree)malloc(sizeof(Tree));
Ptree b=(Ptree)malloc(sizeof(Tree));
Ptree c=(Ptree)malloc(sizeof(Tree));
Ptree d=(Ptree)malloc(sizeof(Tree));
Ptree e=(Ptree)malloc(sizeof(Tree));
a->data=8;
b->data=1;
c->data=2;
d->data=6;
e->data=5;
a->lchild=b;
a->rchild=c;
b->lchild=NULL;
b->rchild=d;
c->lchild=e;
c->rchild=NULL;
d->lchild=NULL;
d->rchild=NULL;
e->lchild=NULL;
e->rchild=NULL;
travel(a);
}
void init (Pqueen Q)
{
Q->base=(element*)malloc(sizeof(element)*6);
Q->f=0;
Q->r=0;
}
void enqueen(Pqueen Q,Ptree T)
{
Q->base[Q->r]=T;
Q->r=(Q->r+1)%6;
}
Ptree outqueen(Pqueen Q)
{
Ptree T=Q->base[Q->f];
Q->f=(Q->f+1)%6;
return T;
}
bool isempty(Pqueen Q)
{
if(Q->f==Q->r)
return true;
else
return false;
}
void travel(Ptree T)
{
Queen Q;
init(&Q);
Ptree p;
enqueen(&Q,T);
while(!isempty(&Q))
{
p=outqueen(&Q);
printf("%d\n",p->data);
if(p->lchild!=NULL)
enqueen(&Q,p->lchild);
if(p->rchild!=NULL)
enqueen(&Q,p->rchild);
}
}
# include <malloc.h>
typedef struct tree
{
int data;
struct tree* lchild;
struct tree* rchild;
}Tree,*Ptree;
typedef Ptree element;//这一步太重要了,分割了树和树的地址
typedef struct queen
{
element* base;
int f,r;
}Queen,*Pqueen;
void init (Pqueen Q);
void enqueen(Pqueen Q,Ptree T);
Ptree outqueen(Pqueen Q);
bool isempty(Pqueen Q);
void travel(Ptree T);
void main()
{
Ptree a=(Ptree)malloc(sizeof(Tree));
Ptree b=(Ptree)malloc(sizeof(Tree));
Ptree c=(Ptree)malloc(sizeof(Tree));
Ptree d=(Ptree)malloc(sizeof(Tree));
Ptree e=(Ptree)malloc(sizeof(Tree));
a->data=8;
b->data=1;
c->data=2;
d->data=6;
e->data=5;
a->lchild=b;
a->rchild=c;
b->lchild=NULL;
b->rchild=d;
c->lchild=e;
c->rchild=NULL;
d->lchild=NULL;
d->rchild=NULL;
e->lchild=NULL;
e->rchild=NULL;
travel(a);
}
void init (Pqueen Q)
{
Q->base=(element*)malloc(sizeof(element)*6);
Q->f=0;
Q->r=0;
}
void enqueen(Pqueen Q,Ptree T)
{
Q->base[Q->r]=T;
Q->r=(Q->r+1)%6;
}
Ptree outqueen(Pqueen Q)
{
Ptree T=Q->base[Q->f];
Q->f=(Q->f+1)%6;
return T;
}
bool isempty(Pqueen Q)
{
if(Q->f==Q->r)
return true;
else
return false;
}
void travel(Ptree T)
{
Queen Q;
init(&Q);
Ptree p;
enqueen(&Q,T);
while(!isempty(&Q))
{
p=outqueen(&Q);
printf("%d\n",p->data);
if(p->lchild!=NULL)
enqueen(&Q,p->lchild);
if(p->rchild!=NULL)
enqueen(&Q,p->rchild);
}
}