Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions hashtable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Python3 program to implement hashing with chaining
BUCKET_SIZE = 7


class Hash(object):
def __init__(self, bucket):
# Number of buckets
self.__bucket = bucket
# Hash table of size bucket
self.__table = [[] for _ in range(bucket)]

# hash function to map values to key
def hashFunction(self, key):
return (key % self.__bucket)

def insertItem(self, key):
# get the hash index of key
index = self.hashFunction(key)
self.__table[index].append(key)

def deleteItem(self, key):
# get the hash index of key
index = self.hashFunction(key)

# Check the key in the hash table
if key not in self.__table[index]:
return

# delete the key from hash table
self.__table[index].remove(key)

# function to display hash table
def displayHash(self):
for i in range(self.__bucket):
print("[%d]" % i, end='')
for x in self.__table[i]:
print(" --> %d" % x, end='')
print()


# Drive Program
if __name__ == "__main__":
# array that contains keys to be mapped
a = [15, 11, 27, 8, 12]

# Create a empty has of BUCKET_SIZE
h = Hash(BUCKET_SIZE)

# insert the keys into the hash table
for x in a:
h.insertItem(x)

# delete 12 from the hash table
h.deleteItem(x)
# Display the hash table
h.displayHash()