散列表(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) 从理论到实用(下) [澈丹,我想要个钻戒.][小北,等等吧,等我再修行两年,你把我烧了,舍利子比钻戒值钱.] ——自扯自蛋 无论开发一个程序还 ...
随机推荐
- 【转】.NET 应用程序是怎么运行的
原文:http://www.cnblogs.com/xishuai/p/mono-dotnetcore.html .NET应用程序运行过程 C#程序运行过程 CLR结构
- Spyer中添加一些常用包的方法
我用的是Anaconda中的Spyer编译,在导入包pyaudio时,发现找不到,需要手工导入.可以打开Anacoda promt,查看已经安装的包名用:pip list Spyer中的所有包在这里安 ...
- html中label及加上属性for之后的用法
定义和用法 <label> 标签为 input 元素定义标签(label). label 元素不会向用户呈现任何特殊的样式.不过,它为鼠标用户改善了可用性,因为如果用户点击 label 元 ...
- javaScript错误(一)Cannot call method 'addEventListener' of null
Cannot call method 'addEventListener' of null 原因很简单,JavaScript代码中要引用到DOM对象,但是这个DOM对象在JavaScript执行后才会 ...
- 理解SQL Server中索引的概念,原理
转自:http://www.cnblogs.com/CareySon/archive/2011/12/22/2297568.html 简介 在SQL Server中,索引是一种增强式的存在,这意味着, ...
- android activity and fragment活动周期
1.状态 /* 每个活动一共有四种状态 *:1.运行状态,就是栈顶的那个 * 2.暂停状态:就是不处于栈顶,但是依然可见,比如对话框下面的界面 * 3.停止状态:不处于栈顶,并且不可见 * 4.销毁状 ...
- 9 jmeter之检查点
jmeter有类似loadrunner检查点的功能,就是断言中的响应断言. 1.响应断言(对返回文字结果进行相应的匹配)右击请求-->添加-->断言-->响应断言-->添加“要 ...
- 009-java中常用的单个键值对
1.Java 6提供AbstractMap.SimpleEntry<K,V>和AbstractMap.SimpleImmutableEntry<K,V> Map.Entry&l ...
- 连接数据库工具类DBUtil
代码如下: import java.io.FileNotFoundException; import java.io.IOException; import java.sql.Connection; ...
- [LeetCode] 298. Binary Tree Longest Consecutive Sequence_Medium tag: DFS recursive
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...