광도 우선 두 갈래 나무 주유
4907 단어 광도 우선 두 갈래 나무 주유
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 20
#define OK 1
#define ERROR 0
typedef int Status;
typedef char TElemType;
//
typedef struct BiTNode
{
TElemType data;
struct BiTNode * lchild;
struct BiTNode * rchild;
}BiTNode,*BiTree;
typedef BiTree QElemtype;
//
typedef struct
{
QElemtype data[MAXSIZE];
int front;
int rear;
}sqQueue;
//
Status InitQueue(sqQueue * Q)
{
Q->front=0;
Q->rear=0;
return OK;
}
//
Status QueueLength(sqQueue Q)
{
return (Q.rear-Q.front+MAXSIZE)%MAXSIZE;
}
//
Status EnQueue(sqQueue * Q, QElemtype e)
{
if((Q->rear+1)%MAXSIZE==Q->front)
{
return ERROR;
}
Q->data[Q->rear]=e;
Q->rear=(Q->rear+1)%MAXSIZE;
return OK;
}
//
Status DeQueue(sqQueue * Q,QElemtype * e)
{
if(Q->front==Q->rear)
{
return ERROR;
}
*e=Q->data[Q->front];
Q->front=(Q->front+1)%MAXSIZE;
return OK;
}
//
void CreateBiTree(BiTree * T)
{
TElemType ch;
scanf("%c",&ch);
if(ch=='#')
{
*T=NULL;
}
else
{
*T=(BiTree)malloc(sizeof(BiTNode));
}
if(!*T)
{
return ;
}
else
{
(*T)->data=ch;
CreateBiTree(&(*T)->lchild); // CreateBiTree(&(*T)->rchild); // } } // ( ) void LevelOrder(BiTree * T,sqQueue * Q) { if(*T) { EnQueue(Q,(*T)); } while(QueueLength((*Q))!=0) { DeQueue(Q,&(*T)); printf("%c ",(*T)->data); // if((*T)->lchild!=NULL) { EnQueue(Q,(*T)->lchild); } if((*T)->rchild!=NULL) { EnQueue(Q,(*T)->rchild); } } printf("
"); } int main() { sqQueue Q; BiTree root; InitQueue(&Q); printf("please enter the data:
"); CreateBiTree(&root); printf("LevelOrdertraversal the data:
"); LevelOrder(&root,&Q); return 0; }