Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 { private Entry[] hashArray; private Entry defunct;

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 { private int key; private E data;

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Theory Icdt 97 6th International Conference Delphi Greece January 8 10 1997 Proceedings Lncs 1186

Authors: Foto N. Afrati ,Phokion G. Kolaitis

1st Edition

3540622225, 978-3540622222

More Books

Students also viewed these Databases questions

Question

(5) What is the regression matrix (A) for this example?

Answered: 1 week ago

Question

1. Explain the 2nd world war. 2. Who is the father of history?

Answered: 1 week ago