#include<stdio.h>
#include<malloc.h>
#include<math.h>
#define ENDKEY 0
typedef int KeyType;
typedef struct Node
{ KeyType key;
struct Node *lchild,*rchild;
}BstNode,*Bstree;
void InsertBst(Bstree *bst,KeyType key)
{ Bstree s;
if(*bst==NULL)
{
s=(Bstree)malloc(sizeof(BstNode));
s->key=key;
s->lchild=NULL;
s->rchild=NULL;
bst=s;
}
else if(key<(*bst)->key)
InsertBst((*bst)->lchild, key);
else if(key>(*bst)->key)
InsertBst((*bst)->rchild, key);
}
void CreatBst(Bstree *bst)
{
int key;
*bst=NULL;
scanf("%d,",&key);
while(key!=ENDKEY)
{ InsertBst(bst,key);
scanf("%d,",&key);
}}
void InOrder(Bstree bst)
{ if(bst!=NULL)
{ InOrder (bst->lchild);
printf("%d",bst->key);
InOrde (bst->rchild);
}}
Bstree SearchBst(Bstree bst,KeyType key)
{ Bstree q; q=bst;
while(q)
{
if(q->key==key) return q;
if(q->key>key) return q=q->lchild;
else q=q->rchild;
}
return NULL;
}
int main()
{
Bstree *b; int a,c;
printf("请输入要排序的序列:");
CreatBst(&b);
printf("中序遍历输出二叉排序表:");
InOrder(&b);
printf("输入要查找得数值:");
scanf("%d",&a);
SearchBst(b,&a);
if(b) printf("查找成功!");
printf("输入要插入得数值:");
scanf("%d",&c);
InsertBst(b,c);
printf("中序遍历输出插入后二叉排序表:");
InOrder(&b);
return 0;
}