散列

简介:

选择键值,冲突的时候采取不同的策略

散列函数:

简单的散列函数:

复制代码
1 int hash(const string & key,int tableSize)
2 {
3     int hashVal = 0;
4     for(int i = 0; i < key.length();++i)
5     {
6         hashVal + = key[i];
7     }
8     return hashVal % tableSize;
9 }
复制代码

比较好的散列函数:

复制代码
 1 int hash( const string & key,int tableSize )
 2 {
 3     int hashVal = 0;
 4     for(int i = 0; i <key.length();++i)
 5     {
 6         hashVal = 37*hashVal + key[i];
 7     }
 8     hashVal %= tableSize;
 9     if(hashVal < 0)
10         hashVal +=tableSize;
11 
12     return hashVal;
13 }
复制代码

键的长度和性质 影响选择。

分离链接法

分离链接散列表的类构架

复制代码
 1 template <typename HashedObj>
 2 class HashTable
 3 {
 4 public:
 5     explicit HashTable( int size = 101);
 6     bool contains (const HashedObj & x) const;
 7     void makeEmpty();
 8     void insert(const HashedObj & x);
 9     void remove(const HashedObj & x);
10 private:
11     vector<list<HashedObj> > theLists;
12     int currentSize;
13     void rehash();
14     int myhash( const HashedObj & x) const;
15 };
16 int hash(const string & key);
17 int hash(int key);
复制代码

散列表myhash的成员函数:

复制代码
1 int myhash(const HashedObj & x) const
2 {
3     int hashVal = hash(x);
4     hashVal %= theLists.size();
5     if(hashVal < 0)
6         hashVal += theLists.size();
7     return hashVal;
8 }
复制代码

使用name成员为键,提供的散列函数实例:

复制代码
 1 class Employee
 2 {
 3 public:
 4     const string & getName() const
 5     {
 6         return name;
 7     }
 8     bool operator==( const Employee & rhs ) const
 9     {
10         return getName() == rhs.getName();
11     }
12     bool operator!=(const Employee & rhs) const
13     {
14         return !(*this == rhs);
15     }
16 private:
17     string name;
18     double salary;
19     int seniority;
20 };
21 int hash(const Employee & item)
22 {
23     return hash(item.getName());
24 }
复制代码

实现makeEmpty contains remove:

复制代码
 1 void makeEmpty()
 2 {
 3     for(int i =0;i<theLists.size();i++)
 4         theLists[i].clear();
 5 }
 6 bool contains(const HashedObj & x) const
 7 {
 8     const list<HashedObj> & whichList = theLists[myhash(x)];
 9     return find(whichList.begin(),whichList.end(),x)!=whichList.end();
10 }
11 bool remove(const HashedObj & x) const
12 {
13     list<HashedObj> & whichList = theLists[myhash(x)];
14     list<HashedObj>::iterator itr = find(whichList.begin(),whichList.end(),x);
15 
16     if(itr == whichList.end())
17         return false;
18     
19     whichList.erase(itr);
20     --currentSize;
21     return true;
22 }
复制代码

分离散列表的insert实例

复制代码
 1 bool insert(const HashedObj & x)
 2 {
 3     list<HashedObj> & whichList = theLists[myhash(x)];
 4     if(find(whichList.begin(),whichList.end(),x)!=whichList.end())
 5         return false;
 6     whichList.push_back(x);
 7     if(++currentSize > theLists.size())
 8         rehash();
 9 
10     return true;
11 }
复制代码

装填因子:散列表中的元素个数 与 散列表大小的 比值

执行一次查找所需的时间:计算散列函数值所需要的常数时间加上遍历表所用的时间

不使用链表的散列表:

当冲突发生时,直接寻找下一单元

<线性探测>

<平方探测>

使用探测策略的散列表的类接口

复制代码
 1 template <typename HashedObj>
 2 class HashedObj
 3 {
 4 public:
 5     explicit HashTable(int size = 101);
 6     bool contains(const HashedObj & x) const;
 7     void makeEmpty();
 8     bool insert(const HashedObj & x);
 9     bool remove(const HashedObj & x);
10     enum EntryType{ACTIVE,EMPTY,DELETED};
11 private:
12     struct HashEntry
13     {
14         HashedObj element;
15         EntryType info;
16 
17         HashEntry(const HashedObj & e = HashedObj(),EntryType i = EMPTY):element(e),info(i){}
18     };
19     vector<HashEntry> array;
20     int currentSize;
21     bool isActive(int currentPos) const;
22     int findPos(const HashedObj & x) const;
23     void rehash();
24     int myhash(const HashedObj & x) const;
25 };
复制代码

初始化平方探测散列表

