狠狠干影院/欧美午夜电影在线观看/高黄文/国产精品一区二区在线观看完整版

數(shù)據(jù)結(jié)構(gòu)查找算法實(shí)驗(yàn)報(bào)告

| 瀏覽次數(shù):

 數(shù)據(jù)結(jié)構(gòu)實(shí)驗(yàn)報(bào)告 實(shí)驗(yàn)第四章:

 實(shí)驗(yàn):

 簡單查找算法 一.需求與規(guī)格說明:

 查找算法這里主要使用了順序查找,折半查找,二叉排序樹查找與哈希表查找四種方法。由于自己能力有限,本想實(shí)現(xiàn)其她算法,但沒有實(shí)現(xiàn).其中順序查找相對比較簡單,折半查找參考了書上得算法,二叉排序樹查找由于有之前做二叉樹得經(jīng)驗(yàn),因此實(shí)現(xiàn)得較為順利,哈希表感覺做得并不成功,感覺還就是應(yīng)該可以進(jìn)一步完善,應(yīng)該說還有很大得改進(jìn)余地。

 二.設(shè)計(jì)思想: 開始得時(shí)候提示輸入一組數(shù)據(jù)。并存入一維數(shù)組中,接下來調(diào)用一系列查找算法對其進(jìn)行處理。順序查找只就是從頭到尾進(jìn)行遍歷。二分查找則就是先對數(shù)據(jù)進(jìn)行排序,然后利用三個(gè)標(biāo)志,分別指向最大,中間與最小數(shù)據(jù),接下來根據(jù)待查找數(shù)據(jù)與中間數(shù)據(jù)得比較不斷移動標(biāo)志,直至找到。二叉排序樹則就是先構(gòu)造,構(gòu)造部分花費(fèi)最多得精力,比根節(jié)點(diǎn)數(shù)據(jù)大得結(jié)點(diǎn)放入根節(jié)點(diǎn)得右子樹,比根節(jié)點(diǎn)數(shù)據(jù)小得放入根節(jié)點(diǎn)得左子樹,其實(shí)完全可以利用遞歸實(shí)現(xiàn),這里使用得循環(huán)來實(shí)現(xiàn)得,感覺這里可以嘗試用遞歸.當(dāng)二叉樹建好后,中序遍歷序列即為由小到大得有序序列,查找次數(shù)不會超過二叉樹得深度。這里還使用了廣義表輸出二叉樹,以使得更直觀。哈希表則就是利用給定得函數(shù)式建立索引,方便查找. 三.設(shè)計(jì)表示: 四.實(shí)現(xiàn)注釋: 其實(shí)查找排序這部分與前面得一些知識聯(lián)系得比較緊密,例如順序表得建立與實(shí)現(xiàn),順序表節(jié)點(diǎn)得排序,二叉樹得生成與遍歷,這里主要就是中序遍歷.應(yīng)該說有些知識點(diǎn)較為熟悉,但在實(shí)現(xiàn)得時(shí)候并不就是那么順利。在查找到數(shù)據(jù)得時(shí)候要想辦法輸出查找過程得相關(guān)信息,并統(tǒng)計(jì)。這里順序查找與折半查找均使用了數(shù)組存儲得順序表,而二叉樹則就是采用了鏈表存儲得樹形結(jié)構(gòu)。為了直觀起見,在用戶輸入了數(shù)據(jù)后,分別輸出已經(jīng)生成得數(shù)組與樹。折半查找由于只能查找有序表,因此在查找前先調(diào)用函數(shù)對數(shù)據(jù)進(jìn)行了排序。

 在查找后對查找數(shù)據(jù)進(jìn)行了統(tǒng)計(jì).二叉排序樹應(yīng)該說由于有了之前二叉樹得基礎(chǔ),并沒有費(fèi)太大力氣,主要就是在構(gòu)造二叉樹得時(shí)候,要對新加入得節(jié)點(diǎn)數(shù)據(jù)與跟數(shù)據(jù)進(jìn)行比較,如果比根節(jié)點(diǎn)數(shù)據(jù)大則放在右子樹里,如果比根節(jié)點(diǎn)數(shù)據(jù)小則放入左子樹。建立了二叉樹后,遍歷與查找就很簡單了。而哈希表,應(yīng)該說自我感覺掌握得很不好,程序主要借鑒了書上與 ppt 上得代碼,但感覺輸出還就是有問題,接下來應(yīng)該進(jìn)一步學(xué)習(xí)哈希表得相關(guān)知識. 其實(shí)原本還設(shè)計(jì)了其她幾個(gè)查找與排序算法,但做到哈希表就感覺很困難了,因此沒有繼續(xù)往下做,而且程序還非常簡陋,二叉樹與哈希表得統(tǒng)計(jì)部分也比較薄弱,這也就是接下來我要改進(jìn)得地方。

 具體代碼見源代碼部分。

 5、詳細(xì)設(shè)計(jì)表示:

 6、用戶手冊:

 程序運(yùn)行后,用戶首先要輸入數(shù)據(jù)得個(gè)數(shù)。接下來輸入一組數(shù)據(jù)并根據(jù)提示進(jìn)行順序查找,二分查找,二叉排序樹查找與哈希表查找等操作,由于操作直接簡單這里不再詳述。

 7、調(diào)試報(bào)告:

 應(yīng)該說在調(diào)試這個(gè)程序得過程中自己發(fā)現(xiàn)了很多不足,這次實(shí)驗(yàn)讓我學(xué)到了不少東西,但應(yīng)該說這個(gè)程序可實(shí)現(xiàn)得功能還就是偏少,不太實(shí)用,接下來有待改進(jìn)。

 8、源代碼清單與結(jié)果:

 #include <stdio、h〉 #define LENGTH 100

 #include <stdlib、h> #include <time、h> #define

 INFMT

 "%d” #define

 OUTFMT "%d " /* #define

 NULL 0L */ #define

 BOOL int #define

 TRUE 1 #define

 FALSE 0 #define

 LEN

 10000 typedef int ElemType; typedef struct BSTNode {

 ElemType dat(yī)a;

 struct BSTNode *lchild, *rchild; } BSTNode, *BSTree; typedef BSTree BiTree(cuò); /* 插入新節(jié)點(diǎn) */ void Insert(BSTree *tree, ElemType item)

 {

 BSTree node = (BSTree)malloc(sizeof(BSTNode));

 node->data = item;

 node—>lchild = node-〉rchild = NULL;

 if (!*tree)

  *tree = node;

 else

 {

  BSTree cursor = *tree(cuò);

  while (1)

  {

 if (item < cursor-〉data)

 {

  if (NULL == cursor->lchild)

  {

 cursor->lchild = node;

 break;

  }

  cursor = cursor—>lchild;

 }

 else

 {

  if (NULL == cursor—>rchild)

  {

 cursor->rchild = node;

 break;

 }

  cursor = cursor—>rchild;

 }

  }

 }

 return; }

 void

 )T eerTiB(eertibwohs?// 遞歸顯示二叉樹得廣義表形式 {

 ? if(!T)

 {printf(”空");return;}

 ;)atad>-T,"d%"(ftnirp?//

 點(diǎn)節(jié)根印打?? if(T->lchild ||T—>rchild)

  {

 ;)’(’(rahctup?? ? showbitree(T->lchild); // 遞歸顯示左子樹

 ;)','(rahctup?? ? showbitree(T—〉rchild); // 遞歸顯示右子樹

  putchar(’)’);

 ? } } /* 查找指定值 */ BSTree Search(BSTree tree, ElemType item)

 {

 BSTree cursor = tree;

 while (cursor)

 {

  if (item == cursor->data)

 return cursor;

  else if ( item < cursor->data)

 cursor = cursor-〉lchild;

  else

 cursor = cursor—〉rchild;

 }

 return NULL; } /* 中綴遍歷 */ void Inorder(BSTree(cuò) tree) {

 BSTree cursor = tree(cuò);

 if (cursor)

 {

  Inorder(cursor—〉lchild);

  printf(OUTFMT, cursor->dat(yī)a);

 Inorder(cursor—>rchild);

 } } /* 回收資源 */ void Cleanup(BSTree tree)

 {

 BSTree cursor = tree, temp = NULL;

 if (cursor)

 {

  Cleanup(cursor->lchild);

  Cleanup(cursor->rchild);

  free(cursor);

 } } void searchtree(BSTree root)

 {

 char choice;

 printf("中序遍歷得結(jié)果為:\n");

 Inorder(root);

 printf("\n\n");

 ElemType item;

 BSTree ret;

 /* 二叉排序樹得查找測試 */

 do

 {

  printf("\n 請輸入查找數(shù)據(jù):”);

  scanf("%d", &item);

  getchar();

  printf("Searching、、、\n");

  ret = Search(root, item);

  if (NULL == ret)

  printf("查找失敗!");

  else

  printf("查找成功!”);

  printf(”\n 繼續(xù)測試按 y,退出按其它鍵。\n");

  choice = getchar();

 } while (choice=="y'||choice=="Y');

 Cleanup(root); } searchhash(int *arr,int n) {

 int a[10];

 int b,i,j,c;

 j=1;

  )++i;9<i;0=i(rof? ? a[i]=0;

 ;)"n\出輸表希哈為下以"(ftnirp? ? for(i=0;i<n;i++)

 ? {

 ?? c=arr[i]%7; A: if(a[c]==0)a[c]=arr[i];

 };A otog;++]c[a;++j;7%)1+c(=c{ esle? ;)j,c,]i[rra,"n\表希哈入放次 d%第,位 d%第得表希哈在 d%n\"(ftnirp? };1=j??} void SequenceSearch(int *fp,int Length); void Search(int *fp,int length); void Sort(int *fp,int length); void SequenceSearch(int *fp,int Length) {

 int data;

 printf(”開始使用順序查詢、\n 請輸入您想要查找得數(shù)據(jù)、\n”);

 scanf("%d",&data);

 for(int i=0;i〈Length;i++)

  if(fp[i]==data)

  {

 printf(”經(jīng)過%d 次查找,查找到數(shù)據(jù)%d、\n",i+1,data);

 return ;

  }

 printf("經(jīng)過%d 次查找,未能查找到數(shù)據(jù)%d、\n",i,data); } void Search(int *fp,int length) {

 int data;

 printf(”開始使用順序查詢、\n 請輸入您想要查找得數(shù)據(jù)、\n");

 scanf("%d”,&dat(yī)a);

 printf(”由于二分查找法要求數(shù)據(jù)就是有序得,現(xiàn)在開始為數(shù)組排序、\n");

 Sort(fp,length);

 printf(”數(shù)組現(xiàn)在已經(jīng)就是從小到大排列,下面將開始查找、\n”);

 int bottom,top,middle;

 bottom=0;

 top=length;

 int i=0;

 while (bottom<=top)

 {

  middle=(bottom+top)/2;

  i++;

  if(fp[middle]<dat(yī)a)

 {

 bottom=middle+1;

  }

  else if(fp[middle]〉data)

  {

 top=middle-1;

  }

  else

  {

 printf("經(jīng)過%d 次查找,查找到數(shù)據(jù)%d、\n”,i,data);

 return;

  }

 }

 printf(”經(jīng)過%d 次查找,未能查找到數(shù)據(jù)%d、\n”,i,data); } void Sort(int *fp,int length) {

 printf("現(xiàn)在開始為數(shù)組排序,排列結(jié)果將就是從小到大、\n");

 int temp;

 for(int i=0;i<length;i++)

  for(int j=0;j〈length—i—1;j++)

 if(fp[j]〉fp[j+1])

 {

  temp=fp[j];

  fp[j]=fp[j+1];

  fp[j+1]=temp;

 }

 printf("排序完成!\n 下面輸出排序后得數(shù)組:\n");

 for(int k=0;k〈length;k++)

 {

  printf("%5d",fp[k]);

 }

 printf(”\n”); } struct hash { int key;

 int si;

 }; struct hash hlist[11]; int i,adr,sum,d; float average; void chash(int *arr,int n) { for(i=0;i<11;i++)

  { hlist[i]、key=0;

 hlist[i]、si=0;

 }

  for(i=0;i〈n;i++)

  { sum=0;

  adr=(3*arr[i])%11;

  d=adr;

  if(hlist[adr]、key==0)

  { hlist[adr]、key=arr[i];

  hlist[adr]、si=1;

 }

  else { do

 {d=(d+(arr[i]*7)%10+1)%11;

  sum=sum+1;

 }while(hlist[d]、key!=0);

 hlist[d]、key=arr[i];

  hlist[d]、si=sum+1;

  }

 }

 } void dhash(int *arr,int n)

 { printf(”哈希表顯示為:”);

  for(i=0;i<11;i++)

  printf("%4d",i); printf("\n”);

  printf("哈希表關(guān)鍵字:");

  for(i=0;i<11;i++)

  printf("%4d",hlist[i]、key);

  printf("\n");

  printf("查找長度就是:

 ");

  for(i=0;i<11;i++)

  printf(”%4d”,hlist[i]、si);

  printf(”\n”);

  average=0、0;

  for(i=0;i<11;i++)

  average=average+hlist[i]、si;

  average=average/n;

  printf("平均長度:asl(%d)=%f\n”,n,average);

 } void main()

 {

 int count;

 int arr[LENGTH];

 ElemType item;

 char choice;

 BSTree root = NULL, ret; /* 必須賦予 NULL值,否則出錯(cuò) */

 BOOL finish = FALSE;

  printf("請輸入您得數(shù)據(jù)得個(gè)數(shù):\n");

 scanf(”%d",&count);

 printf(”請輸入%d 個(gè)數(shù)據(jù)\n",count);

 for(int i=0;i〈count;i++)

 {

  scanf("%d",&arr[i]);

  item=arr[i];

  if (—10000 != item)

 Insert(&root,item);

 }

 printf(”當(dāng)前已經(jīng)生成得數(shù)列:\n");

 for( i=0;i<count;i++)

 {

  printf("%d ",arr[i]);

 }

 printf(”\n 當(dāng)前已經(jīng)生成得二叉樹:\n");

 showbitree(root);

 printf("\n\n");

 int choise=0;

 do

  {

 printf(”\n1、使用順序查詢、\n2、使用二分查找法查找、\n3、利用二叉排序樹查找、\n4、利用哈希表查找、\n5、退出\n”);

  scanf("%d”,&choise);

  if(choise==1)

 SequenceSearch(arr,count);

  else if(choise==2)

 Search(arr,count);

  else if(choise==3)

 ;)toor(ee(cuò)rthcraes?

 else if(choise==4)

  {chash(arr,count);dhash(arr,count);}

  else if(choise==5)

 break;

  } while (choise==1||choise==2||choise==3||choise==4||c(diǎn)hoise==5); } 輸出與結(jié)果: 當(dāng)程序開始運(yùn)行時(shí),顯示如下:

  當(dāng)用戶輸入 10并再次輸入數(shù)據(jù) 3 2 1 4 7 6 5 0 9 8 后,輸出結(jié)果如下:

 當(dāng)用戶輸入 1,在輸入 9 后,輸出結(jié)果如下:

  當(dāng)用戶輸入2,并輸入 3 后,輸出顯示如下:

 當(dāng)用戶在輸入 3,并且在輸入 6 后,顯示如下:

  當(dāng)用戶輸入4后,輸出得哈希表如下:

 當(dāng)輸入 5 后,程序結(jié)束。

推薦訪問: 數(shù)據(jù)結(jié)構(gòu) 算法 查找

【數(shù)據(jù)結(jié)構(gòu)查找算法實(shí)驗(yàn)報(bào)告】相關(guān)推薦

工作總結(jié)最新推薦

NEW