• 哈希表(哈希函数和处理哈希冲突)_20230528


    哈希表(哈希函数和处理哈希冲突)

    1. 前言

    关于哈希表的主题的小记原计划5月23日完成,由于本人新冠阳性,身体发烧乏力,周末感觉身体状况稍加恢复,赶紧打开电脑把本文完成,特别秉承“写是为了更好地思考,更好地思考才能取得更大进步”的思想,不敢懈怠,赶紧把学习到的东西记录下来。

    首先我们要搞明白为什么要发明哈希表的技术?我们学习和讨论过各种结构(线性表、树、图等),数据记录在结构中的相对位置是随机的,记录和关键字之间不存在确定的关系,因此,在查找记录时,需进行和关键字进行一系列的比较。查找效率依赖于查找过程中所比较的次数。哈希表的发表就是希望不经过任何比较,一次存取便能得到查找记录,可以大大提升查找、插入、删除等基本操作的效率。

    要实现哈希表的技术思想,我们必须在储存位置和它的关键字之间建立一个确定的对应关系或对应函数f,使每个关键字和数据结构的为转移储存位置相对应。因此在查找关键字key的时候,我们只需调取函数f(k),计算出数据的储存位置,若结构中存在的关键字和key相等,那么必定在f(key)的位置上。因此不需要进行任何比较便可直接取得查询的记录。在此我们称这个对应关系为哈希函数(Hash function),按这个思想建立的表称为哈希表(Hash table)。

    1. 哈希函数

    2.1 哈希冲突

    由于哈数是一类影像函数,那么它就比不可避免出现这样一类情况,原始的关键字key_m和key_n并不相等,但是经过哈希函数处理之后f(key_m)=f(key_n),这种情况下就出现所谓的“哈希冲突”。比如我们有5个关键字k1…k5,经过哈希函数h(x)影像之后,h(k2)=h(k5),那么就说k2和k5在h(x)哈希函数影像下出现冲突。

    在这里插入图片描述

    实际应用当中,只能减少哈希冲突的概率,而没有办法杜绝哈希冲突,因为哈希实际上是对关键字进行了某种程度信息上的压缩,导致的后果就是压缩的信息可能相同,需要再次进行区分。

    2.2 哈希函数

    构造哈希函数的方法有很多,在介绍各种方法之前,首先需要明确什么是好的哈希函数。若对关键字集合当中的任何一个关键字,经过哈希函数映射到地址中的任意地址的概率是相等的,则称作此类哈希函数是均匀的哈希函数。换句话手,就是经过哈希函数映射后,得到一个随机地址,以便使一组关键字的哈希地址分布在整个区间中,从而减少冲突。

    2.3 构造哈希函数的常用方法

    a) 直接地址法

    取关键字或关键字某个线性函数的值为哈希地址,
    H ( K e y ) = a ∗ k e y + b H(Key)=a*key+b H(Key)=akey+b
    由于直接定址所得关键字地址和集合相同,对于不同的关键字不会发生哈希冲突,但在实际应用中,使用这种哈希哈数非常少。

    b) 数字分析法

    假设关键字都是以10为基的数,并且哈希表中的关键字都是事先知道的,则可以取关键字的若干位组成哈希地址。

    c) 平方取中法

    取关键字平方后的中间几位为哈希地址。这是一种较常用的构造哈希函数的方法。通常在选定哈希哈数的时候不一定能知道关键字的全部情况,取其中哪几位也不一定合适,而一个数字平方后的中间几位和数的每一位都相关,由此随机分布的关键字的哈希地址也是随机的。

    d) 折叠法

    将关键字分为位数相同的几部分,然后取这几个部分的叠加和作为哈希地址,这个方法称为折叠法(folding)。关键字位数很多,而且关键字每一位上数字分布大致均匀时,可以采用折叠法得到哈希地址。

    e)除留余数法

    取关键字被某个不大于表长m的数p除后所得余数为哈希地址,也即是说,
    H ( k e y ) = k e y   M O D   p    ( p ≤ m ) H(key)=key\ MOD\ p\ \ (p≤m) H(key)=key MOD p  (pm)
    这是最简单,最常用的构造哈希函数的方法,不仅对关键字可以直接取模,也可在折叠,平方取中后进行取模处理。

    1. 哈希冲突处理方法

    “好”的哈希函数可以减少哈希冲突概率,但不能避免,因此,如何处理哈希冲突是哈希造表不可缺少的一个方面。通常处理哈希冲突有下列几种方法:

    3.1 开放定址法
    H i = ( H ( k e y ) + d i )   M O D   m     ( i = 1 , 2 , . . . k ) , k < = ( m − 1 ) H_i=(H(key)+d_i)\ MOD\ m\ \ \ (i=1,2,...k), k<=(m-1) Hi=(H(key)+di) MOD m   (i=1,2,...k),k<=(m1)
    H(key)为哈希函数,m为哈希表长,di为增量序列,可用下列三种取法:

    (1)di=1,2,3,…m-1, 线性探测

    (2) di= ±12,±22,…±k2 称为二次探测再散列

    (3) di= 伪随机序列

    3.2 链地址法

    将所有关键字为同义词的记录存储在同一线性表中。假定某哈希函数产生的哈希地址在区间[0,m-1]上,则设立一个指针型向量

    Chain ChainHash[m]

    其每个分量的初始地址都是空指针,凡是哈希地址为i的记录,都插入到头指针为ChainHash[i]的链表中。为了便于查找、插入和删除操作,需要保证后续链表按关键字有序。

    1. 哈希函数实现

    本文采用链地址方法处理冲突,哈希函数采用最简单的除留余数法。

    4.1 头文件

    /**
     * @file hash_table.h
     * @author your name (you@domain.com)
     * @brief 
     * @version 0.1
     * @date 2023-05-23
     * 
     * @copyright Copyright (c) 2023
     * 
     */
    #ifndef HASH_TABLE_H
    #define HASH_TABLE_H
    #include 
    #include 
    #include 
    #include 
    #include 
    #include "../00_introduction/Status.h"
    
    #define EQ(a,b) ((a)==(b))
    #define LT(a,b) ((a)<(b))
    #define LQ(a,b) ((a)<=(b))
    
    #define MAX_LEN 20
    
    #define m 13    //采用除留余数法,除数定义为13
    
    
    typedef int    KeyType;
    typedef char*  Record;
    
    
    typedef struct  ElemType
    {
        KeyType key;
        Record  value;
    }ElemType;
    
    typedef struct HashNode
    {
        KeyType key;
        struct HashNode *next;
    } HashNode, *HashPtr;
    
    typedef enum NodeType
    {
        head,
        intermediate
    }NodeType;
    
    typedef struct Result
    {
        NodeType type;
        HashPtr *node_ptr;  //待返回的节点
        HashPtr *node_ptr_2; //待返回节点的前一节点,主要用于删除操作
        int      flag; // 0= search failure, 1 = search success
    } Result;
    
    
    typedef struct HashTable
    {
        ElemType   *elem;        //Use the dynamic programming to allocate the space
        int        count;        //Number of element in the current hashtable
        int        size_index;   //Capacity of hash table
    } HashTable;
    
    typedef struct SSTable
    {
        ElemType *elem;
        int len;
    } SSTable;
    
    
    /**
     * @brief Create a static table
     * 
     * @param fp File to pointer
     * @param st Static table
     */
    void create_table(FILE *fp, SSTable *st);
    
    /**
     * @brief Intialize the HashPtr as Null pointer
     *
     * @param hash_chain
     * @param m Number of pointer of hash
     */
    void init_hash(HashPtr *hash_chain);
    
    
    
    /**
     * @brief Use hash function to map to the address
     * 采用取余的方法
     * @param key Key value
     * @return int Return hash address
     */
    int hash_function(KeyType key);
    
    
    
    
    /**
     * @brief search 'key' from the hash table
     * 
     * @param ht Hash table variable
     * @param key Key value
     * @param p   Position of key value
     * @param c   Number of collision in the search
     * @return Result 
     */
    Result search_hash(HashPtr *hash, KeyType key);
    
    
    
    /**
     * @brief Insert one element into the hashtable
     *
     * @param hash Pointer to hash table
     * @param key Element type
     * @return Status -Return success or unsuccess
     */
    Status insert_hash(HashPtr *hash, KeyType key);
    
    
    
    /**
     * @brief Insert one element into the hashtable
     *
     * @param hash Pointer to hash table
     * @param key Element type
     * @return Status -Return success or unsuccess
     */
    Status delete_hash(HashPtr *hash, KeyType key);
    
    
    #endif
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138

    4.2 函数实现

    /**
     * @file hash_table.c
     * @author your name (you@domain.com)
     * @brief 
     * @version 0.1
     * @date 2023-05-23
     * 
     * @copyright Copyright (c) 2023
     * 
     */
    #ifndef HASH_TABLE_C
    #define HASH_TABLE_C
    #include "hash_table.h"
    
    void create_table(FILE *fp, SSTable *st)
    {
        int n;
        char str[MAX_LEN];
        int i;
    
    
        n=0;
        // 当读取 (n-1) 个字符时,或者读取到换行符时,或者到达文件末尾时,它会停止,具体视情况而定。
        while(fgets(str,MAX_LEN,fp)!=NULL)
        {
            n++;
        }
    
        fseek(fp,0,SEEK_SET);
    
        st->len=n;
        st->elem=(ElemType *)malloc(sizeof(ElemType)*(n+1));
    
        for(i=1;i<=n;i++)
        {
            st->elem[i].value=(Record)malloc(sizeof(char)*MAX_LEN);
            memset(st->elem[i].value,0,sizeof(char)*MAX_LEN);
            fscanf(fp,"%d %s",&(st->elem[i].key),st->elem[i].value);
        }
    
        return;    
    }
    
    void init_hash(HashPtr *hash_chain)
    {
        int i;
    
        for(i=0;i<m;i++)
        {
            *(hash_chain+i)=NULL;
        }
    
        return;
    }
    
    int hash_function(KeyType key)
    {
        return (key%m);
    }
    
    Result search_hash(HashPtr *hash, KeyType key)
    {
        Result res;
        int  k;
        HashPtr *p;
        HashPtr *pre_p;
        bool termination=false;
        bool found =false;
    
        p = (HashPtr *)malloc(sizeof(HashPtr));
        pre_p = (HashPtr *)malloc(sizeof(HashPtr));
    
        k=hash_function(key);
    
        if(hash[k]==NULL || ((hash[k]!=NULL) && key<= hash[k]->key))
        {
            res.flag=0;
            res.type=head;
            res.node_ptr=hash+k;
    
            if ((hash[k] != NULL) && key == hash[k]->key)
            {
                res.flag=1;
            }
    
            return res;
        }
    
        *p=hash[k];
        *pre_p=NULL;
    
        while ((*p) && !termination)
        {
           if((*p)->key==key)
           {
               termination = true;
               found =true;
           }
           else if (key < (*p)->key)
           {
              termination =true;
           }
           else
           {
                *pre_p=*p;
                *p=(*p)->next;
           }
        }
        
        if(found)
        {
            res.flag=1;
            res.type=intermediate;
            res.node_ptr=p;
            res.node_ptr_2=pre_p;
    
            return res;
        }
        else
        {
            res.flag = 0;
            res.type = intermediate;
            res.node_ptr = pre_p;
            res.node_ptr_2=p;
    
            return res;
        }
    }
    
    Status insert_hash(HashPtr *hash, KeyType key)
    {
        Result res;
        HashPtr new_node;
        HashPtr temp;
        res=search_hash(hash,key);
    
        if(res.flag==1)
        {
            return ERROR;
        }
        else
        {
            new_node=(HashPtr)malloc(sizeof(HashNode));
            new_node->key=key;
            new_node->next=NULL;
    
            if(res.type==head)
            {
                new_node->next=*(res.node_ptr);
                *(res.node_ptr)=new_node;
            }
            else
            {
                new_node->next = (*(res.node_ptr))->next;
                (*(res.node_ptr))->next = new_node;
            }
        }
    
        return OK;
    }
    
    Status delete_hash(HashPtr *hash, KeyType key)
    {
        Result res;
        HashPtr temp;
        res = search_hash(hash, key);
    
        if (res.flag == 0)
        {
            return ERROR;
        }
        else
        {
            if (res.type == head)
            {
                (*(res.node_ptr)) = (*(res.node_ptr))->next;
            }
            else The previous next will be the current next
            {
                (*(res.node_ptr_2))->next = (*(res.node_ptr))->next; 
            }
        }
    }
    
    #endif
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185

    4.3测试函数

    /**
     * @file hash_table_main.c
     * @author your name (you@domain.com)
     * @brief 
     * @version 0.1
     * @date 2023-05-21
     * 
     * @copyright Copyright (c) 2023
     * 
     */
    #ifndef HASH_TABLE_MAIN_C
    #define HASH_TABLE_MAIN_C
    #include "hash_table.c"
    
    int main(void)
    {
        int i;
        FILE *fp;
        SSTable st;
        HashPtr hash[m];
        
    
    
        fp=fopen("data.txt","r");
        create_table(fp,&st);
        init_hash(hash);
    
        for(i=1;i<=st.len;i++)
        {
            insert_hash(hash,st.elem[i].key);
        }
    
        delete_hash(hash,14);
        
        printf("This is the end of test\n");
        getchar();
        fclose(fp);
        return EXIT_SUCCESS;
    }
    
    
    #endif
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    1. 小结

    本文对哈希表、哈希函数以及处理哈希冲突的方法进行总结,并且利用C语言对实现了简单的哈希插入、查找和删除操作。

    参考资料:

    《数据结构》清华大学,严蔚敏

  • 相关阅读:
    机器人中的数值优化(十五)——PHR增广拉格朗日乘子法
    量子计算:数据安全难题
    如何获取combinations(a, 2)的结果,只能变量一遍,非常地花费时间。
    栈与队列5:逆波兰表达式求值
    Redis学习笔记( 入门篇)
    VirtualBox 进入虚拟机后,鼠标出不来了
    树莓派之树莓派系统安装
    码住!双11支付宝小程序云云市集,享骨折优惠最强购物清单,抽奖赢iPhone 15 Pro!
    大厂设计师力推的14款平面图设计工具!
    AJAX学习笔记3练习
  • 原文地址:https://blog.csdn.net/Jasonchen1224/article/details/130910501