结点结构
/**********结点结构*********/
typedef struct BTNode
{
int key;
struct BTNode *lchild;
struct BTNode *rchild;
}BiNode,*BiTree;
查找关键字
/********查找关键字********/
BiNode* BSTSearch(BiTree bt, int key)
{
if (bt == NULL)
return NULL;
else
{
if (key == bt->key)
printf("查找成功");
else if (key < bt->key)
return BSTSearch(bt->lchild,key);
else
return BSTSearch(bt->rchild,key);
}
}
插入关键字
/**********插入关键字*********/
int BSTInsert(BiNode *bt,int key)
{
if (bt == NULL)
{
bt =(BiNode*) malloc(sizeof(BiNode));
bt->lchild = bt->rchild = NULL;
bt->key = key;
return 1;
}
else
{
if (key == bt->key) //关键字已经存在于树中,插入失败
return 0;
else if (key < bt->key)
return BSTInsert(bt->lchild, key);
else
return BSTInsert(bt->rchild, key);
}
}