There are mainly two methods to handle collision:
1) Separate Chaining (Closed Addressing)
2) Open Addressing
Open Addressing is done following ways:
a) Linear Probing: In linear probing, we linearly probe for next slot. For example, typical gap between two probes is 1 as taken in below example also.
1) Separate Chaining (Closed Addressing)
2) Open Addressing
Open Addressing is done following ways:
a) Linear Probing: In linear probing, we linearly probe for next slot. For example, typical gap between two probes is 1 as taken in below example also.
public void put(int key, int value) throws HashtableException {
int hash = key % SIZE;
int count = 0;
while(map[hash] != null && map[hash].getKey() != key){
hash = (hash + 1) % SIZE;
if(count == SIZE)
throw new HashtableException("Table full");
count++;
}
map[hash]= new Pair(key,value);
}
public int get(int key) throws HashtableException{
int hash = key % SIZE;
int count = 0;
while(map[hash] != null && map[hash].getKey() != key){
hash = (hash+1) % SIZE;
if(count == SIZE)
throw new HashtableException("No matching key in the table");
count++;
}
if(map[hash] == null)
throw new HashtableException("No matching key in the table");
return map[hash].getValue();
}
|
Comments
Post a Comment