-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuadraticProbingHashTable.java
277 lines (232 loc) · 5.42 KB
/
QuadraticProbingHashTable.java
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// QuadraticProbing Hash table class
//
// CONSTRUCTION: an approximate initial size or default of 101
//
// ******************PUBLIC OPERATIONS*********************
// bool insert( x ) --> Insert x
// bool remove( x ) --> Remove x
// bool contains( x ) --> Return true if x is present
// void makeEmpty( ) --> Remove all items
/**
* Probing table implementation of hash tables.
* Note that all "matching" is based on the equals method.
* @author Mark Allen Weiss
*/
import java.util.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.lang.*;
public class QuadraticProbingHashTable {
public static QuadraticProbingHashTable hash;
public static int ln=0;
public static List<String> list;
/**
* Construct the hash table.
*/
public QuadraticProbingHashTable() {
this(DEFAULT_TABLE_SIZE);
}
/**
* Construct the hash table.
*
* @param size
* the approximate initial size.
*/
public QuadraticProbingHashTable(int size) {
allocateArray(size);
doClear();
}
/**
* Insert into the hash table. If the item is already present, do nothing.
*
* @param x
* the item to insert.
*/
public boolean insert(String x) {
// Insert x as active
int currentPos = findPos(x);
if (isActive(currentPos))
return false;
array[currentPos] = new HashEntry<>(x, true);
theSize++;
// Rehash; see Section 5.5
if (++occupied > array.length / 2)
rehash();
return true;
}
/**
* Expand the hash table.
*/
private void rehash() {
HashEntry<String>[] oldArray = array;
// Create a new double-sized, empty table
allocateArray(2 * oldArray.length);
occupied = 0;
theSize = 0;
// Copy table over
for (HashEntry<String> entry : oldArray)
if (entry != null && entry.isActive)
insert(entry.element);
}
/**
* Method that performs quadratic probing resolution.
*
* @param x
* the item to search for.
* @return the position where the search terminates.
*/
private int findPos(String x) {
int offset = 1;
int currentPos = myhash(x);
while (array[currentPos] != null
&& !array[currentPos].element.equals(x)) {
currentPos += offset; // Compute ith probe
offset += 2;
if (currentPos >= array.length)
currentPos -= array.length;
}
return currentPos;
}
/**
* Remove from the hash table.
*
* @param x
* the item to remove.
* @return true if item removed
*/
public boolean remove(String x) {
int currentPos = findPos(x);
if (isActive(currentPos)) {
array[currentPos].isActive = false;
theSize--;
return true;
} else
return false;
}
public String get(int i) {
return array[i].element;
}
/**
* Get current size.
*
* @return the size.
*/
public int size() {
return theSize;
}
/**
* Get length of internal table.
*
* @return the size.
*/
public int capacity() {
return array.length;
}
/**
* Find an item in the hash table.
*
* @param x
* the item to search for.
* @return the matching item.
*/
public boolean contains(String x) {
int currentPos = findPos(x);
return isActive(currentPos);
}
/**
* Return true if currentPos exists and is active.
*
* @param currentPos
* the result of a call to findPos.
* @return true if currentPos is active.
*/
private boolean isActive(int currentPos) {
return array[currentPos] != null && array[currentPos].isActive;
}
/**
* Make the hash table logically empty.
*/
public void makeEmpty() {
doClear();
}
private void doClear() {
occupied = 0;
for (int i = 0; i < array.length; i++)
array[i] = null;
}
private int myhash(String x) {
int hashVal = hashCode(x);
hashVal %= array.length;
if (hashVal < 0)
hashVal += array.length;
return hashVal;
}
public int hashCode(String x) {
final int prime = 37;
int result = 1;
for (int i = 0; i < x.length(); i++) {
result += x.substring(i, i + 1).hashCode();
}
return result;
}
public static class HashEntry<String> {
public String element; // the element
public boolean isActive; // false if marked deleted
public HashEntry(String e) {
this(e, true);
}
public HashEntry(String e, boolean i) {
element = e;
isActive = i;
}
}
private static final int DEFAULT_TABLE_SIZE = 101;
public HashEntry<String>[] array; // The array of elements
private int occupied; // The number of occupied cells
private int theSize; // Current size
/**
* Internal method to allocate array.
*
* @param arraySize
* the size of the array.
*/
private void allocateArray(int arraySize) {
array = new HashEntry[nextPrime(arraySize)];
}
public HashEntry<String>[] getArray() {
return array;
}
/**
* Internal method to find a prime number at least as large as n.
*
* @param n
* the starting number (must be positive).
* @return a prime number larger than or equal to n.
*/
private static int nextPrime(int n) {
if (n % 2 == 0)
n++;
for (; !isPrime(n); n += 2)
;
return n;
}
/**
* Internal method to test if a number is prime. Not an efficient algorithm.
*
* @param n
* the number to test.
* @return the result of the test.
*/
private static boolean isPrime(int n) {
if (n == 2 || n == 3)
return true;
if (n == 1 || n % 2 == 0)
return false;
for (int i = 3; i * i <= n; i += 2)
if (n % i == 0)
return false;
return true;
}
}