算法导论11.2散列表Hash tables链式法解决碰撞11.3.1除法散列法
11.2是第11章的主要内容,11章叫散列表(Hash Tables)11.2也叫散列表(Hash Tables)
11.3节讲散列函数(比如除尘散列法),11.4节讲处理碰撞的另外一种方法区别于链式法技术
散列技术,有两个事情要做,一是先哈希函数(11.3),二是解决碰撞技术(11.2链式解决碰撞,11.4开放寻址解决碰撞)。
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 | |
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· ASP.NET Core 模型验证消息的本地化新姿势
· 对象命名为何需要避免'-er'和'-or'后缀
· SQL Server如何跟踪自动统计信息更新?
· AI与.NET技术实操系列:使用Catalyst进行自然语言处理
· 分享一个我遇到过的“量子力学”级别的BUG。
· AI Agent爆火后,MCP协议为什么如此重要!
· Draw.io:你可能不知道的「白嫖级」图表绘制神器
· dotnet 源代码生成器分析器入门
· ASP.NET Core 模型验证消息的本地化新姿势
· Java使用多线程处理未知任务数方案