This class will implement a hash table. The hash table will hold data items whos
ID: 3791888 • Letter: T
Question
This class will implement a hash table. The hash table will hold data items whose type is tuple. This class will have following public methods and constructor. HashTable(int size) Finds the smallest prime integer p whose value is at least size. Creates a hash table of size p where each cell initially is NULL. It will determine the hash function to be used in the hash table by creating the object new HashFunction(p). maxLoad () Returns the maximum load of the hash table averageLoad () Returns the average load of the hash table size () returns the current size of the hash table. numElements() returns the number of Tuples that are currently stored in the hash table. loadFactor() return the load factor which is numElements()/size() add (Tuple t) Adds the tuple t to the hash table; places t in the list pointed by the cell h(t.getKey()) where h is the hash function that is being used. When the load factors become bigger than 0.7, then it (approximately) doubles the size of the hash table and rehashes all the elements (tuples) to the new hash table. The size of the new hash table must be: Smallest prime integer whose value is at least twice the current size. search (int k) returns an array list of Tuples (in the hash table) whose key equals k. If no such Tuples exist, returns an empty list. Note that the type of this method must be ArrayList remove (Tuple t) Removes the Tuple t from the hash table.Explanation / Answer
HashTable.java
import java.math.BigInteger;
import java.util.ArrayList;
public class HashTable {
TupleCell[] hashTable; //Table to store Tuple cells
HashFunction hashFunction; //Current hash Function
public HashTable(int size) {
int actualSize = Math.abs(size);
while (!new BigInteger(Integer.toString(actualSize)).isProbablePrime(1))
actualSize++; //Determining next prime number
hashTable = new TupleCell[actualSize]; //Create tuple cells
hashFunction = new HashFunction(actualSize); //Create hash function
}
public int maxLoad() { //Maximum load in hash table
int maxValue = 0;
for (TupleCell tupleCell : hashTable) {
TupleCell node = tupleCell;
int count = 0;
while (node != null) {
count++;
node = node.nextCell;
}
if (count > maxValue) {
maxValue = count;
}
}
return maxValue;
}
public double averageLoad() { //avarage load in hash table
return ((double) numElements()) / size();
}
public int size() { //size of table
return hashTable.length;
}
public int numElements() { // number of elements in table
int count = 0;
for (TupleCell tupleCell : hashTable) {
TupleCell node = tupleCell;
while (node != null) {
count++;
node = node.nextCell;
}
}
return count;
}
public double loadFactor() { //load factor
return ((double) numElements()) / size();
}
public void add(Tuple t) {
TupleCell node = hashTable[hashFunction.hashIndex(t.getKey())];
if (node == null) {
hashTable[hashFunction.hashIndex(t.getKey())] = new TupleCell(t); //If hash table cell is empty
} else {
while (node.nextCell != null) {
node = node.nextCell;
}
node.nextCell = new TupleCell(t); //adding new tuple to end of corresponding linked list
}
if (loadFactor() > 0.7) { //Rehash
int newSize = 2 * hashTable.length;
while (!new BigInteger(Integer.toString(newSize))
.isProbablePrime(1))
newSize++; //Finding next prime number
TupleCell[] newHashTable = new TupleCell[newSize];
HashFunction newHashFunction = new HashFunction(newSize);
rehash(newHashTable, newHashFunction); //Rehash based on new hashtable and new hash function
hashTable = newHashTable;
hashFunction = newHashFunction;
}
}
private void rehash(TupleCell[] newHashTable, HashFunction newHashFunction) { //Rehash funciton
for (TupleCell tupleCell : hashTable) { //iterating through each cell
TupleCell node = tupleCell;
while (node != null) { //iterating through each node in linked list
TupleCell reNode = newHashTable[newHashFunction
.hashIndex(node.t.getKey())];
if (reNode == null) {
newHashTable[newHashFunction.hashIndex(node.t.getKey())] = node; //Adding to new hash table
} else {
while (reNode.nextCell != null) {
reNode = reNode.nextCell;
}
reNode.nextCell = node;
}
TupleCell temp = node;
node = node.nextCell;
temp.nextCell = null; //reseting next cell reference
}
}
}
public ArrayList<Tuple> search(int key) {
ArrayList<Tuple> returnList = new ArrayList<Tuple>();
TupleCell node = hashTable[hashFunction.hashIndex(key)]; //Getting corresponding linked list
while (node != null) {
if (node.t.getKey() == key)
returnList.add(node.t); //match
node = node.nextCell;
}
return returnList;
}
public boolean remove(Tuple t) {
boolean found = false;
TupleCell node = hashTable[hashFunction.hashIndex(t.getKey())];
if (node != null) {
if (node.t.equals(t)) {
hashTable[hashFunction.hashIndex(t.getKey())] = node.nextCell; //if table cell matches tuple
found = true;
} else {
TupleCell prevNode = node;
while (node != null) {
if (node.t.equals(t)) {
prevNode.nextCell = node.nextCell; //if node in linked list matches tuple
found = true;
break;
}
prevNode = node; //remove node
node = node.nextCell;
}
}
}
return found;
}
static class TupleCell { //Wrapper class for tuple with additional reference to next TupleCell - for linked list
Tuple t;
TupleCell nextCell;
TupleCell(Tuple t) {
this.t = t;
}
}
}
class HashFunction { //Hash function class
int size;
public HashFunction(int p) {
this.size = p;
}
public int hashIndex(int key) { //key to index method
return Math.abs(key) % size;
}
}
Tuple.java
public class Tuple {
private int key;
public Tuple(int key){
this.key = key;
}
public int getKey() { //used by hash table
return key;
}
public String toString(){
return Integer.toString(key);
}
}