Implement a hash table in C++
keyExists
,find
,remove
, andsize
should all be O(1)add
should be reasonably fast. Use linear probing to find an open slot in the hash table. This will be O(1), on average, except whengrow
is called.grow
should be O(n)- Do not leak memory
"Open Data Structures," Chapter 5, except for 5.2.3. http://opendatastructures.org/ods-cpp/5_Hash_Tables.html
1. Which of the above requirements work, and which do not? For each requirement, write a brief response.
- all of those methods have run times of O(1).
- asdd linearly probes for a new space if needed.
- grow add each old term into the new array in O(n) time.
- memory is not leaked as data is manually deleted as it isnt needed anymore.
2. I decided to use two function (keyExists
and find
) to enable lookup of keys. Another option would have been to have find
return a T*
, which would be NULL
if an item with matching key is not found. Which design do you think would be better? Explain your reasoning. You may notice that the designers of C++ made the same decision I did when they designed http://www.cplusplus.com/reference/unordered_map/unordered_map/
Splitting the method into two parts allows only a small portion of the method to have to be run every time. That part in this case being the keyExists function.