树——层序遍历
void LevelorderTraversal(BinTree BT)
{
Queue Q;
BinTree T;
if(!BT)return;
Q=CreatQueue();
AddQ(Q,BT);
while(!IsEmpty(Q)){
T=DeleteQ(Q);
printf("%d",T->Data);
if(T->Left) AddQ(Q,T->Left);
if(T->Right) AddQ(Q,T->Right);
}
}
typedef int Position;
struct QNode{
ElementType *Data;
Position Front,Rear;
int MaxSize;
};
typedef struct QNode *Queue;
Queue CreateQueue(int MaxSize)
{
Queue Q = (Queue)malloc(sizeof(struct QNode));
Q->Data = (ElementType*)malloc(MaxSize* sizeof(ElementType));
Q->Front = Q->Rear = 0;
Q->MaxSize = MaxSize;
return Q;
}
bool IsEmpty(Queue Q)
{
return(Q->Front == Q->Rear);
}
bool IsFull(Queue Q)
{
return((Q->Rear+1)%Q->MaxSize == Q->Front);
}
bool AddQ(Queue Q,ElementType X)
{
if(IsFull(Q)){
printf("队列满");
return false;
}
else{
Q->Rear = (Q->Rear+1)%Q->MaxSize;
Q->Data[Q->Rear] = X;
return true;
}
}
ElementType DeleteQ(Queue Q)
{
if(IsEmpty(Q)){
printf("队列空");
return ERROR;
}
else{
Q->Front = (Q->Front+1)%Q->MaxSize;
return Q->Data[Q->Front];
}
}