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. Spring Cloud Stream 核心概念

    Spring Cloud Stream简介 Spring cloud stream是一个构建与Spring Boot和Spring Integration之上的框架,方便开发人员快速构建基于Messa ...

  2. 盘一盘 NIO (一)—— Buffer源码解析

    Buffer是个啥? Buffer 即缓冲区,用来暂存输入输出数据的区域.Buffer对象是一份固定数量的数据的容器,实质上是一个数组.但是一个缓冲区不仅仅是一个数组,缓冲区提供了对数据的结构化访问, ...

  3. mongo常用语法

    首先要能进入控制台,进不去自己解决. 基本操作: show users:显示用户 show dbs:显示数据库列表 use <db name> 切换/创建数据库 show collecti ...

  4. 《Java 8 in Action》Chapter 6:用流收集数据

    1. 收集器简介 collect() 接收一个类型为 Collector 的参数,这个参数决定了如何把流中的元素聚合到其它数据结构中.Collectors 类包含了大量常用收集器的工厂方法,toLis ...

  5. 实战redhat6.5离线升级openssl&openssh

    记录一次RedHat6.5升级openssl&openssh踩坑填坑.由于机房信息安全员用绿盟扫描出服务器openssh有8个重要的安全漏洞,最好的解决方式就是升级版本. 注意事项: 先升级o ...

  6. 多线程环境中安全使用集合API(含代码)

    转自: http://blog.csdn.net/ns_code/article/details/17200509 在集合API中,最初设计的Vector和Hashtable是多线程安全的.例如:对于 ...

  7. JSP学习笔记(3)——JSTL 标签库

    JSP Standard Tag Lib,名为JSP标准标签库,设计的目的主要用来方便我们将数据输出,而不是使用JSP中的语法<% %> <%= %> <%! %> ...

  8. nginx安装错误:c compiler cc is not found

    今天安装软件nginx的时候遇到的报错:c compiler cc is not found 查了下网上的资料,解决方案也不复杂. 先说明下环境: 服务器:CentOS 7 nginx:2.3.1 原 ...

  9. Oracle大量数据更新策略

    生产上要修改某个产品的产品代号, 而我们系统是以产品为中心的, 牵一发而动全身, 涉及表几乎覆盖全部, 有些表数据量是相当大的, 达到千万, 亿级别. 单纯的维护产品代号的 SQL 是不难的, 但是性 ...

  10. 证书pfx转jks

    keytool -importkeystore -srckeystore  2756649_order.hanels-home.com.pfx -srcstoretype pkcs12 -destke ...