1. 程式人生 > >查詢演算法(二叉搜尋樹查詢,二分查詢,hash查詢)

查詢演算法(二叉搜尋樹查詢,二分查詢,hash查詢)

這個是在連結串列基礎上根據實驗內容然後添加了一個二分查詢的功能:

二分查詢在競賽極其開發中極其有用,尤其是二分的思想!!!

[cpp] view plain copy print?
  1. usingnamespace std;  
  2. constint Dafultsize = 10;  
  3. enum Error_code{success, overflow, underflow, Range_error, faild};  
  4. template<class List_entry>  
  5. class List{  
  6.   public:  
  7.       List(int Size = Dafultsize):Max_list(Size), num(0){  
  8.         Entry = new List_entry[Max_list];  
  9.       }  
  10.       int Size_list()const;  
  11.       bool Full_list()const;  
  12.       bool Empty_list()const;  
  13.       void Clear();  
  14.       void Print_list()const;  
  15.       void Tra_list(void (*visit)(List_entry &));  
  16.       Error_code retrieve(int postion,  List_entry &item)
    const;  
  17.       Error_code Replace(int postion, const List_entry &item);  
  18.       Error_code Remove(int postion,  List_entry &item);  
  19.       Error_code Insert(int postion, const List_entry &item);  
  20.       ~List(){  
  21.          delete []Entry;  
  22.       }Error_code Binary_search(const List_entry &key, List_entry &position);  
  23.   protected:  
  24.       int num;  
  25.       List_entry  *Entry;  
  26.       int Max_list;  
  27. };  
  28. template<class List_entry>  
  29. int List<List_entry>::Size_list()const{  
  30.   return num ;  
  31. }  
  32. template<class List_entry>  
  33. bool List<List_entry>::Full_list()const{  
  34.    return num == Max_list - 1;  
  35. }  
  36. template<class List_entry>  
  37. bool List<List_entry>::Empty_list()const{  
  38.    return num == 0;  
  39. }  
  40. template<class List_entry>  
  41. void List<List_entry>::Clear(){  
  42.    num = 0;  
  43. }  
  44. template<class List_entry>  
  45. Error_code List<List_entry>::Insert(int position, const List_entry &item){  
  46.     if(Full_list()){  
  47.         return overflow;  
  48.     }  
  49.     if(position < 0 || position > num){  
  50.         return Range_error;  
  51.     }  
  52.     for(int i = num - 1; i >= position; i--){  
  53.         Entry[i + 1] = Entry[i];  
  54.     }  
  55.     Entry[position] = item;  
  56.     num++;  
  57.     return success;  
  58. }  
  59. template<class List_entry>  
  60. void List<List_entry>::Tra_list(void(*visit)(List_entry &)){  
  61.    for(int i = 0; i < num; i++){  
  62.         (*visit)(Entry[i]);  
  63.    }  
  64. }  
  65. template<class List_entry>  
  66. Error_code List<List_entry>::retrieve(int position,  List_entry &item)const{  
  67.     if(Full_list()){  
  68.         return underflow;  
  69.     }  
  70.     if(position < 0 || position > num){  
  71.         return Range_error;  
  72.     }  
  73.     item = Entry[position];  
  74.     return success;  
  75. }  
  76. template<class List_entry>  
  77. Error_code List<List_entry>::Replace(int position, const List_entry &item){  
  78.     if(position > num || position < 0){  
  79.         return Range_error;  
  80.     }  
  81.    Entry[position] = item;  
  82.    return success;  
  83. }  
  84. template<class List_entry>  
  85. Error_code List<List_entry>::Remove(int position,  List_entry &item){  
  86.    if(Empty_list()){  
  87.     return underflow;  
  88.    }  
  89.    if(position < 0 || position > num){  
  90.         return Range_error;  
  91.     }  
  92.     item = Entry[position];  
  93.    for(int i = position;i < num; i++){  
  94.        Entry[i] = Entry[i + 1];  
  95.    }  
  96.    num--;  
  97.    return success;  
  98. }  
  99. template<class List_entry>  
  100. void List<List_entry>::Print_list()const{  
  101.     cout<<"| ";  
  102.    for(int i = 0; i < num; i++){  
  103.     cout<<Entry[i];  
  104.     if(i != num - 1){  
  105.         cout<<" -- ";  
  106.     }  
  107.    }  
  108.    cout<<" |"<<endl;  
  109. }  
  110. template<class List_entry>  
  111. Error_code List<List_entry>:: Binary_search(const List_entry & key, List_entry &position){  
  112.       List_entry low, high, mid;  
  113.       low =0; high = this -> Size_list() - 1;  
  114.       while(low <= high){  
  115.         mid = (low + high) / 2;  
  116.         if(key < Entry[mid]){  
  117.             high = mid - 1;  
  118.         }  
  119.         elseif(key > Entry[mid]){  
  120.             low = mid + 1;  
  121.         }  
  122.         else{  
  123.            position = mid;  
  124.             return success;  
  125.         }  
  126.       }  
  127.       return faild;  
  128. }  

