Hash详解

对于Hash,我们是怎样来处理冲突的。现在就来介绍一些经典的Hash冲突处理的方法。主要包括

 

  (1)开放地址法

  (2)拉链法

  (3)再哈希法

  (4)建立公共溢出区

 

(1)开放地址法

 

    基本思想:当发生地址冲突时,按照某种方法继续探测Hash表中其它存储单元,直到找到空位置为止。描述如下

 

    

 

    其中为关键字的直接Hash地址,Hash表的长度,

    每次再探测时的地址增量。根据的不同取法,有不同的称呼。

 

    线性探测再散列     

    二次探测再散列     

    伪随机再散列       伪随机数

 

(2)拉链法

   

    拉链法又叫链地址法,适合处理冲突比较严重的情况。基本思想是把所有关键字为同义词的记录存储在同一个

    线性链表中。

 

代码:

[cpp] view plain copy
  1. #include <iostream>  
  2. #include <string.h>  
  3. #include <stdio.h>  
  4.   
  5. using namespace std;  
  6. const int N = 35;  
  7.   
  8. struct node  
  9. {  
  10.     int key;    //关键字  
  11.     int len;    //每个节点引出的链表长度  
  12.     bool flag;  //有数据的标志  
  13.     node *next;  
  14. };  
  15.   
  16. node list[N];  
  17.   
  18. void Init(node list[])  
  19. {  
  20.     for(int i=0; i<N; i++)  
  21.     {  
  22.         list[i].len = 0;  
  23.         list[i].flag = 0;  
  24.         list[i].next = NULL;  
  25.     }  
  26. }  
  27.   
  28. void Insert(node list[], int val, int m)  
  29. {  
  30.     int id = val % m;  
  31.     if(!list[id].flag)  
  32.     {  
  33.         list[id].key = val;  
  34.         list[id].flag = 1;  
  35.     }  
  36.     else  
  37.     {  
  38.         node *p = new node();  
  39.         p->key = val;  
  40.         p->next = list[id].next;  
  41.         list[id].next = p;  
  42.     }  
  43. }  
  44.   
  45. //输出HashTable  
  46. void Print(node list[], int m)  
  47. {  
  48.     for(int i=0; i<m; i++)  
  49.     {  
  50.         node *p = list[i].next;  
  51.         if(!list[i].flag)  
  52.             printf("The %dth record is NULL!\n", i);  
  53.         else  
  54.         {  
  55.             printf("The %dth record is %d", i, list[i].key);  
  56.             list[i].len++;  
  57.             while(p)  
  58.             {  
  59.                 printf("->%d", p->key);  
  60.                 p = p->next;  
  61.                 list[i].len++;  
  62.             }  
  63.             puts("");  
  64.         }  
  65.     }  
  66. }  
  67.   
  68. //计算平均查找长度  
  69. double ASL(node list[], int m)  
  70. {  
  71.     double ans = 0;  
  72.     for(int i=0; i<m; i++)  
  73.         ans += (list[i].len + 1) * list[i].len / 2.0;  
  74.     return ans / m;  
  75. }  
  76.   
  77. int main()  
  78. {  
  79.     int n, m;  
  80.     Init(list);  
  81.     scanf("%d %d", &n, &m);  
  82.     for(int i=0; i<n; i++)  
  83.     {  
  84.         int val;  
  85.         scanf("%d", &val);  
  86.         Insert(list, val, m);  
  87.     }  
  88.     Print(list, m);  
  89.     printf("The Average Search Length is %.5lf\n", ASL(list, m));  
  90.     return 0;  
  91. }  
  92.   
  93. /** 
  94. 12 11 
  95. 47 7 29 11 16 92 22 8 3 37 89 50 
  96. */  


关于拉链法还可以参考:http://www.51nod.com/question/index.html#!questionId=1191

 

(3)再哈希法

 

    再哈希法又叫双哈希法,有多个不同的Hash函数,当发生冲突时,使用第二个,第三个,....,等哈希函数

    计算地址,直到无冲突。虽然不易发生聚集,但是增加了计算时间。

 

(4)建立公共溢出区

 

    建立公共溢出区的基本思想是:假设哈希函数的值域是[1,m-1],则设向量HashTable[0...m-1]为基本

    表,每个分量存放一个记录,另外设向量OverTable[0...v]为溢出表,所有关键字和基本表中关键字为同义

    词的记录,不管它们由哈希函数得到的哈希地址是什么,一旦发生冲突,都填入溢出表。



来源:http://blog.csdn.net/ACdreamers/article/details/38980951

posted @ 2018-02-23 10:33  绍兴土匪  阅读(270)  评论(0编辑  收藏  举报