(a) Suppose that we number the rows of the board 1-8 and the columns of the board 1-8. One possible hash function is to compute the sum of the row and column of every piece on the board and mod the total by the tablesize. What is the disadvantage of this hash function?
(b) What if we were to modify the above hash function so that we also add in a value based on the type of the piece (pawn = 1, rook = 2, ..., king = 8) and multiply the white pieces by 384 (16 pieces * (8 rows + 8 cols + 8 types of pieces)) to distinguish them from the black pieces? (As always, we mod this result by the tablesize)
(c) Describe the best hash function you can think of for a chess board configuration.
FindMax()
operation in O(1) time. He does this by adding
an extra piece of data to the HashTable class:
class HashTable{ private: ... normal stuff here ... HashedObj maxval; public: ... normal stuff here ... HashedObj& FindMax(); };
His approach is to store the first piece of data inserted into the
hash table in maxval
. From that point on, every piece of
data that is inserted is compared against maxval
and, if
it is bigger, replaces maxval
. The implementation of
FindMax()
is simply to return
maxval
.
This implementation has a fundamental flaw that will prevent
FindMax()
from always running in O(1) time. What is it?
(you should assume that HashedObj
supports the
"<
" and ">
" operators)