m2skills

qhash py

Apr 4th, 2017
8,752
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.73 KB | None | 0 0
  1. # Program to implement Hashing with Quadratic Probing
  2.  
  3. class hashTable:
  4.     # initialize hash Table
  5.     def __init__(self):
  6.         self.size = int(input("Enter the Size of the hash table : "))
  7.         # initialize table with all elements 0
  8.         self.table = list(0 for i in range(self.size))
  9.         self.elementCount = 0
  10.         self.comparisons = 0
  11.    
  12.     # method that checks if the hash table is full or not
  13.     def isFull(self):
  14.         if self.elementCount == self.size:
  15.             return True
  16.         else:
  17.             return False
  18.    
  19.    
  20.     # method that returns position for a given element
  21.     # replace with your own hash function
  22.     def hashFunction(self, element):
  23.         return element % self.size
  24.        
  25.    
  26.     # method to resolve collision by quadratic probing method
  27.     def quadraticProbing(self, element, position):
  28.         posFound = False
  29.         # limit variable is used to restrict the function from going into infinite loop
  30.         # limit is useful when the table is 80% full
  31.         limit = 50
  32.         i = 1
  33.         # start a loop to find the position
  34.         while i <= limit:
  35.             # calculate new position by quadratic probing
  36.             newPosition = position + (i**2)
  37.             newPosition = newPosition % self.size
  38.             # if newPosition is empty then break out of loop and return new Position
  39.             if self.table[newPosition] == 0:
  40.                 posFound = True
  41.                 break
  42.             else:
  43.                 # as the position is not empty increase i
  44.                 i += 1
  45.         return posFound, newPosition
  46.  
  47.        
  48.     # method that inserts element inside the hash table
  49.     def insert(self, element):
  50.         # checking if the table is full
  51.         if self.isFull():
  52.             print("Hash Table Full")
  53.             return False
  54.            
  55.         isStored = False
  56.        
  57.         position = self.hashFunction(element)
  58.            
  59.         # checking if the position is empty
  60.         if self.table[position] == 0:
  61.             # empty position found , store the element and print the message
  62.             self.table[position] = element
  63.             print("Element " + str(element) + " at position " + str(position))
  64.             isStored = True
  65.             self.elementCount += 1
  66.        
  67.         # collision occured hence we do linear probing
  68.         else:
  69.             print("Collision has occured for element " + str(element) + " at position " + str(position) + " finding new Position.")
  70.             isStored, position = self.quadraticProbing(element, position)
  71.             if isStored:
  72.                 self.table[position] = element
  73.                 self.elementCount += 1
  74.  
  75.         return isStored
  76.        
  77.  
  78.     # method that searches for an element in the table
  79.     # returns position of element if found
  80.     # else returns False
  81.     def search(self, element):
  82.         found = False
  83.        
  84.         position = self.hashFunction(element)
  85.         self.comparisons += 1
  86.         if(self.table[position] == element):
  87.             return position
  88.        
  89.         # if element is not found at position returned hash function
  90.         # then we search element using quadratic probing
  91.         else:
  92.             limit = 50
  93.             i = 1
  94.             newPosition = position
  95.             # start a loop to find the position
  96.             while i <= limit:
  97.                 # calculate new position by quadratic probing
  98.                 newPosition = position + (i**2)
  99.                 newPosition = newPosition % self.size
  100.                 self.comparisons += 1
  101.                
  102.                 # if element at newPosition is equal to the required element
  103.                 if self.table[newPosition] == element:
  104.                     found = True
  105.                     break
  106.                    
  107.                 elif self.table[newPosition] == 0:
  108.                     found = False
  109.                     break
  110.                    
  111.                 else:
  112.                     # as the position is not empty increase i
  113.                     i += 1
  114.             if found:
  115.                 return newPosition
  116.             else:
  117.                 print("Element not Found")
  118.                 return found           
  119.  
  120.     # method to remove an element from the table       
  121.     def remove(self, element):
  122.         position = self.search(element)
  123.         if position is not False:
  124.             self.table[position] = 0
  125.             print("Element " + str(element) + " is Deleted")
  126.             self.elementCount -= 1
  127.         else:
  128.             print("Element is not present in the Hash Table")
  129.         return
  130.        
  131.    
  132.     # method to display the hash table
  133.     def display(self):
  134.         print("\n")
  135.         for i in range(self.size):
  136.             print(str(i) + " = " + str(self.table[i]))
  137.         print("The number of element is the Table are : " + str(self.elementCount))
  138.        
  139.            
  140. # main function
  141. table1 = hashTable()
  142.  
  143. # storing elements in table
  144. table1.insert(12)
  145. table1.insert(26)
  146. table1.insert(31)
  147. table1.insert(17)
  148. table1.insert(90)
  149. table1.insert(28)
  150. table1.insert(88)
  151. table1.insert(40)
  152. table1.insert(77)       # element that causes collision at position 0
  153.  
  154. # displaying the Table
  155. table1.display()
  156. print()
  157.  
  158. # printing position of elements
  159. print("The position of element 31 is : " + str(table1.search(31)))
  160. print("The position of element 28 is : " + str(table1.search(28)))
  161. print("The position of element 90 is : " + str(table1.search(90)))
  162. print("The position of element 77 is : " + str(table1.search(77)))
  163. print("The position of element 1 is : " + str(table1.search(1)))
  164. print("\nTotal number of comaprisons done for searching = " + str(table1.comparisons))
  165. print()
  166.  
  167. table1.remove(90)
  168. table1.remove(12)
  169.  
  170. table1.display()
Add Comment
Please, Sign In to add comment