【数据结构】回顾散列表

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


  • 6
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值