复制代码
 1 explicit HashTable(int size = 101):array(nextPrime(size))
 2 {
 3     makeEmpty();
 4 }
 5 void makeEmpty()
 6 {
 7     currentSize = 0;
 8     for(int i = 0 ; i < array.size(); i++)
 9         array[i].info = EMPTY;
10 }
复制代码

使用平方探测进行散列的contains findPos isActive

复制代码
 1 bool contains(const HashedObj & x) const
 2 {
 3     return isActive(findPos(x));
 4 }
 5 int findPos(const HashedObj & x) const
 6 {
 7     int offset = 1;
 8     int currentPos = myhash(x);
 9 
10     while(array[currentPos].info != EMPTY && array[currentPos].element != x)
11     {
12         currentPos += offset;
13         offset += 2;
14         if(currentPos >= array.size())
15             currentPos -= array.size();
16     }
17     return currentPos;
18 }
19 bool isActive(int currentPos) const
20 {
21     return array[currentPos].info == ACTIVE;
22 }
复制代码

使用平方探测的insert remove

复制代码
bool insert( const HashedObj & x)
{
    int currentPos = findPos(x);
    if(isActive( currentPos ))
        return false;
    array[currentPos] = HashEntry(x,ACTIVE);
    if(++currnetSize>array.size()/2)
        rehash();
}
bool remove(const HashedObj & x)
{
    int currentPos = findPos(x);
    if(!isActive(currentPos))
        return false;
    array[currentPos].info = DELETED;
    return true;
}
复制代码

<双散列>

对分离散列表的再散列

复制代码
 1 void rehash()
 2 {
 3     vector<HashEntry> oldArray = array;
 4     array.size(nextPrime(2*oldArray.size()));
 5     for(int j = 0; j < array.size(); j++)
 6     {
 7         array[j].info = EMPTY;
 8     }
 9     currentSize = 0;
10     for(int i = 0; i < array.size(); i++)
11     {
12         if(oldArray[i].info == ACTIVE)
13             insert(oldArray[i].element);
14     }
15 }
复制代码

对探测散列表的再散列

复制代码
 1 void rehash()
 2 {
 3     vector<list<HashedObj> > oldLists = theLists;
 4     theLists.resize( nextPrime( 2*theLists.size() ) );
 5     for(int j = 0; j < theLists.size(); j++)
 6     {
 7         theLists[j].clear();
 8     }
 9     currentSize = 0;
10     for(int i = 0; i < oldLists.size(); i++)
11     {
12         list<HashedObj>::iterator itr = oldLists[i].begin();
13         while( itr != oldLists[i].end( ) )
14             insert(*itr++);
15     }
16 }
复制代码

 

 

本文转自博客园xingoo的博客,原文链接:散列,如需转载请自行联系原博主。
相关文章
|
算法 机器学习/深度学习 数据安全/隐私保护
murmur3哈希算法
murmur3哈希算法 murmur3非加密哈希算法 murmur3非加密哈希算法导图 据算法作者Austin Appleby描述,有c1, c2, n 三个常量用大量测试数据调测出来的,可以对数值进行微调。
14489 0
|
4月前
|
存储 算法 Java
哈希算法篇 - 布隆过滤器
哈希算法篇 - 布隆过滤器
41 1
|
5月前
|
存储 算法 安全
哈希算法
哈希算法是单向加密技术,将任意数据转化为固定长度的唯一摘要。特征包括确定性、快速性、雪崩效应和单向性。应用广泛,如数据完整性校验、密码存储和哈希表。常见算法有MD5、SHA-1、SHA-256,选定时需注意安全性和抗碰撞能力。
43 3
|
12月前
|
存储 Serverless 索引
|
存储 算法 数据处理
哈希(散列)查找算法
本文主要是散列表的认识与哈希查找!
220 1
|
存储 算法 安全
哈希算法介绍
哈希算法是一种将任意长度的数据映射为固定长度的固定大小值的算法。它是一种单向函数,即无法从哈希值反推出原始数据。哈希算法在密码学、数据完整性校验、数据索引等领域有广泛的应用。
177 0
|
算法
散列,字符串hash初步
散列,字符串hash初步
|
算法 Serverless C++
|
存储 算法 C++
|
算法 安全 PHP
Hash算法详细介绍与实现(一)
Hash表(HashTable)又称散列表,通过把关键字Key映射到数组中的一个位置来访问记录,以加快查找速度,这个映射函数称为Hash函数,存放记录的数组称为Hash表.散列表是散列函数的一个主要应用(注意:关键字不是像在加密中所使用的那样是秘密的,但它们都是用来"解锁"或者访问数据的。)例如,在英语字典中的关键字是英文单词,和它们相关的记录包含这些单词的定义。在这种情况下,散列函数必须把按照字母顺序排列的字符串映射到为散列表的内部数组所创建的索引上。
362 1