Question: Define the ChainingHashTable class which uses separate chaining to handle collision resolution. The class should contain: a constructor which is passed one parameter - the
Define the ChainingHashTable class which uses separate chaining to handle collision resolution. The class should contain:
- a constructor which is passed one parameter - the table size. Two instance variables are needed: the size of the table and the list of keys the table contains. Note that since we are using separate chaining, each element of the HashTable will itself be a list to store all the keys allocated to that particular slot.
- The get_hash_value(self, key) method which, when passed a key, calculates the slot where this key is to be stored. The get_hash_value() uses the hash function - key % table_size
- The __str__(self) method which provides a string representation of the hash table (in list of lists format, e.g., [[], [], [], [], []].
Submit your entire ChainingHashTable class.
For example:
| Test | Result |
|---|---|
x = ChainingHashTable(2) print(x) print(x.get_hash_value(2)) print(x.get_hash_value(5)) | [[], []] 0 1 |
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
