用到队列来辅助实现,从左向右,自上而下,依次将二叉树的各节点入队,这样便可以保证输出的顺序是层序排列的。
/*
* Created by Microsoft Visual Studio 2013
* @author: Teresa
* @date: 2017-10-24
* @description: 二叉树遍层序遍历
*/
#include <stdio.h>
#include <stdlib.h>
/*函数状态码*/
#define TRUE 1 //成功
#define OK 1
#define FALSE 0 //失败
#define ERROR 0 //错误
#define INFEASIBLE -1 //不可行的
#define OVERFLOW -2 //溢出
#define MAXSIZE 100
typedef int Status; //函数的返回值类型
typedef char TElemType;
/* 二叉链表表示的二叉树 */
typedef struct BiTNode
{
TElemType data;
struct BiTNode *lchild, *rchild;
} BiTNode, *BiTree;
typedef BiTree QElemType;
typedef struct
{
QElemType data[MAXSIZE];
int front, rear; // 分别存放队头和队尾的位置
} SqQueue, *SqQueuePtr;
Status PrintElement(TElemType e){
putchar(e);
return OK;
}
//按先序次序输入二叉树中结点的值(一个字符),空格字符表示空树,构造二叉链表表示的二叉树T。
Status CreatBiTree(BiTree *T){
char ch;
scanf("%c", &ch);
//如果当前