以下是二叉排序树的建立和查找算法:
typedef char *KeyType;
typedef struct{
KeyType key;
}DataType;
typedef struct BinSTreeNode{
DataType elemt;
struct BinSTreeNode *lchild,*rchild;
}*BinSTree;
BinSTree BSTreeSearch(BinSTree t,KeyType k,BinSTree *p) //查找
{
BinSTree q,bt;
q=t;
bt=t;
while(bt)
{
if(bt->elemt.key==k)
{
*p=bt;
return(bt);
}
q=bt;
if(bt->elemt.key>k)
bt=bt->lchild;
else
bt=bt->rchild;
}
*p=q;
return(bt);
}
void BSTreeInsert(BinSTree *bt,char *k) //建立和插入
{
BinSTree p,q,r;
q=*bt;
if(BSTreeSearch(q,k,&p)==NULL)
{
r=(BinSTree)malloc(sizeof(struct BinSTreeNode));
r->elemt.key=k;
r->lchild=r->rchild=NULL;
if(*bt==NULL)
*bt=r;
if(k<p->elemt.key)
p->lchild=r;
else
p->rchild=r;
}
}
以上算法请求改进,有意者请看看
如何从指针数组中获取元素,并建立二差排序树呢?有意者请给予改进想法
如从
char *st1[8]={"99070101","99070102","99070103","99070104","99070105","99070106","99070107"};
中建立二叉排序树并给予查找?