#include<queue>
#include<iostream>
using namespace std;
struct Tree{
int val; //结点数据
struct Tree *lchild; //左孩子
struct Tree *rchild; //右孩子
};
void addTree(Tree* T,Tree* p) //创造二叉树
{
queue<Tree* >q;
q.push(T);
Tree * tem=q.front();
while (!q.empty())
{
q.pop();
if (tem->lchild==NULL)
{
tem->lchild=p;
break;
}
if (tem->rchild==NULL)
{
tem->rchild=p;
break;
}
q.push(tem->lchild);
q.push(tem->rchild);
tem=q.front();
}
}
void front(Tree* T) //前序遍历
{
if(T){
cout<<T->val<<" ";
front(T->lchild);
front(T->rchild);
}
}
void middle(Tree* T) //中序遍历
{
if(T){
middle(T->lchild);
cout<<T->val<<" ";
middle(T->rchild);
}
}
void back(Tree* T) //后序遍历
{
if(T){
back(T->lchild);
back(T->rchild);
cout<<T->val<<" ";
}
}
void print(Tree* tree )
{
if (!tree)
return ;
cout<<"按层打印:"<<endl;
queue<Tree* >q;
q.push(tree);
while ( !q.empty() )
{
Tree * tem=q.front();
q.pop();
cout<<tem->val<<" ";
if (tem->lchild!=NULL)
q.push(tem->lchild);
if (tem->rchild!=NULL)
q.push(tem->rchild);
}
cout<<endl;
}
void main()
{
Tree* tree=NULL;
Tree* p;
printf("请输入结点内容(以0作为结束标识,不读入):\n");
int val;
cin>>val;
while(val!=0){ //判断输入
p=new Tree ; //创建新结点
p->val = val;
p->lchild = NULL;
p->rchild = NULL;
if(tree==NULL)
tree=p;
else
addTree(tree,p);
cin>>val;//读入用户输入
}
print(tree);
cout<<"前序遍历:"<<endl;
front(tree);
cout<<"\n中序遍历:"<<endl;
middle(tree);
cout<<"\n后序遍历:"<<endl;
back(tree);
}
二叉树的遍历
最新推荐文章于 2022-11-03 10:15:00 发布