Repository navigation
Expand file tree
/
Copy pathDoubleHashing.java
More file actions
executable file
·155 lines (130 loc) · 4.76 KB
/
Copy pathDoubleHashing.java
File metadata and controls
executable file
·155 lines (130 loc) · 4.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package dsGuy.hashing;
import java.util.ArrayList;
public class DoubleHashing {
String[] hashTable;
int noOfCellsUsedInHashTable;
//Constructor
DoubleHashing() {
hashTable = new String[13];
noOfCellsUsedInHashTable = 0;
}// end of method
// HashFunction to be used on Keys
public int simpleASCIIHashFunction(String x, int M) {
char ch[];
ch = x.toCharArray();
int i, sum;
for (sum = 0, i = 0; i < x.length(); i++) {
sum = sum + ch[i];
}
// System.out.println("Index from hashfunction: " + sum % M);
return sum % M;
}// end of method
//2nd HashFunction.
int secondHashFunction(String x, int M) {
char ch[];
ch = x.toCharArray();
int xlength = x.length();
int i, sum;
for (sum = 0, i = 0; i < xlength; i++)
sum += ch[i];
while (sum > 13)
sum = addAllTheDigitsTogether(sum);
return sum % M;
}// end of method
private int addAllTheDigitsTogether(int sum) {
int value = 0;
while (sum > 0) {
value = sum % 10;
sum = sum / 10;
}
return value;
}// end of method
public double getLoadFactor() {
return noOfCellsUsedInHashTable * 1.0 / hashTable.length;
}// end of method
public void insertKeyInHashTable(String value) {
double loadFactor = getLoadFactor();
if (loadFactor >= 0.75) {
System.out.println("Load factor of this HashTable has exceeded 0.75, hence we need to Rehash !\n");
rehashKeys(value);
} else {
System.out.println("Inserting \"" + value + "\" in HashTable...");
int x = simpleASCIIHashFunction(value, hashTable.length);
int y = secondHashFunction(value, hashTable.length);
for (int i = 0; i < hashTable.length; i++) {
int newIndex = (x + i * y) % hashTable.length;
// If there is a collision then we go to the next index
// and keep going till we reach starting index.
if (hashTable[newIndex] == null) {
hashTable[newIndex] = value;
System.out.println("Index: " + newIndex + " is blank. Inserting there...");
System.out.println("Successfully inserted " + "\"" + value + "\"" + " in location: " + newIndex);
System.out.println("-------------------------------------------\n");
break;
}else {
System.out.println("Index: " + newIndex + " is already occupied. Trying next empty cell...");
}
}
}
noOfCellsUsedInHashTable++;
}// end of method
//Creates a new HashTable and does ReHashing
public void rehashKeys(String newStringToBeInserted) {
noOfCellsUsedInHashTable = 0; //need to reset it as we are now dealing with fresh HashTable
ArrayList<String> data = new ArrayList<String>();
for (String s : hashTable) { //loop through the HashTable and save all the keys in ArrayList
if (s != null)
data.add(s);
}
data.add(newStringToBeInserted);
hashTable = new String[hashTable.length * 2]; // make new table with double size
for (String s : data) { // push all old data into new table
insertKeyInHashTable(s);
}
}//end of method
//Search for a given key in hashTable
public boolean searchKeyInHashTable(String stringToBeSearched) {
int index = simpleASCIIHashFunction(stringToBeSearched, hashTable.length);
for (int i = index; i < index + hashTable.length; i++) {
int newIndex = i % hashTable.length;
if (hashTable[newIndex]!=null && hashTable[newIndex].equals(stringToBeSearched)) {
System.out.println("\n" + "\"" + stringToBeSearched + "\"" + " found in HashTable at location: "+newIndex);
return true;
}
}
System.out.println("\n" + "\"" + stringToBeSearched + "\"" + " not found in HashTable.");
return false;
}//end of method
//Delete key from HashTable
public void deleteKeyFromHashTable(String stringToBeDeleted) {
int index = simpleASCIIHashFunction(stringToBeDeleted, hashTable.length);
for (int i = index; i < index + hashTable.length; i++) {
int newIndex = i % hashTable.length;
if (hashTable[newIndex]!=null && hashTable[newIndex].equals(stringToBeDeleted)) {
hashTable[newIndex] = null;
System.out.println("\n" + "\"" + stringToBeDeleted + "\"" + " has been found in HashTable." );
System.out.println("\"" + stringToBeDeleted + "\"" + " has been deleted from HashTable !" );
return;
}
}
System.out.println("\nCould not find " + "\"" + stringToBeDeleted + "\"" + " in HashTable");
}//end of method
// Display the hash table
public void displayHashTable() {
if(hashTable == null) {
System.out.println("\nHashTable does not exits !");
return;
}else {
System.out.println("\n---------- HashTable ---------");
for (int i = 0; i < hashTable.length; i++) {
System.out.println("Index:" + i + ". Key:" + hashTable[i]);
}
}
System.out.println("\n");
}// end of method
// deletes entire HashTable
public void deleteHashTable() {
System.out.println("Successfully deleted HashTable !");
hashTable = null;
}// end of method
}