#include<bits/stdc++.h>
using namespace std;
struct stu
{
int x;
struct stu *left,*right;
stu()
{
left=NULL;
right=NULL;
}
}*root;
void insert(int x,stu *&r)
{
if(r==NULL)
{
r=new stu;
r->x=x;
}
else
{
if(x<r->x)
{
insert(x,r->left);
}
else if(x>r->x)
{
insert(x,r->right);
}
}
}
void xian(stu *r)
{
if(r==NULL)
return;
cout<<r->x<<" ";
xian(r->left);
xian(r->right);
}
void zhong(stu *r)
{
if(r==NULL)
return;
zhong(r->left);
cout<<r->x<<" ";
zhong(r->right);
}
void hou(stu *r)
{
if(r==NULL)
return;
hou(r->left);
hou(r->right);
cout<<r->x<<" ";
}
int main()
{
int n;
int x;
while(cin>>n)
{
root=NULL;
while(n--)
{
cin>>x;
insert(x,root);
}
xian(root);
cout<<endl;
zhong(root);
cout<<endl;
hou(root);
cout<<endl;
}
return 0;
}
二叉排序树
最新推荐文章于 2022-04-01 16:49:42 发布