typedef struct Tnode
{
int data;
struct Tnode *lchild,*rchild;
}BSTnode,*BSTree;
//二叉排序树的查找算法
BSTree SearchBT(BSTree root,int key,BSTree *father)
{
BSTree p=root; *father=NULL;
while(p&&p->data!=key)
{
*father=p;
if(key<p->data) p=p->lchild;
else p=p->rchild;
}
return p;
}
//二叉排序树的插入算法
int InsertBST(BSTree *root,int newkey)
{
BSTree s,p,f;
s=(BSTree)malloc(sizeof(BSTnode));
if(!s) return -1;
s->data=newkey;s->lchild=NULL;s->rchild=NULL;
p=SearchBT(*root,newkey,&f);
if(p)return -1;
if(!f) *root=s;
else if(newkey<f->data)f->lchild=s;
else f->rchild=s;
return 0;
}
二叉排序树
最新推荐文章于 2022-11-20 15:46:37 发布