m2skills

lhash py

Apr 7th, 2017
3,874
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.94 KB | None | 0 0
  1. # Program to implement Hashing with Linear 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.    
  13.     # method that checks if the hash table is full or not
  14.     def isFull(self):
  15.         if self.elementCount == self.size:
  16.             return True
  17.         else:
  18.             return False
  19.    
  20.    
  21.     # method that returns position for a given element
  22.     def hashFunction(self, element):
  23.         return element % self.size
  24.        
  25.    
  26.     # method that inserts element inside the hash table
  27.     def insert(self, element):
  28.         # checking if the table is full
  29.         if self.isFull():
  30.             print("Hash Table Full")
  31.             return False
  32.            
  33.         isStored = False
  34.        
  35.         position = self.hashFunction(element)
  36.        
  37.         # checking if the position is empty
  38.         if self.table[position] == 0:
  39.             self.table[position] = element
  40.             print("Element " + str(element) + " at position " + str(position))
  41.             isStored = True
  42.             self.elementCount += 1
  43.        
  44.         # collision occured hence we do linear probing
  45.         else:
  46.             print("Collision has occured for element " + str(element) + " at position " + str(position) + " finding new Position.")
  47.             while self.table[position] != 0:
  48.                 position += 1
  49.                 if position >= self.size:
  50.                     position = 0
  51.            
  52.             self.table[position] = element
  53.             isStored = True
  54.             self.elementCount += 1
  55.         return isStored
  56.        
  57.  
  58.     # method that searches for an element in the table
  59.     # returns position of element if found
  60.     # else returns False
  61.     def search(self, element):
  62.         found = False
  63.        
  64.         position = self.hashFunction(element)
  65.         self.comparisons += 1
  66.        
  67.         if(self.table[position] == element):
  68.             return position
  69.             isFound = True
  70.        
  71.         # if element is not found at position returned hash function
  72.         # then first we search element from position+1 to end
  73.         # if not found then we search element from position-1 to 0
  74.         else:
  75.             temp = position - 1
  76.             # check if the element is stored between position+1 to size
  77.             while position < self.size:
  78.                 if self.table[position] != element:
  79.                     position += 1
  80.                     self.comparisons += 1
  81.                 else:  
  82.                     return position
  83.            
  84.             # now checking if the element is stored between position-1 to 0
  85.             position = temp
  86.             while position >= 0:   
  87.                 if self.table[position] != element:
  88.                     position -= 1
  89.                     self.comparisons += 1
  90.                 else:  
  91.                     return position
  92.                    
  93.         if not found:
  94.             print("Element not found")
  95.             return False
  96.            
  97.  
  98.     # method to remove an element from the table       
  99.     def remove(self, element):
  100.         position = self.search(element)
  101.         if position is not False:
  102.             self.table[position] = 0
  103.             print("Element " + str(element) + " is Deleted")
  104.             self.elementCount -= 1
  105.         else:
  106.             print("Element is not present in the Hash Table")
  107.         return
  108.        
  109.    
  110.     # method to display the hash table
  111.     def display(self):
  112.         print("\n")
  113.         for i in range(self.size):
  114.             print(str(i) + " = " + str(self.table[i]))
  115.         print("The number of element is the Table are : " + str(self.elementCount))
  116.        
  117.            
  118. # main function
  119. table1 = hashTable()
  120.  
  121. # storing elements in table
  122. table1.insert(12)
  123. table1.insert(26)
  124. table1.insert(31)
  125. table1.insert(17)
  126. table1.insert(90)
  127. table1.insert(28)
  128. table1.insert(88)
  129. table1.insert(40)
  130. table1.insert(77)       # element that causes collision at position 0
  131.  
  132. # displaying the Table
  133. table1.display()
  134. print()
  135.  
  136. # printing position of elements
  137. print("The position of element 31 is : " + str(table1.search(31)))
  138. print("The position of element 28 is : " + str(table1.search(28)))
  139. print("The position of element 90 is : " + str(table1.search(90)))
  140. print("The position of element 77 is : " + str(table1.search(77)))
  141. print("The position of element 1 is : " + str(table1.search(1)))
  142. print("\nTotal number of comaprisons done for searching = " + str(table1.comparisons))
  143. print()
  144.  
  145. table1.remove(90)
  146. table1.remove(12)
  147.  
  148. table1.display()
Add Comment
Please, Sign In to add comment