unsigned int Hash( const string & key, const int h_size ) { unsigned int value = 0; for ( int i=0; i<key.length(); i++ ) value = (value + key[i]) << 3; // multiply by 8 return value % h_size; }Is this a good hash function when
h_size = 128
? Why or why not?
Solution: No. It is a bad hash function. It only hashes to locations that are multiples of 8.
Solution:
When there are no print operations the hash table will be best. Since
there will never be a need to access the items in order, each
operation will require on average a constant amount of time,
independent of the number of items stored. This is better than the
time required by each of the tree operations.
Solution:
This is the best choice when there are enough print operations to
offset the constant time accesses to the hash table and
the input is random enough that balancing is not necessary.
Solution:
When there are print operations to offset the constant time access for
insert, find of hash tables and when the input is close enough to
being ordered that an unbalanced tree would result if a regular
binary search tree were used.