next up previous index
Next: Linear probing Up: Hash Tables Previous: Hashing   Index


Collision resolution

There are several strategies for resolving collisions. We deal here just with the one called open addressing.

To be specific, suppose that we have an array of objects called table. This is considered to be a hash table which is initially empty. We indicate this by using a variable size whose value is initially 0. Suppose we wish to insert a data object d into the table. Let the hashing function be h, so that the hash code for d is h(d). This is an integer which we assume can be of any size, whereas an array index must lie between 0 and table.length-1. We therefore use

h(d) % table.length
as the index at which to insert the item d. Let us call this index hd, so that the object d is inserted into the table at table[hd]. The variable size now has the value 1.

So far there is no problem. If we want to check whether d is present, or if we want to update it, we can recalculate the index hd and refer to the contents of table[hd]. Since the table was initially empty, there could not have been any conflict when we inserted d. Note that we can assume that the table elements are initialised to null. So it will be easy to test whether or not a given element of the table is occupied.

Suppose now we wish to insert a further item, e say. Again we calculate the index

h(e) % table.length
and call this he. Provided table[he] is not occupied, we can insert e at this location.

But suppose that, after a number of insertions, we try to insert an item n and find that the location hn defined by

h(n) % table.length
is already occupied. This means that some earlier item, m say, must already have been inserted for which
h(m) % table.length == h(n) % table.length
This could have come about either because m and n had the same hash codes h(m) and h(n) or else because they had the same remainder modulo table.length.



Subsections
next up previous index
Next: Linear probing Up: Hash Tables Previous: Hashing   Index
Peter Williams 2005-06-07