returnbin_search(A,low,mid-1,x);//将在左边区域查找的结果作为在整个区域的查找结果返回
elsereturnbin_search(A,mid+1,high,x);//将在右边区域查找的结果作为在整个区域的查找结果返回
}
}
3.二叉排序树查找算法:
1)请写出二叉排序树结点的结构体定义语句。
typedefchardatatype;
typedefstructnode
{keytypekey;
datatypedata;
structnode*lchild,*rchild;
}BSTnode;
2)请写出二叉排序树中插入结点的算法。
voidinsert(Bnode*&T,Bnode*S)//将指针S所指结点插入到二叉排序树T中
{
if(T==NULL)
T=S;//插入到空树时,插入结点成为根结点
elseif(S->keykey)
insert(T->lchild,S);//插入到T的左子树中
elseinsert(T->rchild,S);//插入到T的右子树中
}
3)请写出二叉排序树构造的算法。
voidcreate_bst(Bnode*&T);//通过插入结点构造二叉排序树的算法
{Bnode*u;elementtypex;
T=NULL;cin>>x;//初始化根指针并读入第一个元素值
While(x!
=end_of_num)//x不是结束符时
{u=newBnode;u->data=x;//产生新结点并装入数据
u->lchild=NILL;u->rchild=NULL;//设置左、右孩子指针为空
insert(T,u);//插入结点到二叉排序树T中
cin>>x;//读入下一个元素的值
}
}
4)请写出二叉排序树查找的算法。
非递归算法:
Bnode*bst_search(Bnode*T,keytypex)
{
Bnode*P=T;//P指向根
while(p!
=NULL)
if(x==p->key)returnp;//查找成功
elseif(xkey=p->lchild);//到左子树中继续查找
elsep=p->rchild;//到右子树中继续查找
returnp;//返回结果可能为空,也可能非空
}
递归算法:
Bnode*bst_search(Bnode*T,keytypex)
{
if(T==NULL||t->key=x)
returnT;//子树为空或已经找到时均可结束
elseif(xkey)
returnbst_search(T->lchild,x);//左子树中查找的结果就是函数的结果
elsereturnbst_search(T->rchild,x);//右子树中查找的结果就是函数的结果
}
三、上机实验
1.实验内容。
1)建立一个顺序表,用顺序查找的方法对其实施查找;
2)建立一个有序表,用折半查找的方法对其实施查找;
3)建立一个二叉排序树,根据给定值对其实施查找;
4)对同一组数据,试用三种方法查找某一相同数据,并尝试进行性能分析。
2.实验源程序。
(1)
#include
#include
#definemax100
intx;
typedefstruct
{
intdata[max];
intlistlen;
}seqlist;
voidinitial_list(seqlist*L)
{
L->listlen=0;
}
voidlist_creat(seqlist*L)
{
inti;
L->listlen++;
i=L->listlen;
L->data[i]=x;
}
intlast_search(seqlist*L)
{
inti;
i=L->listlen;
L->data[0]=x;
while(L->data[i]!
=x)
i--;
returni;
}
intfirst_search(seqlist*L)
{
inti,n;
n=L->listlen;
for(i=1;i<=n;i++)
{
if(L->data[i]==x)
returni;
}
return-1;
}
intbin_search(seqlist*L)
{
intmid,low=1,high=L->listlen;
while(low<=high)
{
mid=(low+high)/2;
if(x==L->data[mid])
returnmid;
elseif(x<=L->data[mid])
high=mid-1;
else
low=mid+1;
}
return-1;
}
intmain(void)
{
seqlist*L;
L=(seqlist*)malloc(sizeof(seqlist));
inta,b,c;
initial_list(L);
printf("你想创建有序的查找表(以-1结束):
");
scanf("%d",&x);
while(x!
=-1)
{
list_creat(L);
scanf("%d",&x);
}
printf("请输入你想查找的数:
");
scanf("%d",&x);
printf("顺序查找---你所要找数的下标号:
");
a=first_search(L);
if(a==-1)
printf("没有你所要查的数!
");
else
printf("%d",a);
printf("\n");
printf("倒序查找---你所要找数的下标号:
");
b=last_search(L);
if(b==0)
printf("没有你所要查的数!
");
else
printf("%d",b);
printf("\n");
printf("折半查找---你所要找数的下标号:
");
c=bin_search(L);
if(c==-1)
printf("没有你所要查的数!
");
else
printf("%d",c);
printf("\n");
return0;
}
(2)
#include
#include
#include
typedefstructBTnode
{
intdata;
structBTnode*lchild,*rchild;
}BTnode,*Bnode;
voidinsert(Bnode&T,BnodeS)
{
if(T==NULL)
T=S;
elseif(S->datadata)
insert(T->lchild,S);
elseinsert(T->rchild,S);
}
voidcreate_bat(Bnode&T)
{
Bnodeu;
intx;
T=NULL;
printf("putanumber:
");
scanf("%d",&x);
while(x!
=-1)
{
u=(BTnode*)malloc(sizeof(BTnode));
u->data=x;
u->lchild=NULL;
u->rchild=NULL;
insert(T,u);
printf("putanumber:
");
scanf("%d",&x);
}
}
Bnodebst_search(BnodeT,intx)
{
if(T==NULL||T->data==x)
returnT;
elseif((T->data)>x)
returnbst_search(T->lchild,x);
else
returnbst_search(T->rchild,x);
}
intmain()
{
intx;
BnodeT,p;
printf("请先建立一棵二叉排序树:
");
printf("\n");
create_bat(T);
printf("请输入你要查找的数字:
");
scanf("%d",&x);
p=bst_search(T,x);
if(p!
=NULL)
printf("已找到你要查找的数!
");
else
printf("对不起!
没有你要查找的数!
");
printf("\n");
return0;
}
3.实验结果。
四、实验总结(实验过程中出现的问题、解决方法、结果或其它)
问题:
1.输入程序时的手误
2.粗心漏写程序
3.程序格式错误
解决方法:
编译后根据错误提示改正
结果:
程序正确运行,截图并完成实验报告