查找算法(二叉搜索树查找,二分查找,hash查找)

这个是在链表基础上根据实验内容然后添加了一个二分查找的功能:

二分查找在竞赛极其开发中极其有用,尤其是二分的思想!!!

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

Text.cpp:

  1.   #include<iostream>  
  2.     #include"a.h"  
  3.     using namespace 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.   
  14.         list_1.retrieve(1, x);  
  15.         cout<<x<<endl;  
  16.   
  17.         list_1.Replace(2, 100);  
  18.         list_1.Print_list();  
  19.   
  20.         list_1.Remove(3, x);  
  21.         list_1.Print_list();  
  22.   
  23.         list_1.Clear();  
  24.         list_1.Print_list();  
  25.   
  26.   
  27.         // 二分查找部分  
  28.   
  29.         for(int i = 1; i <= 5; i++){  
  30.             list_1.Insert(i - 1, i);  
  31.         }  
  32.         cout<<"二分查找检测:\n原数据:\n"<<endl;  
  33.         list_1.Print_list();  
  34.         cout<<"input value what you want to find:\n";  
  35.         cin>>x;  
  36.         int  position;  
  37.         list_1.Binary_search(x, position);  
  38.         cout<<"positon : "<<x<<endl;  
  39.         return 0;  
  40. }  

二叉搜索树的实现:

 

    这里使用了二级指针来操作,函数传参数的时候传的是二级指针,二级指针的值是一级指针的地址,指向的值是一级指针的值,通过传递二级指针来操作一级指针的值,进而可以改变一级指针的指向内容。如果我们在函数中传递的是一级指针,那么就会出线传递的是一级指针的拷贝,函数就不会改变外部指针的指向内容。尤其应用在寻找插入点的时候的定位操作!!!然后中序遍历打印。


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

Hash拉链实现查找:

测试程序为数字去重,输出部分包括数字不同的个数,并打印出去重后的序列。

  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. using namespace std;  
  15. const int 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.   
  26. int main(){  
  27.     int n, d, p;  
  28.     while(scanf("%d",&n) != EOF){  
  29.          memset(pnd, 0, sizeof(pnd));  
  30.   n_cnt = 0;  
  31.   a_cnt = 0;  
  32. for(int i = 0; i < n; i++){  
  33.     scanf("%d",&d);  
  34.     p = d % M;  
  35.     bool found = false ;  
  36.     Node *pt = pnd[p];  
  37.     while(pt){  
  38.         if(pt -> d == d){  
  39.             found = true;  
  40.             break;  
  41.         }  
  42.         pt = pt -> next;  
  43.     }  
  44.     if(!found){  
  45.         nd[n_cnt].d = d;  
  46.         nd[n_cnt].next = pnd[p];  
  47.         pnd[p] = &nd[n_cnt];  
  48.         n_cnt ++;  
  49.         a[a_cnt++] = d;  
  50.     }  
  51. }  
  52. sort(a, a + a_cnt);  
  53. printf("%d\n%d", a_cnt, a[0]);  
  54. for(int  i = 1; i < a_cnt; ++i){  
  55.     printf(" %d",a[i]);  
  56. }  
  57. printf("\n");  
  58.     }  
  59. return 0;  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值