Thanks for reading the README!
In creating this hashmap, the following assumptions were made:
- Only primitives types were used. All objects more advanced were coded in primitive types.
- All keys will be strings and thus have
__hash__
python properties. - The hashmap is to be FIXED size. This means that I cannot do things like double the table size to allow for more efficient key,value pair storage. I think this inherently introduces some speed issues.
In this folder, there are three different implementations of a HashMap (Pick the one you like! I like the binary search tree one the most):
- Linear Probing
- Chaining w/ Lists
- Chaining w/ Binary search trees. (includes a BST implementation)
- See
/examples
for example scripts on how to run the three different implementations. - See
/tests
for simple Unit Tests. This used theunittest
library in Python.
To run the unit tests:
python hashtest-linearprobing.py
To run the examples:
python example-chaining-list.py
Speed wise, chaining offers a lot of benefits over linear probing. For example, chaining is much better at handling collisions. However, one of the instructions provided was that the load should never be greater than one. In the case of chaining, it is indeed possible for the load factor to be greater than one, especially if we have fixed table size.. For example, if I am using linked lists, my lists could be infinitely sized per table entry, making the load factor greater than 1. Therefore I include my linear probing implementation as well. Also notice that for chaining, get() will always return true since even though the table size is fixed, each table entry contains some data structure, whose size is unlimited.
I've also included two types of chaining in HashMaps. chaining_list.py
stores a list version w/ O(1) setting but O(N) retrieval. chaining_bst.py
stores a binary search tree version w/ O(logN) setting and retrieval. I included both because the case could be made for both as "more optimal".
Feel free to email me mike.wu@yale.edu
if you have any questions or comments. Looking forward to hearing from you!