#include <iostream>
using namespace std;
struct BitNode
{
char data;
BitNode *lchild;
BitNode *rchild;
};
struct LinkNode
{
BitNode *data;
LinkNode *next;
};
struct LinkQueue
{
LinkNode *front;
LinkNode *rear;
};
void init(LinkQueue *&Q)
{
Q=new LinkQueue;
Q->front=new LinkNode;
Q->front->next=NULL;
Q->rear=Q->front;
}
bool isEmpty(LinkQueue *Q)
{
return Q->rear==Q->front;
}
void push(LinkQueue *&Q,BitNode *e)
{
LinkNode *p=new LinkNode;
p->data=e;
p->next=NULL;
Q->rear->next=p;
Q->rear=p;
}
void pop(LinkQueue *&Q)
{
if(!isEmpty(Q))
{
LinkNode *p=Q->front->next;
Q->front->next=p->next;
if(Q->rear==p)
Q->rear=Q->front;
delete p;
}
}
BitNode *get(LinkQueue *Q)
{
if(!isEmpty(Q))
return Q->front->next->data;
}
void create(BitNode *&T)
{
char ch;
cin>>ch;
if(ch=='#')
T=NULL;
else
{
T=new BitNode;
T->data=ch;
create(T->lchild);
create(T->rchild);
}
}
void proOrder(BitNode *T)
{
if(T)
{
cout<<T->data<<" ";
proOrder(T->lchild);
proOrder(T->rchild);
}
}
void levelOrder(BitNode *T)
{
LinkQueue *Q;
init(Q);
push(Q,T);
while(!isEmpty(Q))
{
BitNode *t=get(Q);
cout<<t->data<<" ";
pop(Q);
if(t->lchild)
push(Q,t->lchild);
if(t->rchild)
push(Q,t->rchild);
}
cout<<endl;
}
int main()
{
LinkQueue *Q;
init(Q);
BitNode *T;
create(T);
cout<<"层序遍历二叉树:"<<endl;
levelOrder(T);
return 0;
}
using namespace std;
struct BitNode
{
char data;
BitNode *lchild;
BitNode *rchild;
};
struct LinkNode
{
BitNode *data;
LinkNode *next;
};
struct LinkQueue
{
LinkNode *front;
LinkNode *rear;
};
void init(LinkQueue *&Q)
{
Q=new LinkQueue;
Q->front=new LinkNode;
Q->front->next=NULL;
Q->rear=Q->front;
}
bool isEmpty(LinkQueue *Q)
{
return Q->rear==Q->front;
}
void push(LinkQueue *&Q,BitNode *e)
{
LinkNode *p=new LinkNode;
p->data=e;
p->next=NULL;
Q->rear->next=p;
Q->rear=p;
}
void pop(LinkQueue *&Q)
{
if(!isEmpty(Q))
{
LinkNode *p=Q->front->next;
Q->front->next=p->next;
if(Q->rear==p)
Q->rear=Q->front;
delete p;
}
}
BitNode *get(LinkQueue *Q)
{
if(!isEmpty(Q))
return Q->front->next->data;
}
void create(BitNode *&T)
{
char ch;
cin>>ch;
if(ch=='#')
T=NULL;
else
{
T=new BitNode;
T->data=ch;
create(T->lchild);
create(T->rchild);
}
}
void proOrder(BitNode *T)
{
if(T)
{
cout<<T->data<<" ";
proOrder(T->lchild);
proOrder(T->rchild);
}
}
void levelOrder(BitNode *T)
{
LinkQueue *Q;
init(Q);
push(Q,T);
while(!isEmpty(Q))
{
BitNode *t=get(Q);
cout<<t->data<<" ";
pop(Q);
if(t->lchild)
push(Q,t->lchild);
if(t->rchild)
push(Q,t->rchild);
}
cout<<endl;
}
int main()
{
LinkQueue *Q;
init(Q);
BitNode *T;
create(T);
cout<<"层序遍历二叉树:"<<endl;
levelOrder(T);
return 0;
}