您的位置:首页 > 理论基础 > 数据结构算法

数据结构实验之二叉树五:层序遍历

2016-08-12 08:43 295 查看


#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
typedef struct node
{
char data;
struct node *lchild;
struct node *rchild;
}node, *tree;
char c[100];
int i;
void xianxucreat(tree &t)
{
char a;
a=c[i++];
if(a==',')
{
t=NULL;
}
else
{
t=(tree)malloc(sizeof(node));
t->data=a;
xianxucreat(t->lchild);
xianxucreat(t->rchild);
}
}
void cengxu(tree &t)
{
queue<tree>sq;
if(t)
sq.push(t);
while(!sq.empty())
{
t=sq.front();
cout<<t->data;
sq.pop();
if(t->lchild!=NULL)
{
sq.push(t->lchild);
}
if(t->rchild!=NULL)
{
sq.push(t->rchild);
}

}
}
int main()
{
int n;
tree t;
cin>>n;
while(n--)
{
i=0;
cin>>c;
xianxucreat(t);
cengxu(t);
cout<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: