-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashTable.py
More file actions
67 lines (62 loc) · 2.78 KB
/
HashTable.py
File metadata and controls
67 lines (62 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import Node as nd
# Data class helps maintain the values as key,value (dict)
class Data:
def __init__(self, key, value):
self.key = key
self.value = value
# hashtable class has variables size_table
class HashTable:
def __init__(self, size_table):
self.size_table = size_table
self.hashTable = [None] * size_table
# function for custom hash function is created by adding ascii value of the key
# and multiple it to the total output_hash calculated to avoid collision
# The modulo of output_hash and hashTable size is used to get the index
def hash_function(self, key):
output_hash = 0
for i in key:
output_hash += ord(i)
output_hash = (output_hash * ord(i)) % self.size_table
return output_hash
# using the hash_function to find the index in hashTable
def add_key_value_to_hash_table(self, key, value):
# key is passed to hash_function to get the index
hashed_index = self.hash_function(key)
# checks if a value is present in the hashTable at index hashed_index
# if no value is present
if self.hashTable[hashed_index] is None:
# a new node is created as dict(Data object) and value is set as None
self.hashTable[hashed_index] = nd.Node(Data(key, value), None)
# if value is present at hashed_index
else:
# iterates through the the linked list in value till it reaches the end
node = self.hashTable[hashed_index]
while node.next:
node = node.next
# a new node is created as dict(Data object) and value is set as None
# this node is added to the end
node.next = nd.Node(Data(key, value), None)
# get value by passing key
def get_value_by_key(self, key):
# key is passed to hash_function to get the index
hashed_index = self.hash_function(key)
# checks if a value is present in the hashTable at index hashed_index
# if value is present
if self.hashTable[hashed_index] is not None:
node = self.hashTable[hashed_index]
# if only one value is present
if node.next is None:
# returns the value
return node.data.value
# if there are multiple values present
while node.next:
# checks if each key in linked list is equal to the input key
if key == node.data.key:
# if the ket is found, the corresponding value is returned
return node.data.value
node = node.next
# checks for last value
if key == node.data.key:
return node.data.value
# if key does not match, None is returned
return None