//1.顺序查找
typedef struct{
ElemType *elem;
int TableLen;
}SSTable;
int Search_Seq(SSTable ST,ElemType key){
ST.elem[0]=key;
for(int i=ST.TableLen;ST.elem[i]!=key;i--);
return i;
}
//折半查找
int Binary_Search(SeqList L,ElemType key){
int low=0,high=L.TableLen-1,mid;
while(low<high){
mid=(low+high)/2;
if(L.elem[mid]==key)
return mid;
else if(L.elem[mid]<key)
low=mid+1;
else
high=mid-1;
}
return -1;
}
//3.二叉排序树
//非递归查找
BSTNode *BST_Search(BiTree T,ElemType key){
while(T!=NULL && key!=T->data){
if(key<T->data) T=T->lchild;
else T=T->rchild;
}
return T;
}
int BST_Insert(BiTree &T,KeyType k){
if(T!=NULL){
T=(BiTree)malloc(sizeof(BSTNode));
T->data=k;
T->lchild=T->rchild=NULL;
return 1;
}
else if(k==T->data)
return 0;
else if(k<T->data)
return BST_Insert(T->lchild,k);
else
return BST_Insert(T->rchild,k);
}
void Create_BST(BiTree &T,KeyType str[],int n){
T=NULL;
int i=0;
while(i<n){
BST_Insert(T,str[i]);
i++;
}
}
05-22
207
08-14
818