散列表(HashTable)
散列表
i. 散列函数
i. 冲突解决
ii. 分离链表法
ii. 开放地址法
iii. 线性探测法
iii. 平方探测法
iii. 双散列
ii. 再散列
ii. 可扩散列
i. 装填因子:元素个数 / 散列表大小
C++实现
1. 冲突解决:分离链表法
散列函数:key % tableSize
/*
散列函数: key % tableSize
冲突解决: 分离链表法
*/
#include <iostream>
#include <vector>
#include <list> using namespace std; typedef int value; class HashTable
{
public:
HashTable(unsigned size) : _size(size)
{
_table = new vector<list<value>* >(size); for (size_t i = ; i < _size; i++)
{
(*_table)[i] = new list<value>;
}
} void insert(value val)
{
(*_table)[ val % _size ]->push_back(val);
} value find(value val)
{
list<value>::const_iterator it = (*_table)[val % _size]->cbegin(); while (it != (*_table)[val % _size]->cend())
{
if (*it == val)
return *it; it++;
} return NULL;
} ~HashTable()
{
for (size_t i = ; i < _size; i++)
{
delete (*_table)[i];
} delete _table;
} private:
vector<list<value>* >* _table;
unsigned _size;
}; int main()
{
HashTable h(); h.insert();
h.insert( + ); cout << h.find( + ); return ;
}
2. 冲突解决:开放地址法 之 线性探测
散列函数:(key + collisionNum) % TableSize
装填因子:散列大小 > 元素个数 × 2
/*
冲突解决:开放地址法 之 线性探测
散列函数:(key + collisionNum) % TableSize
装填因子:散列大小 > 元素个数 × 2
*/ #include <iostream>
#include <vector> using namespace std; class HashTable
{
friend void dump(const HashTable&); public:
HashTable(size_t size = ) : _size(size)
{
_table = new vector<int>(_size); for (size_t i = ; i < _size; i++)
{
_table->operator[](i) = NULL;
}
} void insert(int val)
{
for (unsigned i = ; i < _size; i++)
{
if (_table->operator[](_hash(val, i)) == NULL)
{
_table->operator[](_hash(val, i)) = val; return;
}
}
} int find(int val) const
{
for (unsigned i = ; i < _size; i++)
{
if (_table->operator[](_hash(val, i)) == val)
{
return _table->operator[](_hash(val, i));
}
} return NULL;
} ~HashTable()
{
delete _table;
} private:
size_t _hash(size_t val, unsigned collision) const
{
return (val + collision) % _size; //散列函数 线性探测
} vector<int>* _table;
size_t _size;
}; void dump(const HashTable& h)
{
vector<int>::const_iterator it = h._table->cbegin(); while (it != h._table->cend())
{
cout << it - h._table->cbegin() << " " << *it++ << endl;
}
} int main()
{
HashTable h(); h.insert();
h.insert();
h.insert();
h.insert();
h.insert(); dump(h); cout << h.find() << endl;
cout << h.find() << endl; return ;
}
3. 冲突解决:开放地址法 之 平方探测
散列函数:(key + collisionNum ^ 2) % TableSize
装填因子:散列大小 > 元素个数 × 2
/*
冲突解决:开放地址法 之 平方探测
散列函数:(key + collisionNum ^ 2) % TableSize
装填因子:散列大小 > 元素个数 × 2
*/ #include <iostream>
#include <vector> using namespace std; class HashTable
{
friend void dump(const HashTable&); public:
HashTable(size_t size = ) : _size(size)
{
_table = new vector<int>(_size); for (size_t i = ; i < _size; i++)
{
_table->operator[](i) = NULL;
}
} void insert(int val)
{
for (unsigned i = ; i < _size; i++)
{
if (_table->operator[]( _hash(val, i)) == NULL)
{
_table->operator[]( _hash(val, i)) = val; return;
}
}
} int find(int val) const
{
for (unsigned i = ; i < _size; i++)
{
if (_table->operator[](_hash(val, i)) == val)
{
return _table->operator[](_hash(val, i));
}
} return NULL;
} ~HashTable()
{
delete _table;
} private:
size_t _hash(size_t val, unsigned collision) const
{
return (val + collision * collision) % _size; //散列函数,平方探测
} vector<int>* _table;
size_t _size;
}; void dump(const HashTable& h)
{
vector<int>::const_iterator it = h._table->cbegin(); while (it != h._table->cend())
{
cout << it - h._table->cbegin() << " " << *it++ << endl;
}
} int main()
{
HashTable h(); h.insert();
h.insert();
h.insert();
h.insert();
h.insert(); dump(h); cout << h.find() << endl;
cout << h.find() << endl; return ;
}
散列表(HashTable)的更多相关文章
- JavaScript 散列表(HashTable)
TypeScript方式实现源码 // 特性: // 散列算法的作用是尽可能快地在数据结构中找到一个值. 在之前的章节中, 你已经知道如果 // 要在数据结构中获得一个值(使用get方法) ,需要遍历 ...
- 深入浅出数据结构C语言版(14)——散列表
我们知道,由于二叉树的特性(完美情况下每次比较可以排除一半数据),对其进行查找算是比较快的了,时间复杂度为O(logN).但是,是否存在支持时间复杂度为常数级别的查找的数据结构呢?答案是存在,那就是散 ...
- HashTable(散列表)
最近都在研究数据结构,关于hashtable,或者叫做散列表,过去一直不了解是什么东西,现在终于明白了. 所谓hashtable,就是某组key,通过某个关系(函数),得到一个与之对应的映射值(在计算 ...
- 数据结构--hashtable(散列表)
散列 散列又叫hash.是通过关键字把数据映射到指定位置的一种数据结构.理想的散列表,是一个包含关键字的固定大小的数组 哈希表存储的是键值对,其查找的时间复杂度与元素数量多少无关,哈希表在查找元素时是 ...
- 散列表碰撞处理、开链法、HashTable散列
散列表碰撞处理.开链法.HashTable散列 /** * 散列表碰撞处理.开链法.HashTable散列. * 将数组里的元素位置,也设置为数组,当两个数据的散列在同一个位置时, * 就可以放在这个 ...
- hashtable——散列表
2018-11-01 散列表---哈希表基于快速存取,时间换空间一种基于线性数组的线性表,不过元素之间并非紧密排列 散列函数--通过函数,有key关键码计算地址(相当于数组下标),函数尽可能使元素均匀 ...
- [转载] 散列表(Hash Table)从理论到实用(上)
转载自:白话算法(6) 散列表(Hash Table)从理论到实用(上) 处理实际问题的一般数学方法是,首先提炼出问题的本质元素,然后把它看作一个比现实无限宽广的可能性系统,这个系统中的实质关系可以通 ...
- [转载] 散列表(Hash Table)从理论到实用(中)
转载自:白话算法(6) 散列表(Hash Table)从理论到实用(中) 不用链接法,还有别的方法能处理碰撞吗?扪心自问,我不敢问这个问题.链接法如此的自然.直接,以至于我不敢相信还有别的(甚至是更好 ...
- [转载] 散列表(Hash Table) 从理论到实用(下)
转载自: 白话算法(6) 散列表(Hash Table) 从理论到实用(下) [澈丹,我想要个钻戒.][小北,等等吧,等我再修行两年,你把我烧了,舍利子比钻戒值钱.] ——自扯自蛋 无论开发一个程序还 ...
随机推荐
- SQL Fundamentals:Restricting and Sorting Data限制和排序数据(FROM-WHERE-SELECT-ORDER BY)
SQL Fundamentals || Oracle SQL语言 控制操作的显示列:基本的SELECT语句 控制行:限定查询和排序显示 分组统计查询 限定查询:WHERE字句 排序显示:ORDER B ...
- SQL Fundamentals || DCL(Data Control Language) || 角色ROLES
SQL Fundamentals || Oracle SQL语言 语句 解释 Create user Creates a user(usually performed by a DBA) Grant ...
- JavaScript学习11.30
window.history:包含浏览器的历史,可以不时用window这个前缀history.back():加载历史列表的前一个URLhistory.forward():加载历史列表的后一个URLwi ...
- Gym - 101149K Revenge of the Dragon 脑洞题,样例题
http://codeforces.com/gym/101149/problem/K 题意:题目贼长,但其实是个脑筋急转弯... 题解:题目要求某图形面积.该图形只有一个自由度,就是起点与终点距离x. ...
- Drip is a launcher for the Java Virtual Machine that provides much faster startup times than the java command. The drip script is intended to be a drop-in replacement for the java command, only faster
小结: 1.初始化jvm: 2.第一次唤醒java命令不快,后续快: https://github.com/elastic/logstash Advanced: Drip Launcher Drip ...
- python修饰器各种实用方法
This page is meant to be a central repository of decorator code pieces, whether useful or not <wi ...
- Jsoup爬虫解析
需要下载jsoup-1.8.1.jar包 jsoup 是一款Java 的HTML解析器,可直接解析某个URL地址.HTML文本内容.它提供了一套非常省力的API,可通过DOM,CSS以及类似于jQue ...
- jmeter连接mysql数据库报错Cannot create PoolableConnectionFactory (Could not create connection to database server.)
今天在学习jmeter的jdbc取样器,发现在配置完JDBC Connection Configuration和JDBC Request后,点击运行.在查看结果树中显示响应数据: Cannot cre ...
- 001-GPG入门教程
对信息加密和解密.需要用到GnuPG软件(简称GPG),它是目前最流行.最好用的加密工具之一. 一.什么是GPG 要了解什么是GPG,就要先了解PGP. 1991年,程序员Phil Zimmerman ...
- matlab常用方法
1:matlab进行符号的虚数运算 直接使用符号 a+b*j运算,结果是一个角度值,不是复数. 可以使用 a+b*(1j) 进行运算. 如下 position(index,)=radius; ...