OneCompiler

Implementing Hashing using Chaining

287

Hashing using separate-chaining

Hashing is a great concept in which many operations like searching,deleting,inserting an element happens in atmost constant time(O(1)) and also it is an effective datastructure that acts as a faster look-up table in most of the cases.

chaining is a technique that is mainly used to handle the collisions.collision is a condition where many values may have same position in address table because of using some bad hash function.

I think most of us are familiar with load factor.load factor decides the number of collisions based on the availability of keys to be inserted and number of slots that are available currently in hashTable.If you are unfamiliar with this term,please check the provided link below
https://www.geeksforgeeks.org/hashing-set-2-separate-chaining/?ref=lbp

This code is quite simply and easy to understand if you know object-orinted-programming in python.
The code is given below as follows

Creating a class Hashing with capacity as parameter.capacity decides how many spaces are allocatedin a hashTable.

class Hashing:
  def __init__(self,capacity):
    self.__capacity = capacity
    self.__hashTable = [[] for _ in range(self.__capacity)]

creating a hash-function to compute to specific position of an element in hashTable.

  #hash-function
  def hash_generator(self,value):
    return value % self.__capacity

Note: I have used list as an underlying datastructure in this code.you can use other
datastructures like linkedlists or AVL trees in your code.

creating methods to perform operations like insert,delete,search.Include all methods inside the class.

  #inserting element
  def insert(self,value):
    i = self.hash_generator(value)
    self.__hashTable[i].append(value)
  
  #deletion of an element
  def delete(self,value):
    idx = self.hash_generator(value)
    for i in self.__hashTable[idx]:
      if i == value:
        self.__hashTable[idx].remove(value)
        return
        
  #searching the element
  def search(self,value):
    idx = self.hash_generator(value)
    for j in self.__hashTable[idx]:
      if j == value:
        print("{0} is present in hashTable".format(value))
        return 
        
    print("{0} is not present in hashTable".format(value))
  

The final method to print the content inside the hash-table(almost represented as linked-list format)

  #printing the values
  def display(self):
    for i in range(self.__capacity):
      print(i,end = " ")
      for j in self.__hashTable[i]:
        print("-->",end = " ")
        print(j,end = " ")
      
      print()

Testing the final code by creating an object of a class and calling different methods.

h = Hashing(7)
h.insert(10)
h.insert(44)
h.insert(23)
h.insert(56)
h.insert(3)
h.insert(12)
h.insert(38)

h.display()
h.search(11)
h.search(12)

h.delete(10)
h.delete(23)

print("After deleting some entries in hashTable:")
h.display()

So,That's it for today and I hope you liked this post.Any suggestions to improve this code may be helpful!.

Note:It is better to take the capacity as prime numbers as it helps in avoiding collisions more at a same position in hashtable.