zoukankan      html  css  js  c++  java
  • 回顾散列表的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

  • 相关阅读:
    HTTP协议中常用相应的状态码总结
    mysql 用户管理
    史上最全的mysql聚合函数总结(与分组一起使用)
    jQuery+masonry实现瀑布流
    MySQL Workbench 导入导出乱码解决方法
    在Google Maps 上点击标签显示说明并保持不消失
    在Google Maps 上点击标签后显示说明
    如何在Google Maps 添加多个标记
    如何在 Google 地图中添加标记和说明
    Google Maps API3 之 Hello World
  • 原文地址:https://www.cnblogs.com/NoMasp/p/4495383.html
Copyright © 2011-2022 走看看