#define NULL 0
typedef struct node{
int key;
struct node *lchild,*rchild;
}BiNode,*BiTree;
void inorder(BiTree t) /* 中序遍历二叉树 */
{ if(t)
{ inorder(t->lchild); /* 中序遍历左子树*/
printf("%4d",t->key); /* 访问根结点 */
inorder(t->rchild); /* 中序遍历右子树 */
}
}
BiTree insert(BiTree t,BiTree s) /* 将s所指结点插入t所指根结点的树中*/
{ BiTree f,p=t;
while(p)
{ f=p;
if(s->key==p->key)return t;
if(s->key<p->key)p=p->lchild;
else p=p->rchild;
}
if(!t)return s;
if(s->key<f->key)f->lchild=s;
else f->rchild=s;
return t;
}
main()
{ BiTree root=NULL,s;
int key;
scanf("%d",&key);
while(key>0)
{ s=(BiTree)malloc(sizeof(BiNode));
s->key=key; s->lchild=s->rchild=NULL;
root=insert(root,s);
scanf("%d",&key);
}
inorder(root);
}
typedef struct node{
int key;
struct node *lchild,*rchild;
}BiNode,*BiTree;
void inorder(BiTree t) /* 中序遍历二叉树 */
{ if(t)
{ inorder(t->lchild); /* 中序遍历左子树*/
printf("%4d",t->key); /* 访问根结点 */
inorder(t->rchild); /* 中序遍历右子树 */
}
}
BiTree insert(BiTree t,BiTree s) /* 将s所指结点插入t所指根结点的树中*/
{ BiTree f,p=t;
while(p)
{ f=p;
if(s->key==p->key)return t;
if(s->key<p->key)p=p->lchild;
else p=p->rchild;
}
if(!t)return s;
if(s->key<f->key)f->lchild=s;
else f->rchild=s;
return t;
}
main()
{ BiTree root=NULL,s;
int key;
scanf("%d",&key);
while(key>0)
{ s=(BiTree)malloc(sizeof(BiNode));
s->key=key; s->lchild=s->rchild=NULL;
root=insert(root,s);
scanf("%d",&key);
}
inorder(root);
}