算法导论11.2散列表Hash tables链式法解决碰撞11.3.1除法散列法

 

111824195113438

11.2是第11章的主要内容,11章叫散列表(Hash Tables)11.2也叫散列表(Hash Tables)

11.3节讲散列函数(比如除尘散列法),11.4节讲处理碰撞的另外一种方法区别于链式法技术

散列技术,有两个事情要做,一是先哈希函数(11.3),二是解决碰撞技术(11.2链式解决碰撞,11.4开放寻址解决碰撞)。

 

71)ST~[$Y`BV`NWTTHAX5@S

 

 

VS()S80]BWU197LCHQ@)HK4

 

BK419SSIX25MIDH0_D46K9J

 

8H~NTWNRT4E%[6@CVNK9YWY

1
  
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
/*
 * IA_11.2ChainedHash.h
 *
 *  Created on: Feb 13, 2015
 *      Author: sunyj
 */
 
#ifndef IA_11_2CHAINEDHASH_H_
#define IA_11_2CHAINEDHASH_H_
 
#include <iostream>
#include <string.h>
 
#include "IA_10.2LinkedLists.h"
 
// CHAINED-HASH-INSERT(T, x)
// insert x at the head of list T[h(x.key)]
 
// CHAINED-HASH-SEARCH(T, k)
// search for an element with key k in list T[h(k)]
 
// CHAINED-HASH-DELETE(T, x)
// delete x from the list T[h(x.key)]
 
template <class T> class ChainedHashTable {
public:
    ChainedHashTable(int64_t const n) : size(n)
    {
        data = new LinkedList<int64_t, T>[n]();
    }
    ~ChainedHashTable() {}
    int64_t HashFunc(int64_t const key)
    {
        return key % size;
    }
    Node<int64_t, T>* search(int64_t const key)
    {
        return data[HashFunc(key)].search(key);
    }
    // the user of this class, has to invoke search first
    // this interface assume that x was not in the hash table
    void insert(Node<int64_t, T>* x)
    {
        (data[HashFunc(x->key)]).insert(x);
    }
    void del(Node<int64_t, T>* x)
    {
        data[HashFunc(x->key)].del(x);
    }
    void print(int64_t key)
    {
        data[HashFunc(key)].print();
    }
private:
    LinkedList<int64_t, T>* data;
    int64_t const size;
};
 
#endif /* IA_11_2CHAINEDHASH_H_ */

 

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
/*
 * IA_11.2ChainedHash.cpp
 *
 *  Created on: Feb 12, 2015
 *      Author: sunyj
 */
 
#include "IA_11.2ChainedHash.h"
 
int main()
{
    /*
     * A prime not too close to an exact power of 2 is often a good choice for m. For
example, suppose we wish to allocate a hash table, with collisions resolved by
chaining, to hold roughly n = 2000 character strings, where a character has 8 bits.
We don't mind examining an average of 3 elements in an unsuccessful search, and
so we allocate a hash table of size m = 701. We could choose 701 because
it is a prime near 2000=3 but not near any power of 2.
     */
    ChainedHashTable<int64_t> table(701); // The division method,
 
    Node<int64_t, int64_t> node1(1, 100);
    Node<int64_t, int64_t> node4(4, 400);
    Node<int64_t, int64_t> node16(16, 1600);
    Node<int64_t, int64_t> node9(9, 900);
    if (nullptr == table.search(node1.key))
    {   // search before insert
        table.insert(&node1);
    }
    else
    {
        std::cout << "node1 already exist" << std::endl;
    }
    if (nullptr == table.search(node1.key))
    {
        table.insert(&node1);
    }
    else
    {
        std::cout << "node1 already exist" << std::endl;
    }
    table.insert(&node4);
    table.insert(&node16);
    table.insert(&node9);
    table.print(4);
    Node<int64_t, int64_t> node25(25, 2500);
    table.insert(&node25);
    table.print(16);
    // search before del, or you are clearly sure that, there are this node exist.
    // if node1 is not exist, and you invoke del, program will crush
    table.del(&node1);
    table.print(9);
    Node<int64_t, int64_t>* tmp;
    tmp = table.search(9);
    table.del(tmp);
    table.print(9);
    return 0;
}
1
  

 

 

98a42a1abb40c1ce157bd80e93a36684 (1)99ddd96b18c1e4fd73533e1562c1f37a

131a68cdb61fceb569b924f006c749f5

posted @   孙永杰  阅读(625)  评论(0编辑  收藏  举报
编辑推荐:
· ASP.NET Core 模型验证消息的本地化新姿势
· 对象命名为何需要避免'-er'和'-or'后缀
· SQL Server如何跟踪自动统计信息更新?
· AI与.NET技术实操系列:使用Catalyst进行自然语言处理
· 分享一个我遇到过的“量子力学”级别的BUG。
阅读排行:
· AI Agent爆火后,MCP协议为什么如此重要!
· Draw.io:你可能不知道的「白嫖级」图表绘制神器
· dotnet 源代码生成器分析器入门
· ASP.NET Core 模型验证消息的本地化新姿势
· Java使用多线程处理未知任务数方案
点击右上角即可分享
微信分享提示