7-6 层次遍历
给定二叉树的包含虚结点的先序遍历序列信息,按层次顺序给出遍历的结果。
输入格式:
首先输入一个整数T,表示测试数据的组数,然后是T组测试数据。每组测试数据在一行中输入一个字符串(不含空格且长度不超过80),表示二叉树的先序遍历序列(其中@表示虚结点)。
输出格式:
对于每组测试,输出层次遍历的结果。
输入样例:
1
ABD@@EG@@@C@F@@
输出样例:
ABCDEFG
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
```c
#include <stdio.h>
#include <stdlib.h>
using namespace std;
typedef char TElemType;
const int MAXSIZES = 50;
typedef struct BiTNode {
TElemType data;
struct BiTNode *lchild, *rchild;
}BiTNode, *BiTree;
typedef BiTree QElemType;
typedef struct {
QElemType data[MAXSIZES];
int front;
int rear;
}SqQueue;
void CreateBiTree(BiTree *T)
{
TElemType ch;
scanf("%c", &ch);
if (ch == '@')
{
*T = NULL;
}
else
{
*T = (BiTree) malloc(sizeof(BiTNode));
if (!T)
{
exit(0);
}
(*T)->data = ch;
CreateBiTree(&(*T)->lchild);
CreateBiTree(&(*T)->rchild);
}
}
void InitQueue(SqQueue *Q)
{
Q->front = Q->rear = 0;
}
void EnQueue(SqQueue *Q, QElemType q)
{
if ((Q->rear + 1) % MAXSIZES == Q->front)
return;
Q->data[Q->rear] = q;
Q->rear = (Q->rear + 1) % MAXSIZES;
}
void DeQueue(SqQueue *Q, QElemType *q)
{
if (Q->front == Q->rear)
return;
*q = Q->data[Q->front];
Q->front = (Q->front + 1) % MAXSIZES;
}
void LevelOrder(BiTree T)
{
SqQueue Q;
InitQueue(&Q);
BiTree p;
EnQueue(&Q, T);
while(Q.front != Q.rear)
{
DeQueue(&Q, &p);
printf("%c", p->data);
if (p->lchild != NULL)
{
EnQueue(&Q, p->lchild);
}
if (p->rchild != NULL)
{
EnQueue(&Q, p->rchild);
}
}
}
int main()
{
int n;
scanf ("%d", &n);
while (n--)
{
BiTree t;
CreateBiTree(&t);
LevelOrder(t);
}
return 0;
}
```
这段代码没什么问题,第28行输入 scanf("%c", &ch); 语句修改为:scanf(" %c", &ch); ,即可实现题目的要求,供参考:
#include <stdio.h>
#include <stdlib.h>
using namespace std;
typedef char TElemType;
const int MAXSIZES = 50;
typedef struct BiTNode {
TElemType data;
struct BiTNode* lchild, * rchild;
}BiTNode, * BiTree;
typedef BiTree QElemType;
typedef struct {
QElemType data[MAXSIZES];
int front;
int rear;
}SqQueue;
void CreateBiTree(BiTree* T)
{
TElemType ch;
scanf(" %c", &ch); //scanf("%c", &ch); 修改
if (ch == '@')
{
*T = NULL;
}
else
{
*T = (BiTree)malloc(sizeof(BiTNode));
if (!T)
{
exit(0);
}
(*T)->data = ch;
CreateBiTree(&(*T)->lchild);
CreateBiTree(&(*T)->rchild);
}
}
void InitQueue(SqQueue* Q)
{
Q->front = Q->rear = 0;
}
void EnQueue(SqQueue* Q, QElemType q)
{
if ((Q->rear + 1) % MAXSIZES == Q->front)
return;
Q->data[Q->rear] = q;
Q->rear = (Q->rear + 1) % MAXSIZES;
}
void DeQueue(SqQueue* Q, QElemType* q)
{
if (Q->front == Q->rear)
return;
*q = Q->data[Q->front];
Q->front = (Q->front + 1) % MAXSIZES;
}
void LevelOrder(BiTree T)
{
SqQueue Q;
InitQueue(&Q);
BiTree p;
EnQueue(&Q, T);
while (Q.front != Q.rear)
{
DeQueue(&Q, &p);
printf("%c", p->data);
if (p->lchild != NULL)
{
EnQueue(&Q, p->lchild);
}
if (p->rchild != NULL)
{
EnQueue(&Q, p->rchild);
}
}
}
int main()
{
int n;
scanf("%d", &n);
while (n--)
{
BiTree t;
CreateBiTree(&t);
LevelOrder(t);
}
return 0;
}
问题分析:根据题目要求,需要完成对二叉树的层次遍历。该算法主要分为两个步骤,一是将根节点入队列,然后逐步弹出队列的节点,并将其左右孩子入队列,直到队列为空;二是按每层的顺序输出所有节点的值。在实现中需要注意的是,需要判断根节点是否为空,否则会出现空指针错误。同时,如果使用递归实现,也需要注意递归的终止条件,以防止栈溢出等问题。
代码实现:
#include <stdio.h>
#include <stdlib.h>
//定义二叉树的结构
struct TreeNode {
int val;
struct TreeNode* left;
struct TreeNode* right;
};
//队列结构
struct Queue {
struct TreeNode* data[1000];
int front; //队首指针
int rear; //队尾指针
};
//初始化队列
void InitQueue(struct Queue* q)
{
q->front = q->rear = 0;
}
//队列是否为空
int IsEmpty(struct Queue* q)
{
return q->front == q->rear;
}
//入队
void EnQueue(struct Queue* q, struct TreeNode* t)
{
q->data[q->rear++] = t;
}
//出队
struct TreeNode* DeQueue(struct Queue* q)
{
return q->data[q->front++];
}
//遍历二叉树,输出每个节点的值
void printTree(struct TreeNode* root)
{
if (root) {
printf("%d ", root->val);
printTree(root->left);
printTree(root->right);
}
}
//二叉树层次遍历
void levelOrder(struct TreeNode* root)
{
if (root == NULL) {
return;
}
struct Queue* q = (struct Queue*)malloc(sizeof(struct Queue));
InitQueue(q);
EnQueue(q, root);
while (!IsEmpty(q)) {
struct TreeNode* cur = DeQueue(q);
printTree(cur);
if (cur->left) EnQueue(q, cur->left);
if (cur->right) EnQueue(q, cur->right);
}
}
int main()
{
struct TreeNode* root = (struct TreeNode*)malloc(sizeof(struct TreeNode));
root->val = 3;
root->left = (struct TreeNode*)malloc(sizeof(struct TreeNode));
root->left->val = 4;
root->left->left = NULL;
root->left->right = NULL;
root->right = (struct TreeNode*)malloc(sizeof(struct TreeNode));
root->right->val = 5;
root->right->left = NULL;
root->right->right = NULL;
levelOrder(root);
return 0;
}
优化建议:在实现该算法时,可以进一步优化队列的数据结构,比如使用循环队列等方式,提高遍历的效率。同时,在使用指针时需要小心,避免出现空指针错误。在编写代码前可以先对各个指针进行非空判断,以提高程序健壮性。