Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In JAVA I am using Data Structure and algorithms in Java Second Edition Program Must Compile and run! Write a rehash() method for the hash.java

In JAVA

I am using Data Structure and algorithms in Java Second Edition

Program Must Compile and run!

Write a rehash() method for the hash.java program. It should be called by insert() to move the entire hash table to an array about twice as large whenever the load factor exceeds 0.5. The new array size should be a prime number. Refer to the section Expanding the Array in this chapter. Dont forget youll need to handle items that have been deleted, that is, written over with 1.

MUST COMPILE AND RUN, PLEASE DISPLAY OUTPUT With Comments

Please include main method below with program

public static void main(String[] args) throws IOException

{

DataItem aDataItem;

int aKey, size, n, keysPerCell;

// get sizes

System.out.print("Enter size of hash table: ");

size = getInt();

System.out.print("Enter initial number of items: ");

n = getInt();

keysPerCell = 10;

// make table

HashTable theHashTable = new HashTable(size);

for(int j=0; j

{

aKey = (int)(java.lang.Math.random() *

keysPerCell * size);

aDataItem = new DataItem(aKey);

theHashTable.insert(aDataItem);

}

while(true) // interact with user

{

System.out.print("Enter first letter of ");

System.out.print("show, insert, delete, or find: ");

char choice = getChar();

switch(choice)

{

case 's':

theHashTable.displayTable();

break;

case 'i':

System.out.print("Enter key value to insert: ");

aKey = getInt();

aDataItem = new DataItem(aKey);

theHashTable.insert(aDataItem);

break;

case 'd':

System.out.print("Enter key value to delete: ");

aKey = getInt();

theHashTable.delete(aKey);

break;

case 'f':

System.out.print("Enter key value to find: ");

aKey = getInt();

aDataItem = theHashTable.find(aKey);

if(aDataItem != null)

{

System.out.println("Found " + aKey);

}

else

System.out.println("Could not find " + aKey);

break;

default:

System.out.print("Invalid entry ");

} // end switch

} // end while

} // end main()

Output may look like:

Enter size of hash table: 11

Enter initial number of items: 5

Enter first letter of show, insert, delete, or find: s

Table: 22 ** ** 47 70 ** ** ** 74 ** 109

Enter first letter of show, insert, delete, or find: d

Enter key value to delete: 47

Enter first letter of show, insert, delete, or find: s

Table: 22 ** ** -1 70 ** ** ** 74 ** 109

Enter first letter of show, insert, delete, or find: i

Enter key value to insert: 47

Rehashing 6 items, new size is 23

Enter first letter of show, insert, delete, or find: s

Table: ** 47 70 ** ** 74 ** ** ** ** ** ** ** ** ** ** ** 109 ** ** ** ** 22

Enter first letter of show, insert, delete, or find: i

Enter key value to insert: 13

Enter first letter of show, insert, delete, or find: s

Table: ** 47 70 ** ** 74 ** ** ** ** ** ** ** 13 ** ** ** 109 ** ** ** ** 22

Enter first letter of show, insert, delete, or find:

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

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

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

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

Get Started

Students also viewed these Databases questions