回顾散列表的7点

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

2.散列函数示例

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),它将散列到同一个值得所有元素都保留到一个链表中。

这里写图片描述

分离链接散列表的类构架:

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种探测方式。

线性探测

这里写图片描述

这里写图片描述

平方探测

这里写图片描述

双散列

这里写图片描述

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

这里写图片描述

这里写图片描述

这里写图片描述

7.对于分散链接散列法,虽然装填因子不大时性能并不明显降低,但装填因子还是应该接近于1.对于探测散列,除非完全不可避免,否则装填因子不应该超过0.5.如果用线性探测,那么性能随着装填因子接近于1而急速下降。再扩散运算可以通过使表增长和收缩来保持合理的装填因子。


欢迎大家点击左上角的“关注”或右上角的“收藏”方便以后阅读。


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

posted @ 2015-05-09 17:31  nomasp  阅读(186)  评论(0编辑  收藏  举报