回顾散列表的7点

1.散列表(hash table)的实现成为散列(hashing),是一种以常数平均时间执行输入、删除和查找的技术。可是那些须要元素间任何排序信息的数操做将不会获得有效的支持。算法

2.散列函数示例bash

int hash(const string & key, int tableSize)
{
    int hashVal=0;
    for(int i=0;i<key.length();i++)
        hashVal=37*hashVal+key[i];
    hashVal %= tableSize;
    if(hashVal<0)
        hashVal+=tableSize;
    return hashVal;

3.散列表是由键值对来提供动力的,所以若是在值相同而键不一样的状况下就会发生冲突。那么解决冲突的办法,有一种叫作分离连接法(separate chaining),它将散列到同一个值得全部元素都保留到一个链表中。markdown

这里写图片描述

分离连接散列表的类构架:数据结构

template <typename HashedObj>
class HashTable
{
public:
    explicit HashTable(int size=101);
    bool contains(const HashedObj & x) const;

    void makeEmpty();
    void insert(const HashedObj & x);
    void remove(const HashedObj & x);

private:
    vector<list<HashedObj>> theLists;
    int currentSize;

    void rehash();
    int myhash(const HashedObj & x) const;
};

int hash(const string & key);
int hash(int key);
int myhash(const HashedObj & x) const
{
    int hashVal=hash(x);
    hashVal %= theLists.size();
    if(hashVal<0)
        hashVal+=theLists.size();
    return hashVal;
}

4.分离连接散列表的insert函数函数

bool insert(const HashedObj & x)
{
    list<HashedObj> & whichList=theLists[myhash(x)];
    if(find(whichList.begin(),whichList.end(),x)!=whichList.end())
        return false;
    whichList.push_back(x);

    if(++currentSize>theLists.size())
        rehash();
    return true;
}

5.分离连接散列表算法的缺点是使用了一些链表,因为给新单元分配地址须要时间,所以这就致使算法的速度有些减慢,同时算法实际上还要求第二种数据结构的实现。所以探测散列表就应运而生。它又包含了3种探测方式。性能

线性探测ui

这里写图片描述

这里写图片描述

平方探测spa

这里写图片描述

双散列.net

这里写图片描述

6.若是散列表已经不足以来存放你的数据,那么能够考虑使用可扩散列(extendible hashing)。code

这里写图片描述

这里写图片描述

这里写图片描述

7.对于分散连接散列法,虽然装填因子不大时性能并不明显下降,但装填因子仍是应该接近于1.对于探测散列,除非彻底不可避免,不然装填因子不该该超过0.5.若是用线性探测,那么性能随着装填因子接近于1而急速降低。再扩散运算能够经过使表增加和收缩来保持合理的装填因子。


欢迎你们点击左上角的“关注”或右上角的“收藏”方便之后阅读。


为使本文获得斧正和提问,转载请注明出处:
http://blog.csdn.net/nomasp

相关文章
相关标签/搜索