#include<stdlib.h>
#define Max 200
typedef struct BTree{
long int data;
struct BTree *Lchild,*Rchild;
}BinTree,*Pbtree;
Pbtree root=NULL;
void insert(long int);
void creat_btree()
{
long int n,i;
long int a[Max];
printf("please put into node number:");
scanf("%ld",&n);
for(i=0;i<n;i++)
{
printf("%ld,",i);
scanf("%ld",a+i);
}
for(i=0;i<n;i++)
{
insert(a[i]);
}
}
void insert(long m)
{
Pbtree p,q;
if(root==NULL)
{
root=(Pbtree)malloc(sizeof(BinTree));
root->data=m;
root->Lchild=NULL;
root->Rchild=NULL;
}
else
{
p=root;
while(m!=p->data)
{
if(m>p->data)
{
if(p->Rchild==NULL)
{
q=(Pbtree)malloc(sizeof(BinTree));
q->data=m;
q->Lchild=NULL;
q->Rchild=NULL;
p->Rchild=q;
}
else
{
p=p->Rchild;
}
}
else
{
if(p->Lchild==NULL)
{
q=(Pbtree)malloc(sizeof(BinTree));
q->data=m;
q->Lchild=NULL;
q->Rchild=NULL;
p->Lchild=q;
}
else
{
p=p->Lchild;
}
}
}
}
}
void pre_order(Pbtree p)
{
if(p!=NULL)
{
printf("%ld ,",p->data);
pre_order(p->Lchild);
pre_order(p->Rchild);
}
}
void in_order(Pbtree p)
{
if(p!=NULL)
{
in_order(p->Lchild);
printf("%ld ,",p->data);
in_order(p->Rchild);
}
}
void post_order(Pbtree p)
{
if(p!=NULL)
{
post_order(p->Lchild);
post_order(p->Rchild);
printf("%ld ,",p->data);
}
}
void main()
{ printf("The establishment of the establishment of non-recursive tree:/n");
creat_btree();
printf("/n");
printf("Pre-order traversal: ");
pre_order(root);
printf("/n");
printf("in-order traversal: ");
in_order(root);
printf("/n");
printf("post-order traversal: ");
post_order(root);
printf("/n");
for(;;){}
}