散列表

    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)的更多相关文章

  1. JavaScript 散列表(HashTable)

    TypeScript方式实现源码 // 特性: // 散列算法的作用是尽可能快地在数据结构中找到一个值. 在之前的章节中, 你已经知道如果 // 要在数据结构中获得一个值(使用get方法) ,需要遍历 ...

  2. 深入浅出数据结构C语言版(14)——散列表

    我们知道,由于二叉树的特性(完美情况下每次比较可以排除一半数据),对其进行查找算是比较快的了,时间复杂度为O(logN).但是,是否存在支持时间复杂度为常数级别的查找的数据结构呢?答案是存在,那就是散 ...

  3. HashTable(散列表)

    最近都在研究数据结构,关于hashtable,或者叫做散列表,过去一直不了解是什么东西,现在终于明白了. 所谓hashtable,就是某组key,通过某个关系(函数),得到一个与之对应的映射值(在计算 ...

  4. 数据结构--hashtable(散列表)

    散列 散列又叫hash.是通过关键字把数据映射到指定位置的一种数据结构.理想的散列表,是一个包含关键字的固定大小的数组 哈希表存储的是键值对,其查找的时间复杂度与元素数量多少无关,哈希表在查找元素时是 ...

  5. 散列表碰撞处理、开链法、HashTable散列

    散列表碰撞处理.开链法.HashTable散列 /** * 散列表碰撞处理.开链法.HashTable散列. * 将数组里的元素位置,也设置为数组,当两个数据的散列在同一个位置时, * 就可以放在这个 ...

  6. hashtable——散列表

    2018-11-01 散列表---哈希表基于快速存取,时间换空间一种基于线性数组的线性表,不过元素之间并非紧密排列 散列函数--通过函数,有key关键码计算地址(相当于数组下标),函数尽可能使元素均匀 ...

  7. [转载] 散列表(Hash Table)从理论到实用(上)

    转载自:白话算法(6) 散列表(Hash Table)从理论到实用(上) 处理实际问题的一般数学方法是,首先提炼出问题的本质元素,然后把它看作一个比现实无限宽广的可能性系统,这个系统中的实质关系可以通 ...

  8. [转载] 散列表(Hash Table)从理论到实用(中)

    转载自:白话算法(6) 散列表(Hash Table)从理论到实用(中) 不用链接法,还有别的方法能处理碰撞吗?扪心自问,我不敢问这个问题.链接法如此的自然.直接,以至于我不敢相信还有别的(甚至是更好 ...

  9. [转载] 散列表(Hash Table) 从理论到实用(下)

    转载自: 白话算法(6) 散列表(Hash Table) 从理论到实用(下) [澈丹,我想要个钻戒.][小北,等等吧,等我再修行两年,你把我烧了,舍利子比钻戒值钱.] ——自扯自蛋 无论开发一个程序还 ...

随机推荐

  1. 2.1TF模型持久化

    目前tf只能保存模型中的variable变量,整个模型还不能保存,版本1.x 保存模型代码 import tensorflow as tf import numpy as np # Save to f ...

  2. 51nod 1432 - 独木舟 - [贪心]

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1432 基准时间限制:1 秒 空间限制:131072 KB   ...

  3. ECNU 3247 - 铁路修复计划

    Time limit per test: 2.0 seconds Time limit all tests: 15.0 seconds Memory limit: 256 megabytes 在 A ...

  4. JQUERY中各个ajax函数

    1.$(selecter).load()     --- load() 方法从服务器加载数据,并把返回的数据放入被选元素中 2.$.get(url,callback()) 3.$.post(url,d ...

  5. Ubuntu下安装vsftpd

    1.sudo apt-get install vsftpd 2.修改配置文件 sudo gedit /etc/vsftpd.conf write_enable=YES ls_recurse_enabl ...

  6. MySQL5.6命令笔记

    授权root用户在远程终端访问 ' WITH GRANT OPTION;

  7. java string.getBytes(“UTF-8”) javascript equivalent

    1. byte[] bytes = "test.message".getBytes("UTF-8"); //result: [116, 101, 115, 11 ...

  8. C#静态类,静态构造函数,静态变量

    静态变量位于栈上,它是一个全局变量,在编译期就已经生成. public class Cow public static int count; private int id; { id = ++coun ...

  9. 杀死正在运行的进程: linux

    1:杀死正在运行的进程:使用ps -aux|grep labor   查出进程PID 2:使用kill  PID  将进程杀死.

  10. [vue]v-bind: sytle/class-bind&属性值绑定

    v-bind - style绑定 - class绑定 - 属性值绑定 <!DOCTYPE html> <html lang="en"> <head&g ...