[Algorithm] Hashing for search
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的更多相关文章
- PAT-1145(Hashing - Average Search Time)哈希表+二次探测解决冲突
Hashing - Average Search Time PAT-1145 需要注意本题的table的容量设置 二次探测,只考虑正增量 这里计算平均查找长度的方法和书本中的不同 #include&l ...
- 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 ...
- [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 ...
- PAT_A1145#Hashing - Average Search Time
Source: PAT A1145 Hashing - Average Search Time (25 分) Description: The task of this problem is simp ...
- PAT 甲级 1145 Hashing - Average Search Time (25 分)(读不懂题,也没听说过平方探测法解决哈希冲突。。。感觉题目也有点问题)
1145 Hashing - Average Search Time (25 分) The task of this problem is simple: insert a sequence of ...
- 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 ...
- 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 ...
- 1145. Hashing - Average Search Time
The task of this problem is simple: insert a sequence of distinct positive integers into a hash ta ...
- PAT 甲级 1145 Hashing - Average Search Time
https://pintia.cn/problem-sets/994805342720868352/problems/994805343236767744 The task of this probl ...
随机推荐
- 从零写一个编译器(十三):代码生成之遍历AST
项目的完整代码在 C2j-Compiler 前言 在上一篇完成对JVM指令的生成,下面就可以真正进入代码生成部分了.通常现代编译器都是先把生成IR,再经过代码优化等等,最后才编译成目标平台代码.但是时 ...
- TDH 安装 TDH-Client
1. TDH-Client 下载 (下载分享:链接:https://pan.baidu.com/s/1ZmP4BUCiuRypCtsoAuvKRA 提取码:xsbl ) tar -vxf td ...
- SPP NET (Spatial Pyramid Pooling in Deep Convolutional Networks for Visual Recognition)
1. https://www.cnblogs.com/gongxijun/p/7172134.html (SPP 原理) 2.https://www.cnblogs.com/chaofn/p/9305 ...
- Excel VBA 在保留原单元格数据的情况下,将计算的百分比加在后面
算的是红框占绿框的百分比 难点在保留原数据的情况下,把百分比加在后面.通过公式我是不会,但程序实现也不难. 先在Excel中的开发工具中打开visual basic,或者用宏也可以 导入代码文件,代码 ...
- 人员考勤,MySQLl数据库一个表自动生成3表筛选人员迟到早退缺勤
前言:漂亮的人事小姐姐找我帮忙弄考勤:由于人员考勤和门禁一起,打卡记录太多,打卡机只能导出一个打卡Excel总表,不容易人工筛选. Excel表的格式是这样的:(这里101代替真实人名) 实现目标: ...
- Spring框架的重要问题
这篇文章总结了一些关于Spring框架的重要问题,这些问题都是你在面试或笔试过程中可能会被问到的. 目录 Spring概述 依赖注入 Spring Beans Spring注解 Spring的对象访问 ...
- MySQL数据库笔记六:数据定义语言及数据库的备份和修复
1. MySQL中的函数 <1>加密函数 password(str) 该函数可以对字符串str进行加密,一般情况下,此函数给用户密码加密. select PASSWORD('tlxy666 ...
- C++ switch注意事项(陷阱)
话不多说,直接上代码 int a; printf("请输入一个整数:"); scanf("%d", &a); switch (a) { : printf ...
- TK可视化之文件内容查找
1.内容输出类 import tkinter import python基础.day15.搜索数据可视化.BigDataFind class InputViem(): def __init__(sel ...
- codeforces 27 E. Number With The Given Amount Of Divisors(数论+dfs)
题目链接:http://codeforces.com/contest/27/problem/E 题意:问因数为n个的最小的数是多少. 题解:一般来说问到因数差不多都会想到素因子. 任意一个数x=(p1 ...