[3] add hash table
This commit is contained in:
parent
85a4936d4c
commit
c3ed1e09f1
3
pomelovsd/DataStruct/BinaryTree.py
Normal file
3
pomelovsd/DataStruct/BinaryTree.py
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
def create_node(name,phone):
|
||||
return {"name": name, "phone": phone, "left": None, "right": None}
|
||||
|
||||
30
pomelovsd/DataStruct/HashTable.py
Normal file
30
pomelovsd/DataStruct/HashTable.py
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
from LinkedList import ll_insert, ll_find, ll_delete, ll_list_all
|
||||
|
||||
def ht_create(size = 1000):
|
||||
return[None] * size
|
||||
|
||||
def hash_function(name, size = 1000):
|
||||
value = 0
|
||||
for char in name:
|
||||
value = (value * 31 + ord(char)) % size
|
||||
return value
|
||||
|
||||
def ht_insert(buckets, name, phone):
|
||||
index = hash_function(name, len(buckets))
|
||||
buckets[index] = ll_insert(buckets[index], name, phone)
|
||||
return buckets
|
||||
|
||||
def ht_delete(buckets, name):
|
||||
index = hash_function(name, len(buckets))
|
||||
buckets[index] = ll_delete(buckets[index], name)
|
||||
return buckets
|
||||
|
||||
def ht_find(buckets, name):
|
||||
index = hash_function(name, len(buckets))
|
||||
return ll_find(buckets[index], name)
|
||||
|
||||
def ht_list_all(buckets):
|
||||
records = []
|
||||
for bucket in buckets:
|
||||
if buckets is not None:
|
||||
records.extend(ll_list_all(buckets))
|
||||
|
|
@ -15,7 +15,7 @@
|
|||
}
|
||||
],
|
||||
"source": [
|
||||
"import LinkedLists\n",
|
||||
"import LinkedList\n",
|
||||
"import HashTable\n"
|
||||
]
|
||||
}
|
||||
|
|
@ -1,3 +0,0 @@
|
|||
def
|
||||
|
||||
def ht_insert(buckets, name, phone):
|
||||
Loading…
Reference in New Issue
Block a user