Ch 6 - Hash Tables (3)
Ch 6 - Hash Tables (3)
Data Structures
In Hash Table, several items are distributed in a list using a hash function that is
applied to a property of the data called (a key).
Digest: a large integer number, Index: an integer in the range 0 …. n-1, where n: list size
The result of the hash function is a large integer, which is much greater than the size
of the list (digest cannot be used as an index).
Another function is used to compress the digest to fit in the index space of a list
(to be in [0 … n-1]), where is the list size.
A simple example of a compression function is the modulo , for example:
If the digest is: 76543763234, and n = 1000, then
the index = 70043763234 % 1000 = 234.
But all digests that end by 234 will have the same index!!
Hash functions, and compression functions, are not ideal, so a hash function could
produce the same digest for different keys, and so for compression functions, as
shown previously.
Having the same index for two different keys (or more) is called a COLLISION.
In this method, each cell of the array that stores the list is a linked list of items.
If an item has an index (given by the hash function), it will be added to the linked
list of the cell, if another item is given the same index (by the hash function, then it
will be also added to the linked list of the cell.
In the case of searching an item , the hash function gives it an , then we search for it
in the linked list at the cell .
In this method, if item has an index (given by the hash function), it will be
stored in the cell.
If another item is given the same index (by the hash function), then we find the
first empty cell after and store in that cell.
If we reach the end of the list we can return back to the start of the list to find an
empty cell.
In the case of searching an item , if is given the index (by the hash function),
and , we search the next cells until we find , or return back to the cell (in this
case the item is not found).
Faculty of Information Technology - Computer Science Department 11
Linear Probing Example