Question
I need help with expand method !!! public class LinearProbingHashTable { private Entry[] hashArray; private Entry defunct; public LinearProbingHashTable(int size) { hashArray = new Entry[size];
I need help with expand method !!!
public class LinearProbingHashTable
public LinearProbingHashTable(int size) { hashArray = new Entry[size]; defunct = new Entry(-1, null); }
public int hashFunc(int key) { return key % hashArray.length; }
public boolean isFull() { for (int i = 0; i < hashArray.length; i++) if (hashArray[i] == null || hashArray[i] == defunct) return false; return true; }
public void displayTable() { for (int j = 0; j < hashArray.length; j++) if (hashArray[j] != null) hashArray[j].display(); else System.out.println("available"); }
public void insertOld(int k, E d) { if (!isFull()) { int hashVal = hashFunc(k); while (hashArray[hashVal] != null && hashArray[hashVal] != defunct) { ++hashVal; hashVal %= hashArray.length; } hashArray[hashVal] = new Entry(k, d); } }
public void insert(int k, E d) { if (isFull()){ expand(2*hashArray.length); }
int hashVal = hashFunc(k); while (hashArray[hashVal] != null && hashArray[hashVal] != defunct) { ++hashVal; hashVal %= hashArray.length; } hashArray[hashVal] = new Entry(k, d); }
public E find(int k) { int hashVal = hashFunc(k); int start = hashVal; boolean checkAll = false; while (hashArray[hashVal] != null && !checkAll) { if (hashArray[hashVal].key == k) return (E) hashArray[hashVal].data; ++hashVal; hashVal %= hashArray.length; if (hashVal == start) checkAll = true; } return null; }
public E delete(int k) { int hashVal = hashFunc(k); int start = hashVal; boolean checkAll = false; while (hashArray[hashVal] != null && !checkAll) { if (hashArray[hashVal].key == k) { E temp = (E) hashArray[hashVal].data; hashArray[hashVal] = defunct; return temp; } ++hashVal; hashVal %= hashArray.length; if (hashVal == start) checkAll = true; } return null; }
private void expand( int newCapacity )
//input code here !!! {
// INCLUDED FOR TESTING public int getCapacity() { return hashArray.length; }
public class Entry
public Entry(int k, E d) { key = k; data = d; }
public int getKey() { return key; }
public E getData() { return data; } public void display() { System.out.print(key + ":"); System.out.println(data); } }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started