Java Reference
In-Depth Information
String [] HashTable= new String [m];
// By default HashTable[i]=null
for (i=0;i < animals . length ; i++)
int s2int=String2Integer(animals [ i ]) ;
int pos=HashFunction( s2int ) ;
while (HashTable [ pos ]!= null )
pos=(pos+1)%m;
HashTable [ pos]= new String(animals [ i ]) ;
}
Position 0
whale
Position 1
snake
Position 2
bee
Position 3
spider
Position 4
butterfly
Position 5
null
Position 6
null
Position 7
cow
Position 8
shark
Position 9
null
Position 10
null
Position 11
null
Position 12
fish
Position 13
cat
Position 14
peacock
Position 15
dog
Position 16
horse
Position 17
tortoise
Position 18
flamingo
Position 19
pelican
Position 20
parrot
Position 21
lion
Position 22
elephant
To search whether a given string belongs to the hash table or not, we proceed
by first converting it to an integer, hashing it to the hash array index, and
checking the value at that location. If there is nothing, we report that the
object is not present (all this in constant time). Otherwise, we iteratively visit
the cells by incrementing the index until we find the string or we find an empty
array cell.
The main drawback of open addressing is that we cannot insert for sure more
strings than the array size. In case we cannot upper bound the maximal number
of elements. It is better to consider an alternative technique for managing the
hash table. This is where linked lists kicked in.
Search WWH ::




Custom Search