CS 201 Data Structures - Hash Tables Tutorial 01
CS 201 Data Structures - Hash Tables Tutorial 01
1)
A)
Insertion and searching operations are very fast.
Can store large number of data.
2)
A) public int hashFunction(String s){
int index=0;
for(int i=0;i<s.length();i++)
{
index = index + charAt(i);
}
index = index % 599;
return index;
}
According to the above hash function all the strings get same index,
abcedf index = (97+98+99+100+101+102) % 599 = 2,
bcdefa index = (98+99+100+101+102+97) % 599 = 2,
cedfab index = (99+100+101+102+97+98) % 599 = 2,
efabcd index = (100+101+102+97+98+99) % 599 = 2,
B)
Hash function = (sum of ASCII values + position of letter a + position of letter b) %
number of elements
C)
To avoid collision we can use linear probing methods. According to this method
when we get already used array index that element is looking into next cell until find
empty cell.
When consider the above case,
Array -> before linear probing
“abcdef”
“bcdefa”
“cdefac”
“defabc”
0 1 2 3 4 5
0 1 2 3 4 5
3)
A)
Hash Function = value % 8,
According to this the indexes for each value is,
0 1 2 3 4 5 6 7 8
B)
When using chaining method element list implement as a linked list array. The
elements which have same index insert to same cell as a linked list,
0 1 2 3 4 5 6 7
120 145 138 123 117 119
115 135