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. 如何美观地打印 Python 对象?这个标准库可以简单实现

    前不久,我写了一篇文章回顾 Python 中 print 的发展历史 ,提到了两条发展线索: 明线:早期的 print 语句带有 C 和 Shell 的影子,是个应用程序级的 statement,在最 ...

  2. node.js 初学 自我笔记整理 day01

     node.js   概念问题: Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境.   npm是一个node的包管理工具  ,也是一个网站  ,还是一条命令.N ...

  3. GIS基础知识 - 坐标系、投影、EPSG:4326、EPSG:3857

    最近接手一个GIS项目,需要用到 PostGIS,GeoServer,OpenLayers 等工具组件,遇到一堆地理信息相关的术语名词,在这里做一个总结. 1. 大地测量学 (Geodesy) 大地测 ...

  4. iOS Autoresizing Autolayout Size classes

    Autoresizing:出现最早,仅仅能够针对父控件做约束(注意:要关闭Autolayout&Size classes才能够看到Autoresizing) 代码对应: UIView.h中的a ...

  5. P2157 [SDOI2009]学校食堂 状压DP

    题意: 排队买饭,时间为前一个人和后一个人的异或和,每个人允许其后面B[i] 个人先买到饭,问最少的总用时. 思路: 用dp[i][j][k] 表示1-i-1已经买好饭了,第i个人后面买饭情况为j,最 ...

  6. Atcode B - Colorful Hats(思维)

    题目链接:http://agc016.contest.atcoder.jp/tasks/agc016_b 题解:挺有意思的题目主要还是模拟出最多有几种不可能的情况,要知道ai的差距不能超过1这个想想就 ...

  7. vs 模板更新

    vs 模板更新,执行命令: dotnet new --install McMaster.DotNet.GlobalTool.Templates

  8. Mysql高手系列 - 第8篇:详解排序和分页(order by & limit),及存在的坑

    这是Mysql系列第8篇. 环境:mysql5.7.25,cmd命令中进行演示. 代码中被[]包含的表示可选,|符号分开的表示可选其一. 本章内容 详解排序查询 详解limit limit存在的坑 分 ...

  9. KafkaProducer源码分析

    Kafka常用术语 Broker:Kafka的服务端即Kafka实例,Kafka集群由一个或多个Broker组成,主要负责接收和处理客户端的请求 Topic:主题,Kafka承载消息的逻辑容器,每条发 ...

  10. Python面向对象编程——继承与派生

    Python面向对象编程--继承与派生 一.初始继承 1.什么是继承 继承指的是类与类之间的关系,是一种什么"是"什么的关系,继承的功能之一就是用来解决代码重用问题. 继承是一种创 ...