Hashing Process

关于hash本身,解决冲突是一个小重点,如下图。

代码实现分析

—— 定义HashTable类

一、数据结构

    def __init__(self):
self.size = 11
self.slots = [None] * self.size
self.data = [None] * self.size

二、Hashing策略

注意,因“冲突”而导致的rehash不是原本的"key+1",而是"key的hash结果+1"。用的是”线性解决冲突“的策略。

    def bashfunction(self, key, size):
return key%size def rehash(self, oldhash, size):
return (oldhash+1)%size

三、设置

理解的关键,有冲突时找下一个位置,

    def put(self, key, data):
hashvalue = self.hashfunction(key, len(self.slots)) if self.slots[hashvalue] == None
# 第一次出现,则直接添加
self.slots[hashvalue] = key
self.data[hashvalue] = data
else:
if self.slots[hashvalue] == key:
#已经有了则“更新”数值
self.data[hashvalue] = data
else:
# 有值但key不是,说明“被占”了,那就循环直到”没冲突“时
nextslot = self.rehash(bashvalue, len(self.slots))
while self.slots[nextslot] != None and self.slots[nextslot] != key:  # ”被占“:非空,且key不对
nextslot = self.rehash(nextslot, len(self.slots)) # 找到位置后,看位置的具体情况;
if self.slots[nextslot] == None:
# append new key.
self.slots[nextslot] = key
self.data[nextslot] = data
else:
# update existing key's value.
self.data[nextslot] = data

四、获取

值得注意的是:”没找到“的标示是又回到了原来的起始位置。这也是线性探测的特点。

    def get(self, key):
startslot = self.hashfuncion(key, len(self.slots)) data = None
stop = False
found = False
position = startslot while self.slots[position] != None and not found and not stop:
if self.slots[position] == key:
found = True
data = self.data[position]
else:
# update 'position', 冲突解决之‘线性探测’
position = self.rehash(position, len(self.slots))
if position == startslot:
# 是真没有这个key
stop = True return data

五、类的 ”字典化“

    def __getitem__(self, key):
return self.get(key) def __setitem__(self, key, data):
self.put(key, data)

一个简单的例子:

class Tag:
def __init__(self):
self.change={'python':'This is python'} def __getitem__(self, item):
print('这个方法被调用')
return self.change[item] a=Tag()
print(a['python'])

End.

[Algorithm] Hashing for search的更多相关文章

  1. PAT-1145(Hashing - Average Search Time)哈希表+二次探测解决冲突

    Hashing - Average Search Time PAT-1145 需要注意本题的table的容量设置 二次探测,只考虑正增量 这里计算平均查找长度的方法和书本中的不同 #include&l ...

  2. PAT 1145 Hashing - Average Search Time [hash][难]

    1145 Hashing - Average Search Time (25 分) The task of this problem is simple: insert a sequence of d ...

  3. [PAT] 1143 Lowest Common Ancestor(30 分)1145 Hashing - Average Search Time(25 分)

    1145 Hashing - Average Search Time(25 分)The task of this problem is simple: insert a sequence of dis ...

  4. PAT_A1145#Hashing - Average Search Time

    Source: PAT A1145 Hashing - Average Search Time (25 分) Description: The task of this problem is simp ...

  5. PAT 甲级 1145 Hashing - Average Search Time (25 分)(读不懂题,也没听说过平方探测法解决哈希冲突。。。感觉题目也有点问题)

    1145 Hashing - Average Search Time (25 分)   The task of this problem is simple: insert a sequence of ...

  6. PAT A1145 Hashing - Average Search Time (25 分)——hash 散列的平方探查法

    The task of this problem is simple: insert a sequence of distinct positive integers into a hash tabl ...

  7. hdu, KMP algorithm, linear string search algorithm, a nice reference provided 分类: hdoj 2015-07-18 13:40 144人阅读 评论(0) 收藏

    reference: Rabin-Karp and Knuth-Morris-Pratt Algorithms By TheLlama– TopCoder Member https://www.top ...

  8. 1145. Hashing - Average Search Time

      The task of this problem is simple: insert a sequence of distinct positive integers into a hash ta ...

  9. PAT 甲级 1145 Hashing - Average Search Time

    https://pintia.cn/problem-sets/994805342720868352/problems/994805343236767744 The task of this probl ...

随机推荐

  1. Glide3升级到Glide4碰到的问题汇总以及部分代码修改

    版权声明:本文为xing_star原创文章,转载请注明出处! 本文同步自http://javaexception.com/archives/188 Glide.3x的版本是3.7.0,Glide4.x ...

  2. 入门MySQL——用户与权限

    前言:  前面几篇文章为大家介绍了各种SQL语法的使用,本篇文章将主要介绍MySQL用户及权限相关知识,如果你不是DBA的话可能平时用的不多,但是了解下也是好处多多. 1.创建用户 官方推荐创建语法为 ...

  3. Python-demo(listen)

    import requests import json # 爬虫原理 模拟浏览器 获取请求数据 #点击播放连接 #url = "https://www.ximalaya.com/revisi ...

  4. JavaScript数组方法详解

    JavaScript数组方法详解 JavaScript中数组的方法种类众多,在ES3-ES7不同版本时期都有新方法:并且数组的方法还有原型方法和从object继承的方法,这里我们只介绍数组在每个版本中 ...

  5. Unity官方案例精讲_2015_优化

    1.将公共变量从Inspector视图中隐藏:    [HideInInspector] [HideInInspector] public GameObject player; 2.限定Inspect ...

  6. 王某人从0开始学习lorawan的笔记_0

    最近老板想做lorawan的项目,交给我了,我也应承下来了,但是!!!我TM连lorawan是啥子我都不知道啊啊啊啊啊! 真希望我女朋友可以看穿我的倔强,给我1千万,让我专心当舔狗,等等,我的女朋友? ...

  7. Spring Cloud Alibaba | Nacos动态网关路由

    Spring Cloud Alibaba | Gateway基于Nacos动态网关路由 本篇实战所使用Spring有关版本: SpringBoot:2.1.7.RELEASE Spring Cloud ...

  8. spss分析存在共性线后,接下来是怎么分析?

    在进行线性回归分析时,容易出现自变量(解释变量)之间彼此相关,这种情况被称作多重共线性问题. 适度的多重共线性不成问题,但当出现严重共线性问题时,可能导致分析结果不稳定,出现回归系数的符号与实际情况完 ...

  9. Python数据类型详解——字典

    Python数据类型详解--字典 引子 已经学习了列表,现在有个需求--把公司每个员工的姓名.年龄.职务.工资存到列表里,你怎么存? staff_list = [ ["Kwan", ...

  10. CodeForces 86 D Powerful array 莫队

    Powerful array 题意:求区间[l, r] 内的数的出现次数的平方 * 该数字. 题解:莫队离线操作, 然后加减位置的时候直接修改答案就好了. 这个题目中发现了一个很神奇的事情,本来数组开 ...