完善如下代码:#include<stdio.h> #include<stdlib.h> #define MAXSIZE 100 #define ERROR 0 #define OK 1 typedef int Status; typedef char ElementType; typedef struct TNode{ ElementType Data; struct TNode * Left; struct TNode * Right; }BiTNode,* BinTree; typedef struct QNode{ BinTree Data[MAXSIZE]; int front,rear; }* Queue; void LevelorderTraversal ( BinTree BT ); Queue CreatQueue(); Status IsFullQ(Queue Q); Status AddQ(Queue Q,BinTree X); Status IsEmptyQ(Queue Q); BinTree DeleteQ(Queue Q); BinTree CreatBinTree() { ElementType Data; BinTree BT, T; Queue Q = CreatQueue(); scanf("%c",&Data); if( Data != '@'){ BT = (BinTree)malloc(sizeof(struct TNode)); BT->Data = Data; BT->Left = BT->Right = NULL; AddQ(Q,BT); } else return NULL; while(!IsEmptyQ(Q)){ T = DeleteQ(Q); scanf("%c",&Data); if( Data == '@') T->Left = NULL; else{ T->Left = (BinTree)malloc(sizeof(struct TNode)); T->Left->Data = Data; T->Left->Left = T->Left->Right = NULL; AddQ(Q,T->Left); } scanf("%c",&Data); if(Data == '@') T->Right = NULL; else{ T->Right = (BinTree)malloc(sizeof(struct TNode)); T->Right->Data = Data; T->Right->Left = T->Right->Right = NULL; AddQ(Q,T->Right); } } return BT; } Queue CreatQueue() { Queue Q = (Queue)malloc(sizeof(struct QNode)); Q->front = Q->rear = 0; return Q; } Status IsFullQ(Queue Q) { if( (Q->rear+1)%MAXSIZE == Q->front ) return OK; else return ERROR; } Status AddQ(Queue Q,BinTree X) { if ( IsFullQ(Q) ) { printf("队列满"); return ERROR; } else { Q->rear = (Q->rear+1)%MAXSIZE; Q->Data[Q->rear] = X; return OK; } } Status IsEmptyQ(Queue Q) { if( Q->front == Q->rear ) return OK; else return ERROR; } BinTree DeleteQ(Queue Q) { if ( IsEmptyQ(Q) ) { printf("队列空"); return NULL; } else { Q->front = (Q->front+1)%MAXSIZE; return Q->Data[Q->front]; } } int main() { BinTree BT; BT = CreatBinTree(); if(BT == NULL){ printf("\n空树!\n"); }else{ printf("层序遍历的结果为:"); LevelorderTraversal ( BT ); } return 0; }
时间: 2023-11-28 07:48:39 浏览: 66
```
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100
#define ERROR 0
#define OK 1
typedef int Status;
typedef char ElementType;
typedef struct TNode {
ElementType Data;
struct TNode *Left;
struct TNode *Right;
} BiTNode, *BinTree;
typedef struct QNode {
BinTree Data[MAXSIZE];
int front, rear;
} *Queue;
void LevelorderTraversal(BinTree BT);
Queue CreatQueue();
Status IsFullQ(Queue Q);
Status AddQ(Queue Q, BinTree X);
Status IsEmptyQ(Queue Q);
BinTree DeleteQ(Queue Q);
BinTree CreatBinTree();
int main() {
BinTree BT;
BT = CreatBinTree();
if (BT == NULL) {
printf("\n空树!\n");
} else {
printf("层序遍历的结果为:");
LevelorderTraversal(BT);
}
return 0;
}
BinTree CreatBinTree() {
ElementType Data;
BinTree BT, T;
Queue Q = CreatQueue();
scanf("%c", &Data);
if (Data != '@') {
BT = (BinTree) malloc(sizeof(struct TNode));
BT->Data = Data;
BT->Left = BT->Right = NULL;
AddQ(Q, BT);
} else return NULL;
while (!IsEmptyQ(Q)) {
T = DeleteQ(Q);
scanf("%c", &Data);
if (Data == '@')
T->Left = NULL;
else {
T->Left = (BinTree) malloc(sizeof(struct TNode));
T->Left->Data = Data;
T->Left->Left = T->Left->Right = NULL;
AddQ(Q, T->Left);
}
scanf("%c", &Data);
if (Data == '@')
T->Right = NULL;
else {
T->Right = (BinTree) malloc(sizeof(struct TNode));
T->Right->Data = Data;
T->Right->Left = T->Right->Right = NULL;
AddQ(Q, T->Right);
}
}
return BT;
}
Queue CreatQueue() {
Queue Q = (Queue) malloc(sizeof(struct QNode));
Q->front = Q->rear = 0;
return Q;
}
Status IsFullQ(Queue Q) {
if ((Q->rear + 1) % MAXSIZE == Q->front)
return OK;
else
return ERROR;
}
Status AddQ(Queue Q, BinTree X) {
if (IsFullQ(Q)) {
printf("队列满");
return ERROR;
} else {
Q->rear = (Q->rear + 1) % MAXSIZE;
Q->Data[Q->rear] = X;
return OK;
}
}
Status IsEmptyQ(Queue Q) {
if (Q->front == Q->rear)
return OK;
else
return ERROR;
}
BinTree DeleteQ(Queue Q) {
if (IsEmptyQ(Q)) {
printf("队列空");
return NULL;
} else {
Q->front = (Q->front + 1) % MAXSIZE;
return Q->Data[Q->front];
}
}
void LevelorderTraversal(BinTree BT) {
Queue Q = CreatQueue();
BinTree T;
if (!BT) return;
AddQ(Q, BT);
while (!IsEmptyQ(Q)) {
T = DeleteQ(Q);
printf("%c", T->Data);
if (T->Left) AddQ(Q, T->Left);
if (T->Right) AddQ(Q, T->Right);
}
}
```