#include<iostream>
#include<queue>
using namespace std;
//定义二叉树结点链式存储结构
struct BiTNode{
int data;//数据域
struct BiTNode *lchild,*rchild;//左右孩子指针
}*BiTree;
//层序遍历函数声明
int LevelOrder(struct BiTNode *BiTree);
int main()
{
//初始化结点
struct BiTNode node1,node2,node3,node4,node5,node6,node7;
node1=(struct BiTNode){1,&node2,&node3};
node2=(struct BiTNode){2,&node4,&node5};
node3=(struct BiTNode){3,&node6,&node7};
node4=(struct BiTNode){4,NULL,NULL};
node5=(struct BiTNode){5,NULL,NULL};
node6=(struct BiTNode){6,NULL,NULL};
node7=(struct BiTNode){7,NULL,NULL};
//建树
BiTree=&node1;
LevelOrder(BiTree);
cout << endl;
return 0;
}
//层序遍历-非递归实现
int LevelOrder(struct BiTNode *BiTree){
queue<struct BiTNode*> q;
struct BiTNode *p;//遍历指针
p=BiTree;
q.push(p);
if(BiTree==NULL){return 0;}
while(!q.empty()){
p=q.front();
cout << p->data << " ";
q.pop();
if(p->lchild){
q.push(p->lchild);
}
if(p->rchild){
q.push(p->rchild);
}
}
return 0;
}