Text.cpp:

[cpp] view plain copy print?
  1.   #include<iostream>
  2.     #include"a.h"
  3.     usingnamespace std;  
  4.     int main()  
  5.     {  
  6.         List<int>list_1(9);  
  7.         for(int i = 1; i <= 5; i++){  
  8.             list_1.Insert(i - 1, i);  
  9.         }  
  10.         int x;  
  11.         list_1.Print_list();  
  12.         cout<<list_1.Size_list()<<endl;  
  13.         list_1.retrieve(1, x);  
  14.         cout<<x<<endl;  
  15.         list_1.Replace(2, 100);  
  16.         list_1.Print_list();  
  17.         list_1.Remove(3, x);  
  18.         list_1.Print_list();  
  19.         list_1.Clear();  
  20.         list_1.Print_list();  
  21.         // 二分查詢部分
  22.         for(int i = 1; i <= 5; i++){  
  23.             list_1.Insert(i - 1, i);  
  24.         }  
  25.         cout<<"二分查詢檢測:\n原資料:\n"<<endl;  
  26.         list_1.Print_list();  
  27.         cout<<"input value what you want to find:\n";  
  28.         cin>>x;  
  29.         int  position;  
  30.         list_1.Binary_search(x, position);  
  31.         cout<<"positon : "<<x<<endl;  
  32.         return 0;  
  33. }  

二叉搜尋樹的實現:

這裡使用了二級指標來操作,函式傳引數的時候傳的是二級指標,二級指標的值是一級指標的地址,指向的值是一級指標的值,通過傳遞二級指標來操作一級指標的值,進而可以改變一級指標的指向內容。如果我們在函式中傳遞的是一級指標,那麼就會出線傳遞的是一級指標的拷貝,函式就不會改變外部指標的指向內容。尤其應用在尋找插入點的時候的定位操作!!!然後中序遍歷列印。

