-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyHashTable.java
executable file
·443 lines (388 loc) · 8.85 KB
/
MyHashTable.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
// Student Name: Mert Gurkan
// ID Number: 260716883
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
class MyHashTable<K, V> {
/*
* Number of entries in the HashTable.
*/
private int entryCount = 0;
/*
* Number of buckets. The constructor sets this variable to its initial
* value, which eventually can get changed by invoking the rehash() method.
*/
private int numBuckets;
/**
* Threshold load factor for rehashing.
*/
private final double MAX_LOAD_FACTOR = 0.75;
/**
* Buckets to store lists of key-value pairs. Traditionally an array is used
* for the buckets and a linked list is used for the entries within each
* bucket. We use an Arraylist rather than an array, since the former is
* simpler to use in Java.
*/
ArrayList<HashLinkedList<K, V>> buckets;
/*
* Constructor.
*
* numBuckets is the initial number of buckets used by this hash table
*/
MyHashTable(int numBuckets) {
// ADD YOUR CODE BELOW HERE
this.numBuckets = numBuckets;
buckets = new ArrayList<HashLinkedList<K, V>>();
for (int i = 0; i < this.getNumBuckets(); i++)
{
buckets.add(new HashLinkedList<K, V>());
}
// ADD YOUR CODE ABOVE HERE
}
/**
* Given a key, return the bucket position for the key.
*/
private int hashFunction(K key) {
return Math.abs(key.hashCode()) % numBuckets;
}
/**
* Checking if the hash table is empty.
*/
public boolean isEmpty() {
if (entryCount == 0) {
return true;
} else {
return (false);
}
}
/**
* return the number of entries in the hash table.
*/
public int size() {
return (entryCount);
}
/**
* Adds a key-value pair to the hash table. If the load factor goes above
* the MAX_LOAD_FACTOR, then call the rehash() method after inserting.
*
* If there was a previous value for the given key in this hashtable, then
* overwrite it with new value and return the old value. Otherwise return
* null.
*/
public V put(K key, V value) {
// ADD YOUR CODE BELOW HERE
int index;
if (this.getNumBuckets() == 0)
{
HashLinkedList<K, V> newList = new HashLinkedList<K, V>();
newList.add(key, value);
buckets.add(newList);
this.entryCount++;
this.numBuckets++;
if ((((double) this.size()) / ((double) this.getNumBuckets())) > this.MAX_LOAD_FACTOR)
{
this.rehash();
}
}
else
{
index = this.hashFunction(key);
if (this.isEmpty())
{
buckets.get(index).add(key, value);
this.entryCount++;
if ((((double) this.size()) / ((double) this.getNumBuckets())) > this.MAX_LOAD_FACTOR)
{
this.rehash();
}
} else
{
if (this.containsKey(key))
{
V preValue = this.get(key);
if (preValue == null)
{
System.out.println("Error");
return null;
}
HashNode<K, V> node = buckets.get(index).getListNode(key);
if (node == null)
{
System.out.println("Error");
return null;
}
else
{
node.helpValue(value);
}
if ((((double) this.size()) / ((double) this.getNumBuckets())) > this.MAX_LOAD_FACTOR)
{
this.rehash();
}
return preValue;
}
else
{
buckets.get(index).add(key, value);
this.entryCount++;
if ((((double) this.size()) / ((double) this.getNumBuckets())) > this.MAX_LOAD_FACTOR)
{
this.rehash();
}
}
}
}
// ADD YOUR CODE ABOVE HERE
return null;
}
/**
* Retrieves a value associated with some given key in the hash table.
* Returns null if the key could not be found in the hash table)
*/
public V get(K key) {
// ADD YOUR CODE BELOW HERE
if (this.getNumBuckets() != 0)
{
if (!this.isEmpty())
{
if (this.containsKey(key))
{
int index = this.hashFunction(key);
HashNode<K, V> node = buckets.get(index).getListNode(key);
if (node == null)
{
System.out.println("Error");
return null;
}
else
{
return node.getValue();
}
}
}
}
// ADD YOUR CODE ABOVE HERE
return null;
}
/**
* Removes a key-value pair from the hash table. Return value associated
* with the provided key. If the key is not found, return null.
*/
public V remove(K key) {
// ADD YOUR CODE BELOW HERE
if (this.getNumBuckets() == 0)
{
return null;
}
else
{
int index = this.hashFunction(key);
if (this.isEmpty())
{
return null;
}
else if (this.size() == 1)
{
if (this.containsKey(key))
{
HashNode<K, V> node = buckets.get(index).remove(key);
if (node == null)
{
System.out.println("Error");
return null;
}
else
{
this.clear();
return node.getValue();
}
}
}
else
{
if (this.containsKey(key))
{
HashNode<K, V> node = buckets.get(index).remove(key);
if (node == null)
{
System.out.println("Error");
return null;
}
else
{
return node.getValue();
}
}
}
}
// ADD YOUR CODE ABOVE HERE
return null;
}
/*
* This method is used for testing rehash(). Normally one would not provide
* such a method.
*/
public int getNumBuckets() {
return numBuckets;
}
/*
* Returns an iterator for the hash table.
*/
public MyHashTable<K, V>.HashIterator iterator() {
return new HashIterator();
}
/*
* Removes all the entries from the hash table, but keeps the number of
* buckets intact.
*/
public void clear() {
for (int ct = 0; ct < buckets.size(); ct++) {
buckets.get(ct).clear();
}
entryCount = 0;
}
/**
* Create a new hash table that has twice the number of buckets.
*/
public void rehash() {
// ADD YOUR CODE BELOW HERE
if (this.getNumBuckets() == 0)
{
System.out.println("Error");
return;
}
else
{
MyHashTable<K, V> updateTable = new MyHashTable<K, V>(this.getNumBuckets() * 2);
for (HashLinkedList<K, V> list : buckets)
{
if (list.isEmpty())
{
continue;
}
else
{
HashNode<K, V> initial = list.getFirst();
while (initial != null) {
updateTable.put(initial.getKey(), initial.getValue());
initial = initial.getNext();
}
}
}
this.entryCount = updateTable.size();
this.numBuckets = updateTable.getNumBuckets();
this.buckets = updateTable.buckets;
}
// ADD YOUR CODE ABOVE HERE
}
/*
* Checks if the hash table contains the given key. Return true if the hash
* table has the specified key, and false otherwise.
*/
public boolean containsKey(K key) {
int hashValue = hashFunction(key);
if (buckets.get(hashValue).getListNode(key) == null) {
return false;
}
return true;
}
/*
* return an ArrayList of the keys in the hashtable
*/
public ArrayList<K> keys() {
ArrayList<K> listKeys = new ArrayList<K>();
// ADD YOUR CODE BELOW HERE
Iterator<HashNode<K, V>> it = this.iterator();
while (it.hasNext())
{
listKeys.add(it.next().getKey());
}
return listKeys;
// ADD YOUR CODE ABOVE HERE
}
/*
* return an ArrayList of the values in the hashtable
*/
public ArrayList<V> values() {
ArrayList<V> listValues = new ArrayList<V>();
// ADD YOUR CODE BELOW HERE
Iterator<HashNode<K, V>> it = this.iterator();
while (it.hasNext())
{
listValues.add(it.next().getValue());
}
return listValues;
// ADD YOUR CODE ABOVE HERE
}
@Override
public String toString() {
/*
* Implemented method. You do not need to modify.
*/
StringBuffer sb = new StringBuffer();
for (int i = 0; i < buckets.size(); i++) {
sb.append("Bucket ");
sb.append(i);
sb.append(" has ");
sb.append(buckets.get(i).size());
sb.append(" entries.\n");
}
sb.append("There are ");
sb.append(entryCount);
sb.append(" entries in the hash table altogether.");
return sb.toString();
}
/*
* Inner class: Iterator for the Hash Table.
*/
public class HashIterator implements Iterator<HashNode<K, V>> {
HashLinkedList<K, V> allEntries;
/**
* Constructor: make a linkedlist (HashLinkedList) 'allEntries' of all
* the entries in the hash table
*/
public HashIterator() {
// ADD YOUR CODE BELOW HERE
allEntries = new HashLinkedList<K, V>();
if (getNumBuckets() == 0 || isEmpty())
{
return;
}
else
{
for (HashLinkedList<K, V> list : buckets)
{
if (list.isEmpty())
{
continue;
}
else
{
HashNode<K, V> temporary = list.getFirst();
while (temporary != null)
{
allEntries.add(temporary.getKey(), temporary.getValue());
temporary = temporary.getNext();
}
}
}
}
// ADD YOUR CODE ABOVE HERE
}
// Override
@Override
public boolean hasNext() {
return !allEntries.isEmpty();
}
// Override
@Override
public HashNode<K, V> next() {
return allEntries.removeFirst();
}
@Override
public void remove() {
// not implemented, but must be declared because it is in the
// Iterator interface
}
}
}