#include <stdio.h>
#include <conio.h>
#define NULL 0
struct node
{
struct node *left;
int data;
struct node *right;
};
typedef struct node tree;
tree *insert(tree *root,int d)
{tree *p,*q;
if (root==NULL)
{
p=(tree *)malloc(sizeof(tree));
p->left=p->right=NULL;
p->data=d;
root=p;
return root;
}
p=root;
while(p&&p->data!=d)
{q=p;
if(p->data>d) p=p->left;
else p=p->right;
}
if(p->data==d) return root;
p=(tree *)malloc(sizeof(tree));
p->data=d;
p->left=NULL;
p->right=NULL;
if(d>q->data) q->right=p;
else q->left=p;
return root;
}
tree *creattree(int *nodelist,int len)
{ tree *k;
int i;
for (i=0;i<len;i++)
{
k=insert(k,nodelist[i]);
}
return k;
}
void print(tree *p)
{ tree *q;
q=p->left;
while(q!=NULL)
{
printf("%d ",q->data);
q=q->left;
}
q=p->right;
while (q!=NULL)
{
printf("%d " ,q->data);
q=q->right;
}
}
void main()
{ tree *p;
int nodelist[20];
int i,n;
printf("~~~~~~~~~please input total of numbers: \n");
scanf("%d",&n);
for(i=0;i<n;i++)
{printf("\n input the %d th number:",i);
scanf("%d",&nodelist[i]);
}
p=creattree(nodelist,n);
print(p);
getch();
}