#include<stdio.h>
#include<string>
using namespace std;
struct node
{
node *lchild;
node *rchild;
int x;
}tree[50];
int loc = 0;
node *creat()
{
tree[loc].lchild = tree[loc].rchild = NULL;
return &tree[loc++];
}
void xianxu(node *t)
{
printf("%d ", t->x);
if (t->lchild != NULL)
xianxu(t->lchild);
if (t->rchild != NULL)
xianxu(t->rchild);
}
void zhongxu(node *t)
{
if (t->lchild != NULL)
zhongxu(t->lchild);
printf("%d ", t->x);
if (t->rchild != NULL)
zhongxu(t->rchild);
}
void houxu(node *t)
{
if (t->lchild != NULL)
zhongxu(t->lchild);
if (t->rchild != NULL)
zhongxu(t->rchild);
printf("%d ", t->x);
}
node *insert(node *t, int y)
{
if (t == NULL)
{
t = creat();
t->x = y;
return t;
}
else if (y < t->x)
t->lchild = insert(t->lchild, y);
else if (y > t->x)
t->rchild = insert(t->rchild, y);
return t;
}
int main()
{
int n;
while (scanf("%d", &n) != EOF)
{
node *t=NULL;
for (int i = 0; i < n; i++)
{
int a;
scanf("%d", &a);
t = insert(t, a);
}
xianxu(t);
printf("\n");
zhongxu(t);
printf("\n");
houxu(t);
printf("\n");
}
return 0;
}
二叉排序树
最新推荐文章于 2024-04-13 22:20:27 发布