#include
#include
typedef int datatype;
typedef struct bitree
{
datatype data;
struct bitree *lchild,*rchild;
}bitree;
bitree *create_root()
{
bitree *root;
root=(bitree *)malloc(sizeof(bitree));
scanf("%d",&root->data);
root->lchild=NULL;
root->rchild=NULL;
return root;
}
void add_node(bitree *n,int value)
{
if(valuedata)
{
if(n->lchild!=NULL){
add_node(n->lchild,value);}
}
else{
bitree *lc=(bitree *)malloc(sizeof(bitree));
lc->lchild=NULL;
lc->rchild=NULL;
lc->data=value;
n->lchild=lc;
}
if(value>n->data)
{
if(n->rchild!=NULL){
add_node(n->rchild,value); }
}
else{
bitree *rc=(bitree *)malloc(sizeof(bitree));
rc->lchild=NULL;
rc->rchild=NULL;
rc->data=value;
n->rchild=rc;
}
}
void inorder(bitree *m)
{
if(m!=NULL)
{
inorder(m->lchild);
printf("%d ",m->data);
inorder(m->rchild);
}
}
void preorder(bitree *m)
{
if(m!=NULL)
{
printf("%d ",m->data);
preorder(m->lchild);
preorder(m->rchild);
}
}
main()
{
bitree *root;
root=NULL;
char c;
root=create_root();
while((c=getchar())!='#')
{
int value;
scanf("%d",&value);
add_node(root,value);
}
inorder(root);
preorder(root);
system("pause");
return 0;
}
|