Created
August 30, 2020 09:51
-
-
Save SiAust/91845a701fbb47ddadda7f32e6ce20c2 to your computer and use it in GitHub Desktop.
Scaling the HashTable by rehashing the contents.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
class Main { | |
public static void main(String[] args) { | |
HashTable<String> hashTable = new HashTable<>(5); // initial size | |
Scanner sc = new Scanner(System.in); | |
int inputs = Integer.parseInt(sc.nextLine()); | |
for (int i = 0; i < inputs; i++) { | |
String[] tableEntryArr = sc.nextLine().split(" "); | |
hashTable.put(Integer.parseInt(tableEntryArr[0]), tableEntryArr[1]); | |
} | |
System.out.println("*** final hash table ***\n" + hashTable); | |
} | |
private static class TableEntry<T> { | |
private final int key; | |
private final T value; | |
public TableEntry(int key, T value) { | |
this.key = key; | |
this.value = value; | |
} | |
public int getKey() { | |
return key; | |
} | |
public T getValue() { | |
return value; | |
} | |
} | |
private static class HashTable<T> { | |
private int size; | |
private TableEntry[] table; | |
public HashTable(int size) { | |
this.size = size; | |
table = new TableEntry[size]; | |
} | |
public boolean put(int key, T value) { | |
int hash = findKey(key); | |
if (hash == -1) { | |
rehash(); // scale the table size by 2 | |
hash = findKey(key); // find the next bucket for this key? | |
// return false; | |
} | |
table[hash] = new TableEntry(key, value); | |
return true; | |
} | |
public T get(int key) { | |
int idx = findKey(key); | |
if (idx == -1 || table[idx] == null) { | |
return null; | |
} | |
return (T) table[idx].getValue(); | |
} | |
private int findKey(int key) { | |
int hash = key % size; | |
while (!(table[hash] == null || table[hash].getKey() == key)) { | |
hash = (hash + 1) % size; | |
if (hash == key % size) { | |
return -1; | |
} | |
} | |
return hash; | |
} | |
private void rehash() { | |
HashTable<T> refactoredHashTable = new HashTable<>(size *= 2); | |
for (TableEntry tableEntry : table) { | |
refactoredHashTable.put(tableEntry.getKey(), (T) tableEntry.getValue()); | |
} | |
System.out.println("*** Refactored table ***\n" + refactoredHashTable + "\n"); | |
table = refactoredHashTable.table; | |
} | |
@Override | |
public String toString() { | |
StringBuilder tableStringBuilder = new StringBuilder(); | |
for (int i = 0; i < table.length; i++) { | |
if (table[i] == null) { | |
tableStringBuilder.append(i + ": null"); | |
} else { | |
tableStringBuilder.append(i + ": key=" + table[i].getKey() | |
+ ", value=" + table[i].getValue()); | |
} | |
if (i < table.length - 1) { | |
tableStringBuilder.append("\n"); | |
} | |
} | |
return tableStringBuilder.toString(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment