资源描述:
《数据结构教程 第三十五课 实验七 查找.doc》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、► 数据结构教程 第三十五课 实验七 查找数据结构教程 第三十五课 实验七 查找 教学目的:练习顺序查找、折半查找及二叉排序树的实现教学重点:教学难点:授课内容:顺序查找折半查找顺序查找及折半查找示例#includetypedefintKeyType;typedefstruct{KeyTypekey;intmaths;intenglish;}ElemType;#defineEQ(a,b)((a)==(b))#defineLT(a,b)((a)<(b))#defineLQ(a,b)((a)<=(b))typedefstruct{Ele
2、mType*elem;intlength;}SSTable;intSearch_Seq(SSTableST,KeyTypekey){inti;ST.elem[0].key=key;for(i=ST.length;!EQ(ST.elem[i].key,key);--i);returni;}intSearch_Bin(SSTableST,KeyTypekey){intlow,mid,high;low=1;high=ST.length;while(low<=high){mid=(low+high)/2;ifEQ(key,ST.elem[mid].key)re
3、turnmid;elseifLT(key,ST.elem[mid].key)high=mid-1;elselow=mid+1;}}getdata(SSTable*t){FILE*fp;inti=1;fp=fopen("stu.txt","r");fscanf(fp,"%d",&(t->length));while(i<=t->length){fscanf(fp,"%d%d%d",&(t->elem[i].key),&(t->elem[i].maths),&(t->elem[i].english));i++;}fclose(fp);}main(){Ele
4、mTypestu[50];SSTableclass;inti,j,k;longtime;class.elem=stu;getdata(&class);printf("Thisclasshas%dstudents.",class.length);printf("Inputstunoyouwantsearch:");scanf("%d",&k);i=Search_Seq(class,k);j=Search_Bin(class,k);printf("MathsEnglish");printf("%d%d",class.elem[i].math
5、s,class.elem[i].english);printf("%d%d",class.elem[j].maths,class.elem[j].english);for(i=1;i<=4;i++){j=stu[i].maths+stu[i].english;printf("%d",j);}}二叉排序树示例#include#defineERROR0;#defineFALSE0;#defineTRUE1;#defineOK1;typedefintElemType;typedefintStatus;typedefintKeyTyp
6、e;#defineEQ(a,b)((a)==(b))#defineLT(a,b)((a)<(b))#defineLQ(a,b)((a)<=(b))typedefstructBinaryTree{ElemTypedata;structBinaryTree*l;structBinaryTree*r;}*BiTree,BiNode;BiNode*new(){return((BiNode*)malloc(sizeof(BiNode)));}CreateSubTree(BiTree*T,ElemType*all,inti){if((all[i]==0)
7、
8、i>1
9、6){*T=NULL;returnOK;}*T=new();if(*T==NULL)returnERROR;(*T)->data=all[i];CreateSubTree(&((*T)->l),all,2*i);CreateSubTree(&((*T)->r),all,2*i+1);}CreateBiTree(BiTree*T){ElemTypeall[16]={0,1,2,3,0,0,4,5,0,0,0,0,6,0,0,0,};CreateSubTree(T,all,1);}printelem(ElemTyped){printf("%d",d);
10、}PreOrderTraverse(BiTreeT,int(*Visit)(ElemTyped