#include<stdio.h>
#include<stdlib.h>
typedef char TElemType;
typedef int Status;
#define MAXSIZE 100
#define OK 1
#define ERROR 0
#define OVERFLOW -1
#define TRUE 1
#define FALSE 0
typedef struct BiNode
{
TElemType data;
struct BiNode *lchild,*rchild;
} BiNode,*BiTree;
typedef struct QueueNode
{
BiTree data;
struct QueueNode *next;
} QueueNode,*pQueueNode;
typedef struct Queue
{
pQueueNode front;
pQueueNode rear;
} Queue,*pQueue;
Status CreatTree(BiTree &T)
{
TElemType ch;
scanf("%c",&ch);
if(ch=='*')
T=NULL;
else
{
T=(BiTree)malloc(sizeof(BiNode));
if(!T)
exit(OVERFLOW);
T->data=ch;
CreatTree(T->lchild);
CreatTree(T->rchild);
}
return OK;
}
pQueue InitQueue(pQueue Q)
{
Q->front=(pQueueNode)malloc(sizeof(QueueNode));
Q->front->next=NULL;
Q->rear=Q->front;
return Q;
}
void EnQueue(pQueue Q,BiTree t)
{
pQueueNode pNew=(pQueueNode)malloc(sizeof(QueueNode));
pNew->data=t;
pNew->next=NULL;
Q->rear->next=pNew;
Q->rear=pNew;
}
BiTree DeQueue(pQueue Q)
{
BiTree x;
pQueueNode pTemp;
pTemp=Q->front->next;
x=pTemp->data;
Q->front->next=pTemp->next;
if(Q->rear==pTemp)
Q->rear=Q->front;
free(pTemp);
return x;
}
Status QueueEmpty(pQueue Q)
{
if(Q->front->next==NULL)
return TRUE;
else
return FALSE;
}
int LevelOrder(BiTree T)
{
pQueue Q=(pQueue)malloc(sizeof(Queue));
BiTree root;
if(!T)
return 0;
Q=InitQueue(Q);
EnQueue(Q,T);
while(!QueueEmpty(Q))
{
root=DeQueue(Q);
printf("%c",root->data);
if(root->lchild)
EnQueue(Q,root->lchild);
if(root->rchild)
EnQueue(Q,root->rchild);
}
}
int main()
{
BiTree T;
CreatTree(T);
LevelOrder(T);
return 0;
}