# hash table 2
class ListNode(object):
def __init__(self, key, val, next=None):
self.key = key
self.val = val
self.next = next
class hashTable2(object):
def __init__(self):
self.capacity = 4
self.size = 0
self.table = [None for _ in range(self.capacity)]
def put(self, key, value):
# new key insert, increase size
if self.get(key) is None:
self.size += 1
self.addListNode(self.table, key, value, self.capacity)
if self.size >= self.capacity * 0.7:
self.rehashing(self.capacity * 2)
# find value, O(1 + k), k is the average length of the node list
def get(self, key):
# get the index with the hash of key
index = self.indexFor(key, self.capacity)
# no nodeList, return None
if self.table[index] == None:
return None
# traverse through the listNode to find the key, value
node = self.table[index]
while node:
if node.key == key:
return node.val
node = node.next
return None
def rehashing(self, newCapacity):
self.capacity = newCapacity # update capacity
self.size = 0
newTable = [None] * newCapacity
# transfer old table to newTable
for node in self.table:
while node:
self.addListNode(newTable, node.key, node.val, newCapacity)
self.size += 1
node = node.next
self.table = newTable # update old table reference to newTable
def addListNode(self, table, key, value, length):
index = self.indexFor(key, length)
if table[index] == None:
table[index] = ListNode(key, value)
return
# traverse through nodelist to find the key match
prev = table[index]
cur = table[index]
while cur:
if cur.key == key:
cur.val = value
return
prev = cur
cur = cur.next
# no key match, add new ListNode(key, val) add the tail
prev.next = ListNode(key, value)
def indexFor(self, key, length):
return hash(key) % length
table = hashTable2()
table.put("a", 1)
table.put("b", 2)
table.put("c", 3)
table.put('d', 10)
table.put('e', 11)
table.put('f', 12)
table.put('a', 10)
print(table.get("a"))
print(table.get("b"))
print(table.get("c"))
print(table.get("d"))
print(table.get("e"))
print(table.get("f"))
print("capacity", table.capacity)
hashtable
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門国裳,熙熙樓的掌柜王于貴愁眉苦臉地迎上來形入,“玉大人,你說我怎么就攤上這事缝左∫谒欤” “怎么了浓若?”我有些...
- 文/不壞的土叔 我叫張陵,是天一觀的道長蛇数。 經(jīng)常有香客問我挪钓,道長,這世上最難降的妖魔是什么耳舅? 我笑而不...
- 正文 為了忘掉前任碌上,我火速辦了婚禮,結(jié)果婚禮上浦徊,老公的妹妹穿的比我還像新娘馏予。我一直安慰自己,他們只是感情好盔性,可當(dāng)我...
- 文/花漫 我一把揭開白布霞丧。 她就那樣靜靜地躺著,像睡著了一般冕香。 火紅的嫁衣襯著肌膚如雪蛹尝。 梳的紋絲不亂的頭發(fā)上,一...
- 文/蒼蘭香墨 我猛地睜開眼务漩,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了它褪?” 一聲冷哼從身側(cè)響起饵骨,我...
- 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎茫打,沒想到半個(gè)月后居触,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡老赤,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年轮洋,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片抬旺。...
- 正文 年R本政府宣布,位于F島的核電站碾褂,受9級(jí)特大地震影響兽间,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜正塌,卻給世界環(huán)境...
- 文/蒙蒙 一嘀略、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧乓诽,春花似錦屎鳍、人聲如沸。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至粮宛,卻和暖如春窥淆,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背巍杈。 一陣腳步聲響...
- 正文 我出身青樓词裤,卻偏偏與公主長得像,于是被迫代替她去往敵國和親鳖宾。 傳聞我的和親對(duì)象是個(gè)殘疾皇子吼砂,可洞房花燭夜當(dāng)晚...
推薦閱讀更多精彩內(nèi)容
- Enumeration接口 該接口較為古老,但在維護(hù)以前的程序時(shí)就會(huì)頻繁遇到撑帖。枚舉Enumeration接口蓉坎,作用...
- 前言 面試官問ConcurrentHashMap和HashTable的區(qū)別,當(dāng)時(shí)很緊張胡嘿,就回答了HashTable...
- java—HashMap與Hashtable的源碼比較 一蛉艾、前言 一直都知道HashMap是常考的,所以今天把Ha...
- 作者:馬以讓 晚飯時(shí)逢享,玲沒吃幾口就情緒反常地向臥室走去罐监。 春急忙跟過去,問:“咋了瞒爬?哪里不舒服弓柱?” 玲沒帶好氣地說...