[cpp] view plain copy print?
  1.     #include<iostream>
  2.     #include<stdio.h>
  3.     #include<stdlib.h>
  4.     usingnamespace std;  
  5.     typedefstruct BiTNode{  
  6.      int data;  
  7.      struct BiTNode *lchild, *rchild;  
  8. }BiTNode, *BiTree;  
  9. // 二叉搜尋樹實現
  10. bool SearchBST(BiTree T, int key, BiTree f, BiTree *p){  
  11.   if(!T){  
  12.     *p = f;  
  13.     returnfalse;  
  14.   }  
  15.   elseif(key == T -> data){  
  16.     *p  = T;  
  17.     returntrue;  
  18.   }  
  19.   elseif((key < T -> data)){  
  20.     return SearchBST(T -> lchild, key, T, p);  
  21.   }  
  22.   else{  
  23.     return SearchBST(T -> rchild, key, T, p);  
  24.   }  
  25. }  
  26. //二叉排序樹的插入操作
  27. bool InsertBST(BiTree *T, int key){  
  28.    BiTree p, s;  
  29.    if(!SearchBST(*T, key, NULL, &p)){  
  30.         s = (BiTree)malloc(sizeof(BiTNode));  
  31.         s -> data = key;  
  32.         s -> lchild = s -> rchild = NULL;  
  33.         if(!p){  
  34.             *T = s;  
  35.         }  
  36.         elseif(key < p -> data){  
  37.             p -> lchild = s;  
  38.         }  
  39.         else{  
  40.             p -> rchild = s;  
  41.         }  
  42.         returntrue;  
  43.    }  
  44.    else
  45.     returnfalse;  
  46. }  
  47. bool Delete(BiTree *p){  
  48.    BiTree q, s;  
  49.    if((*p) -> rchild == NULL){  
  50.     q = *p;  
  51.     *p = (*p) -> lchild;  
  52.     delete q;  
  53.    }  
  54.    elseif((*p) -> lchild == NULL){  
  55.     q = *p;  
  56.     *p = (*p) -> rchild;  
  57.     delete q;  
  58.    }  
  59.    else{  
  60.     q = *p;  
  61.     s = (*p) -> lchild;  
  62.     while(s -> rchild){  
  63.         q = s;  
  64.         s = s -> rchild;  
  65.     }  
  66.     (*p) -> data = s -> data;  
  67.     if(q != *p){  
  68.         q -> rchild = s -> lchild;  
  69.     }  
  70.     else{  
  71.         q -> lchild = s -> lchild;  
  72.     }  
  73.     delete s;  
  74.    }  
  75.    returntrue;  
  76. }  
  77. bool DeleteBST(BiTree *T, int key){  
  78.     if(! *T){  
  79.         returnfalse;  
  80.     }  
  81.     else{  
  82.         if(key == (*T) -> data){  
  83.             return Delete(T);  
  84.         }  
  85.         elseif(key < (*T) -> data){  
  86.             return DeleteBST(&(*T) -> lchild, key);  
  87.         }  
  88.         else{  
  89.             return DeleteBST(&(*T) -> rchild, key);  
  90.         }  
  91.     }  
  92. }  
  93. //中序遍歷演算法,列印二叉排序樹
  94. void InOrderTraverse(BiTree T){  
  95.    if(T == NULL){  
  96.     return ;  
  97.    }  
  98.    InOrderTraverse(T -> lchild);  
  99.    printf("--%d -- ", T -> data);  
  100.    InOrderTraverse(T -> rchild);  
  101. }  
  102.     int main()  
  103.     {  
  104.         BiTNode cnt;  
  105.         cnt.data = 89;  
  106.         cnt.lchild = cnt.rchild = NULL;  
  107.         BiTree T , t;  
  108.        t = T = &cnt;  
  109.        //插入:
  110.        cout<<"input 8 numbers what you want to insert:\n";  
  111.         for(int i = 0; i < 8; i++){  
  112.                 int x;cin>>x;  
  113.             InsertBST(&T, x);  
  114.         }  
  115.         InOrderTraverse(t);  
  116.         //查詢
  117.         cout<<"\ninput value what you want to find"<<endl;  
  118.         int x;  
  119.         cin>>x;  
  120.         t = &cnt;  
  121.         BiTree p, s;  
  122.         p = NULL;  
  123.         SearchBST(t, x, p, &s);  
  124.         cout<<(s) -> data<<endl;  
  125.         // 刪除搜尋樹結點
  126.         cout<<"input node what you want to delete:\n"<<endl;  
  127.         t = &cnt;  
  128.         cin>>x;  
  129.         DeleteBST(&t, x);  
  130.         InOrderTraverse(t);  
  131.         return 0;  
  132.     }  

Hash拉鍊實現查詢:

測試程式為數字去重,輸出部分包括數字不同的個數,並打印出去重後的序列。

[cpp] view plain copy print?
  1. #include<iostream>
  2. #include<sstream>
  3. #include<algorithm>
  4. #include<cstdio>
  5. #include<string.h>
  6. #include<cctype>
  7. #include<string>
  8. #include<cmath>
  9. #include<vector>
  10. #include<stack>
  11. #include<queue>
  12. #include<map>
  13. #include<set>
  14. usingnamespace std;  
  15. constint M = 1007;  
  16. struct Node{  
  17.     int d;  
  18.     Node * next ;  
  19. };  
  20. Node * pnd[M + 1];  
  21. Node nd[M +1];  
  22. int n_cnt;  
  23. int a[1000 + 18];  
  24. int a_cnt;  
  25. int main(){  
  26.     int n, d, p;  
  27.     while(scanf("%d",&n) != EOF){  
  28.          memset(pnd, 0, sizeof(pnd));  
  29.   n_cnt = 0;  
  30.   a_cnt = 0;  
  31. for(int i = 0; i < n; i++){  
  32.     scanf("%d",&d);  
  33.     p = d % M;  
  34.     bool found = false ;  
  35.     Node *pt = pnd[p];  
  36.     while(pt){  
  37.         if(pt -> d == d){  
  38.             found = true;  
  39.             break;  
  40.         }  
  41.         pt = pt -> next;  
  42.     }  
  43.     if(!found){  
  44.         nd[n_cnt].d = d;  
  45.         nd[n_cnt].next = pnd[p];  
  46.         pnd[p] = &nd[n_cnt];  
  47.         n_cnt ++;  
  48.         a[a_cnt++] = d;  
  49.     }  
  50. }  
  51. sort(a, a + a_cnt);  
  52. printf("%d\n%d", a_cnt, a[0]);  
  53. for(int  i = 1; i < a_cnt; ++i){  
  54.     printf(" %d",a[i]);  
  55. }  
  56. printf("\n");  
  57.     }  
  58. return 0;