查找实验报告Word文档格式.docx

上传人:b****5 文档编号:19608165 上传时间:2023-01-08 格式:DOCX 页数:13 大小:119.67KB
下载 相关 举报
查找实验报告Word文档格式.docx_第1页
第1页 / 共13页
查找实验报告Word文档格式.docx_第2页
第2页 / 共13页
查找实验报告Word文档格式.docx_第3页
第3页 / 共13页
查找实验报告Word文档格式.docx_第4页
第4页 / 共13页
查找实验报告Word文档格式.docx_第5页
第5页 / 共13页
点击查看更多>>
下载资源
资源描述

查找实验报告Word文档格式.docx

《查找实验报告Word文档格式.docx》由会员分享,可在线阅读,更多相关《查找实验报告Word文档格式.docx(13页珍藏版)》请在冰豆网上搜索。

查找实验报告Word文档格式.docx

=high)

{mid=(low+high)/2;

if(x==A[mid].keyreturnmid;

elseif(x<

A[mid.key])high=mid-1;

elselow=mid+1;

}

return-1;

//返回查找失败的标志

(2)递归算法

intbin_search(elementtypeA[],intlow,inthigh,keytypex)

{intmid;

if(low>

high)return-1;

//查找失败

else{mid=(low+high)/2;

//求解中间元素的下标

if(x==A[mid].key)returnmid;

//查找成功

elseif(x<

A[mid].key)

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->

key<

T->

key)

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;

//产生新结点并装入数据

lchild=NILL;

u->

rchild=NULL;

//设置左、右孩子指针为空

insert(T,u);

//插入结点到二叉排序树T中

cin>

//读入下一个元素的值

4)请写出二叉排序树查找的算法。

非递归算法:

Bnode*bst_search(Bnode*T,keytypex)

Bnode*P=T;

//P指向根

while(p!

=NULL)

if(x==p->

key)returnp;

//查找成功

p->

key=p->

lchild);

//到左子树中继续查找

elsep=p->

rchild;

//到右子树中继续查找

returnp;

//返回结果可能为空,也可能非空

递归算法:

if(T==NULL||t->

key=x)

returnT;

//子树为空或已经找到时均可结束

returnbst_search(T->

lchild,x);

//左子树中查找的结果就是函数的结果

elsereturnbst_search(T->

rchild,x);

//右子树中查找的结果就是函数的结果

三、上机实验

1.实验内容。

1)建立一个顺序表,用顺序查找的方法对其实施查找;

2)建立一个有序表,用折半查找的方法对其实施查找;

3)建立一个二叉排序树,根据给定值对其实施查找;

4)对同一组数据,试用三种方法查找某一相同数据,并尝试进行性能分析。

2.实验源程序。

(1)

#include<

stdio.h>

stdlib.h>

#definemax100

intx;

typedefstruct

intdata[max];

intlistlen;

}seqlist;

voidinitial_list(seqlist*L)

L->

listlen=0;

voidlist_creat(seqlist*L)

inti;

listlen++;

i=L->

listlen;

data[i]=x;

intlast_search(seqlist*L)

data[0]=x;

while(L->

data[i]!

=x)

i--;

intfirst_search(seqlist*L)

inti,n;

n=L->

for(i=1;

i<

=n;

i++)

{

if(L->

data[i]==x)

returni;

intbin_search(seqlist*L)

intmid,low=1,high=L->

mid=(low+high)/2;

if(x==L->

data[mid])

returnmid;

elseif(x<

=L->

high=mid-1;

else

low=mid+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);

请输入你想查找的数:

顺序查找---你所要找数的下标号:

a=first_search(L);

if(a==-1)

printf("

没有你所要查的数!

else

a);

\n"

倒序查找---你所要找数的下标号:

b=last_search(L);

if(b==0)

b);

折半查找---你所要找数的下标号:

c=bin_search(L);

if(c==-1)

c);

return0;

(2)

#include<

string.h>

typedefstructBTnode

intdata;

structBTnode*lchild,*rchild;

}BTnode,*Bnode;

voidinsert(Bnode&

T,BnodeS)

if(T==NULL)

T=S;

elseif(S->

data<

data)

insert(T->

voidcreate_bat(Bnode&

T)

Bnodeu;

intx;

putanumber:

u=(BTnode*)malloc(sizeof(BTnode));

u->

lchild=NULL;

insert(T,u);

Bnodebst_search(BnodeT,intx)

if(T==NULL||T->

data==x)

returnT;

elseif((T->

data)>

x)

returnbst_search(T->

lchild,x);

rchild,x);

}

intmain()

BnodeT,p;

请先建立一棵二叉排序树:

create_bat(T);

请输入你要查找的数字:

p=bst_search(T,x);

if(p!

已找到你要查找的数!

对不起!

没有你要查找的数!

3.实验结果。

 

四、实验总结(实验过程中出现的问题、解决方法、结果或其它)

问题:

1.输入程序时的手误

2.粗心漏写程序

3.程序格式错误

解决方法:

编译后根据错误提示改正

结果:

程序正确运行,截图并完成实验报告

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 医药卫生

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1