函数题:二叉树的层序遍历
根据二叉树的层序遍历,输出对应的层序序列。
### 函数接口定义:
c++
void LevelorderTraversal ( BinTree BT ) /* 二叉树的层序遍历 */
其中 BinTree 的结构定义为:
c++
typedef struct TNode *Position;
typedef Position BinTree;
struct TNode{
ElementType Data;
BinTree Left;
BinTree Right;
};
可直接调用队列的操作集,队列的结构定义为:
c++
typedef int Position;
typedef struct QNode * PtrToQNode;
struct QNode{
ElementType *Data;
int front,rear;
int MaxSize;
};
typedef PtrToQNode Queue;
### 裁判测试程序样例:
c++
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 100
#define ERROR -1
typedef enum {false, true} bool;
typedef struct TNode * BinTree; /* 二叉树类型 */
typedef char ElementType;
struct TNode{ /* 树结点定义 */
ElementType Data; /* 结点数据 */
BinTree Left; /* 指向左子树 */
BinTree Right; /* 指向右子树 */
};
typedef struct QNode * PtrToQNode; /* 队列 */
struct QNode{
BinTree Data[MAXSIZE];
int front,rear;
};
typedef PtrToQNode Queue;
void LevelorderTraversal ( BinTree BT ); /* 二叉树的层序遍历 */
Queue CreatQueue();
bool IsFullQ(Queue Q);
bool AddQ(Queue Q,BinTree X);
bool IsEmptyQ(Queue Q);
BinTree DeleteQ(Queue Q);
//按层序次序输入二叉树中结点的值(一个字符),@表示空树,构造二叉链表表示二叉树T
BinTree CreatBinTree()
{
ElementType Data;
BinTree BT,T;
Queue Q = CreatQueue(); /* 创建空队列 */
/* 建立第1个结点,即根结点 */
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; /* 若第1个数据就是0,返回空数 */
while(!IsEmptyQ(Q)){
T = DeleteQ(Q); /* 从队列中取出一结点地址 */
scanf("%c",&Data); /* 读入T的左孩子 */
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); /* 读入T的右孩子 */
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);
}
} /* 结束while */
return BT;
}
Queue CreatQueue()
{
Queue Q = (Queue)malloc(sizeof(struct QNode));
Q->front = Q->rear = 0;
return Q;
}
bool IsFullQ(Queue Q)
{
if( (Q->rear+1)%MAXSIZE == Q->front )
return true;
else
return false;
}
bool AddQ(Queue Q,BinTree X)
{
if ( IsFullQ(Q) ) {
printf("队列满");
return false;
}
else {
Q->rear = (Q->rear+1)%MAXSIZE;
Q->Data[Q->rear] = X;
return true;
}
}
bool IsEmptyQ(Queue Q)
{
if( Q->front == Q->rear )
return true;
else
return false;
}
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;
}
/* 请在这里填写答案 */

### 输入样例:
in
ABCDE@F@@G@@@@@
### 输出样例:
out
层序遍历的结果为:ABCDEFG
答案:若无答案欢迎评论
### 函数接口定义:
c++
void LevelorderTraversal ( BinTree BT ) /* 二叉树的层序遍历 */
其中 BinTree 的结构定义为:
c++
typedef struct TNode *Position;
typedef Position BinTree;
struct TNode{
ElementType Data;
BinTree Left;
BinTree Right;
};
可直接调用队列的操作集,队列的结构定义为:
c++
typedef int Position;
typedef struct QNode * PtrToQNode;
struct QNode{
ElementType *Data;
int front,rear;
int MaxSize;
};
typedef PtrToQNode Queue;
### 裁判测试程序样例:
c++
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 100
#define ERROR -1
typedef enum {false, true} bool;
typedef struct TNode * BinTree; /* 二叉树类型 */
typedef char ElementType;
struct TNode{ /* 树结点定义 */
ElementType Data; /* 结点数据 */
BinTree Left; /* 指向左子树 */
BinTree Right; /* 指向右子树 */
};
typedef struct QNode * PtrToQNode; /* 队列 */
struct QNode{
BinTree Data[MAXSIZE];
int front,rear;
};
typedef PtrToQNode Queue;
void LevelorderTraversal ( BinTree BT ); /* 二叉树的层序遍历 */
Queue CreatQueue();
bool IsFullQ(Queue Q);
bool AddQ(Queue Q,BinTree X);
bool IsEmptyQ(Queue Q);
BinTree DeleteQ(Queue Q);
//按层序次序输入二叉树中结点的值(一个字符),@表示空树,构造二叉链表表示二叉树T
BinTree CreatBinTree()
{
ElementType Data;
BinTree BT,T;
Queue Q = CreatQueue(); /* 创建空队列 */
/* 建立第1个结点,即根结点 */
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; /* 若第1个数据就是0,返回空数 */
while(!IsEmptyQ(Q)){
T = DeleteQ(Q); /* 从队列中取出一结点地址 */
scanf("%c",&Data); /* 读入T的左孩子 */
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); /* 读入T的右孩子 */
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);
}
} /* 结束while */
return BT;
}
Queue CreatQueue()
{
Queue Q = (Queue)malloc(sizeof(struct QNode));
Q->front = Q->rear = 0;
return Q;
}
bool IsFullQ(Queue Q)
{
if( (Q->rear+1)%MAXSIZE == Q->front )
return true;
else
return false;
}
bool AddQ(Queue Q,BinTree X)
{
if ( IsFullQ(Q) ) {
printf("队列满");
return false;
}
else {
Q->rear = (Q->rear+1)%MAXSIZE;
Q->Data[Q->rear] = X;
return true;
}
}
bool IsEmptyQ(Queue Q)
{
if( Q->front == Q->rear )
return true;
else
return false;
}
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;
}
/* 请在这里填写答案 */

### 输入样例:
in
ABCDE@F@@G@@@@@
### 输出样例:
out
层序遍历的结果为:ABCDEFG
答案:若无答案欢迎评论