• 范文大全
  • 公文写作
  • 工作总结
  • 工作报告
  • 医院总结
  • 合同范文
  • 党团范文
  • 心得体会
  • 讲话稿
  • 安全管理
  • 广播稿
  • 条据书信
  • 优秀作文
  • 口号大全
  • 简历范文
  • 应急预案
  • 经典范文
  • 入党志愿书
  • 感恩演讲
  • 发言稿
  • 工作计划
  • 党建材料
  • 脱贫攻坚
  • 党课下载
  • 民主生活会
  • 不忘初心
  • 主题教育
  • 对照材料
  • 您现在的位置:雨月范文网 > 公文写作 > 正文

    查找实验报告

    来源:雨月范文网 时间:2020-07-25 点击:

     实验报告

     姓

      课程名称:

      院(系

     专业/年级:

     实验四

     —- - 查找

     一、实验目得 1. 掌握顺序表得查找方法,尤其就是折半查找方法; 2. 掌握二叉排序树得查找算法。

     二、实验预习内容 请在上机前认真阅读教材及实验指导书 , 并在以下空白处填写相应得内容 . 1. 请写出简单顺序查找算法。

     int seq_search(elementtype A[],int n, keytype x)

     {

      i=n;A[0]、key=x;

      while(A[i]、key=x)

     i-—;

      return i; } 2. 请写出有序表二分(折半)查找算法。

     (1)非递归算法 int bin_search(elementtype A[],int n,keytype x)

     { int mid,low=0,high=n-1;

     //初始化查找区域

      while(low<=high)

     { mid=(low+high)/2;

     if(x==A[mid]、key return mid;

     else if(x<A[mid、key])high=mid-1;

     else low=mid+1;

     }

     return —1;

     //返回查找失败得标志 } (2)递归算法 int bin_search(elementtype A[],int low,int high,keytype x) { int mid;

      if( low>high)

     return -1;//查找失败

      else { mid=(low+high)/2;//求解中间元素得下标

     if( x==A[mid]、key ) return mid;//查找成功

     else if( x<A[mid]、key )

     return bin_search(A,low,mid-1,x);//将在左边区域查找得结果作为在整个区域得查找结果返回

      else return bin_search(A,mid+1,high,x);

     //将在右边区域查找得结果作为在整个区域得查找结果返回

     } }

     3. 二叉排序树查找算法: 1)请写出二叉排序树结点得结构体定义语句。

     typedef char datatype; typedef struct node {

     keytype key;

     datatype data;

     struct node * lchild, *rchild; }BSTnode;

     2)请写出二叉排序树中插入结点得算法. void insert (Bnode *&T,Bnode *S)

      //将指针 S 所指结点插入到二叉排序树 T 中 {

     if (T==NULL)

     T=S;

      //插入到空树时,插入结点成为根结点

      else if (S—〉key<T—>key)

     insert (T->lchild,S);

      //插入到 T 得左子树中

      else insert(T—>rchild,S);

     //插入到 T 得右子树中 }

      3)请写出二叉排序树构造得算法。

     void create_bst(Bnode *&T);

      //通过插入结点构造二叉排序树得算法 {

     Bnode * u;elementtype x;

     T=NULL;cin〉>x;

      //初始化根指针并读入第一个元素值

     While (x!=end_of_num)

     //x 不就是结束符时

      {

     u=new Bnode; u->data=x;

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

      u->lchild=NILL;u->rchild=NULL;

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

      insert (T,u);

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

      cin〉>x;

      //读入下一个元素得值

     } } 4)请写出二叉排序树查找得算法.

      非递归算法:

     Bnode * bst_search(Bnode * T,keytype x) {

     Bnode * P=T;

     //P 指向根

     while (p!=NULL)

     if( x==p-〉key) return p;

      //查找成功

      else if( x〈p->key=p—〉lchild);

     //到左子树中继续查找

      else

     p=p—>rchild;

     //到右子树中继续查找

      return p;

     //返回结果可能为空,也可能非空 } 递归算法:

     Bnode * bst_search(Bnode * T,keytype x) {

      if (T==NULL || t—>key=x)

     return T;

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

      else

     if(x〈T->key)

     return bst_search(T->lchild, x);

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

      else

     return bst_search(T->rchild, x);

      //右子树中查找得结果就就是函数得结果 } 三、上机实验 1. 实验内容.

     1)建立一个顺序表,用顺序查找得方法对其实施查找; 2)建立一个有序表,用折半查找得方法对其实施查找; 3)建立一个二叉排序树,根据给定值对其实施查找; 4)对同一组数据,试用三种方法查找某一相同数据,并尝试进行性能分析。

     2. 实验源程序。

     (1)

     #include 〈stdio、h> #include <stdlib、h〉 #define max 100 int x; typedef struct

     {

     ;]xam[atad tniﻩ ;neltsil tniﻩ}seqlist; void initial_list(seqlist *L) {

     L->listlen=0; } void list_creat(seqlist *L) {

     int i;

     ;++neltsil>—Lﻩ i=L->listlen;

     ;x=]i[atad〉-Lﻩ} int last_search(seqlist *L) {

     int i;

      ;neltsil>-L=iﻩ L->data[0]=x;

     while(L->data[i]!=x)

      i——;

     return i; } int first_search(seqlist *L)

     {

     int i,n;

     n=L->listlen;

     )++i;n=<i;1=i(rofﻩ {ﻩ

     )x==]i[atad>-L(fiﻩ

      ;i nruterﻩ }ﻩ return -1; } int bin_search(seqlist *L)

     {

     int mid,low=1,high=L—>listlen;

     )hgih=<wol(elihwﻩ {ﻩ ﻩ mid=(low+high)/2;

     ﻩ if(x==L->data[mid])

     ﻩ

     ;dim nruterﻩ

     else if(x<=L->data[mid])

     high=mid—1;

      esleﻩ

      low=mid+1;

     }ﻩ ;1— nruterﻩ} int main(void)

     {

     seqlist *L;

     L=(seqlist*)malloc(sizeof(seqlist));

      int a,b,c;

     ;)L(tsil_laitiniﻩ printf("您想创建有序得查找表(以-1 结束):”);

     scanf("%d",&x);

     while(x!=-1)

     {

      ;)L(taerc_tsilﻩ

     scanf(”%d”,&x);

     }

      printf("请输入您想查找得数:");

     ;)x&,”d%”(fnacsﻩ

     printf("顺序查找---您所要找数得下标号:");

     a=first_search(L);

     if(a==—1)

      ;)"!数得查要所您有没"(ftnirpﻩ esleﻩ

     printf("%d”,a);

      printf("\n”);

     printf("倒序查找——-您所要找数得下标号:");

      b=last_search(L);

      if(b==0)

      printf("没有您所要查得数!");

     esleﻩ ﻩ printf("%d",b);

      printf("\n");

      printf("折半查找——-您所要找数得下标号:");

      c=bin_search(L);

      if(c==-1)

     ;)”!数得查要所您有没"(ftnirpﻩﻩ else

     ﻩ printf("%d",c);

      printf("\n");

     ;0 nruterﻩ} (2)

     #include<stdio、h>

     #include〈string、h> #include<stdlib、h> typedef struct BTnode {

     int data;

     struct BTnode *lchild,*rchild; } BTnode,*Bnode; void insert(Bnode &T,Bnode

     S)

     {

     )LLUN==T(fiﻩ

     ;S=Tﻩ )atad〉-T<atad>—S(fi esleﻩ ﻩ insert(T—>lchild,S);

      else insert(T->rchild,S); } void create_bat(Bnode &T) {

     Bnode u;

      ;x tniﻩ ;LLUN=Tﻩ printf("put a number:");

     ;)x&,"d%”(fnacsﻩ )1-=!x(elihwﻩ {ﻩ

     ;))edonTB(foezis(collam)*edonTB(=uﻩ

     ;x=atad>-uﻩ

     u->lchild=NULL;

      u—〉rchild=NULL;

      insert(T,u);

      ;)”:rebmun a tup"(ftnirpﻩ

      ;)x&,”d%"(fnacsﻩ } } Bnode bst_search(Bnode T,int x) {

     )x==atad〉-T||LLUN==T(fiﻩ

     return T;

      )x〉)atad〉-T((fi esleﻩ ;)x,dlihcl>—T(hcraes_tsb nruterﻩﻩ else

     ﻩ return bst_search(T->rchild,x); }

     int main()

     {

     int x;

     ;p,T edonBﻩ printf("请先建立一棵二叉排序树:”);

     ;)"n\"(ftnirpﻩ create_bat(T);

     ;)":字数得找查要您入输请"(ftnirpﻩ

     scanf(”%d",&x);

     ;)x,T(hcraes_tsb=pﻩ )LLUN=!p(fiﻩ ﻩ printf("已找到您要查找得数!");

      esleﻩ

     ;)"!数得找查要您有没!起不对"(ftnirpﻩ ;)"n\”(ftnirpﻩ ;0 nruterﻩ} 3、

     实验结果。

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

     问题:1、输入程序时得手误

      2、粗心漏写程序

      3、程序格式错误

     解决方法:编译后根据错误提示改正 结果:程序正确运行,截图并完成实验报告

    推荐访问:查找 实验 报告