This is for java I need a program that inserts N/2 random int keys into an array
ID: 3606456 • Letter: T
Question
This is for java I need a program that inserts N/2 random int keys into an array of size N using the linear probing strategy described in the context of hash tables. I need this to work for N = 103, 104, 105, 106. Note: we're not actually hashing an entry here, just imagining that we've been given the results of some hash function for N/2 elements, where the hash function is randomly distributed.
Next, we wish to compute the average cost for a search miss in the resulting partly-filled array. In other words, suppose a new element that doesn't match one of the hashed elements is given to us, and the hash function maps it to a random location in the array; how many probes will be required to recognize that this entry does not have a match? (remember: once a hashed search lands on a filled cell, it must proceed along sequential positions until it finds an empty one)
Do this for N = 103, 104, 105, 106. Discuss the extent to which your results validate Proposition M in the book (p 473): In a linear-probing hash table with M lists and N = M keys, the average number of probes (under Assumption J) required is
for search hits and search misses (or inserts), respectively. Capture your discussion in the README file.
and 1Explanation / Answer
Solution:
code:
import java.util.Scanner;
/** LinearProbingHashTable class **/
class LinearProbingHashTable
{
// class members private variables.
private int currentSize, maximumSize;
private String[] keys;
private String[] vals;
/** parameter Constructor **/
public LinearProbingHashTable(int capacity)
{
currentSize = 0;
maximumSize = capacity;
keys = new String[maximumSize];
vals = new String[maximumSize];
}
/** Function to clear hash table **/
public void makeEmpty()
{
currentSize = 0;
keys = new String[maximumSize];
vals = new String[maximumSize];
}
/** Function to get size of hash table **/
public int getSize()
{
return currentSize;
}
/** Function to check if hash table is full **/
public boolean isFull()
{
return currentSize == maximumSize;
}
/** Function to check if hash table is empty **/
public boolean isEmpty()
{
return getSize() == 0;
}
/** Fucntion to check if hash table contains a key **/
public boolean contains(String key)
{
return get(key) != null;
}
/** Functiont to get hash code of a given key **/
private int hash(String key)
{
return key.hashCode() % maximumSize;
}
/** Function to insert key-value pair **/
public void insert(String key, String val)
{
int tmp = hash(key);
int i = tmp;
do
{
if (keys[i] == null)
{
keys[i] = key;
vals[i] = val;
currentSize++;
return;
}
if (keys[i].equals(key))
{
vals[i] = val;
return;
}
i = (i + 1) % maximumSize;
} while (i != tmp);
}
/** Function to get value for a given key **/
public String get(String key)
{
int i = hash(key);
while (keys[i] != null)
{
if (keys[i].equals(key))
return vals[i];
i = (i + 1) % maximumSize;
}
return null;
}
/** Function to remove key and its value **/
public void remove(String key)
{
if (!contains(key))
return;
/** find position key and delete **/
int i = hash(key);
while (!key.equals(keys[i]))
i = (i + 1) % maximumSize;
keys[i] = vals[i] = null;
/** rehash all keys **/
for (i = (i + 1) % maximumSize; keys[i] != null; i = (i + 1) % maximumSize)
{
String tmp1 = keys[i], tmp2 = vals[i];
keys[i] = vals[i] = null;
currentSize--;
insert(tmp1, tmp2);
}
currentSize--;
}
/** Function to print HashTable **/
public void printHashTable()
{
System.out.println(" Hash Table: ");
for (int i = 0; i < maximumSize; i++)
if (keys[i] != null)
System.out.println(keys[i] +" "+ vals[i]);
System.out.println();
}
}
/** Class LinearProbingHashTableTest **/
public class LinearProbingHashTableTest
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Hash Table Test ");
System.out.println("Enter size");
/** maximumSizeake object of LinearProbingHashTable **/
LinearProbingHashTable lpht = new LinearProbingHashTable(scan.nextInt() );
char ch;
/** Perform LinearProbingHashTable operations **/
do
{
System.out.println(" Hash Table Operations ");
System.out.println("1. insert ");
System.out.println("2. remove");
System.out.println("3. get");
System.out.println("4. clear");
System.out.println("5. size");
int choice = scan.nextInt();
switch (choice)
{
case 1 :
System.out.println("Enter key and value");
lpht.insert(scan.next(), scan.next() );
break;
case 2 :
System.out.println("Enter key");
lpht.remove( scan.next() );
break;
case 3 :
System.out.println("Enter key");
System.out.println("Value = "+ lpht.get( scan.next() ));
break;
case 4 :
lpht.makeEmpty();
System.out.println("Hash Table Cleared ");
break;
case 5 :
System.out.println("Size = "+ lpht.getSize() );
break;
default :
System.out.println("Wrong Entry ");
break;
}
/** Display hash table **/
lpht.printHashTable();
System.out.println(" Do you want to continue (Type y or n) ");
ch = scan.next().charAt(0);
} while (ch == 'Y'|| ch == 'y');
}
}
/*
This is a Java Program to implement hash tables with Linear Probing. A hash table (also hash map) is a data structure used to implement an associative array, a structure that can map keys to values. A hash table uses a hash function to compute an index into an array of buckets or slots, from which the correct value can be found. Linear probing is a probe sequence in which the interval between probes is fixed (usually 1).
*/
/* output:-
Hash Table Test
Enter size
2
Hash Table Operations
1. insert
2. remove
3. get
4. clear
5. size
1
Enter key and value
K pottasium
Hash Table:
K pottasium
Do you want to continue (Type y or n)
y
Hash Table Operations
1. insert
2. remove
3. get
4. clear
5. size
1
Enter key and value
Na Sodium
Hash Table:
Na Sodium
K pottasium
Do you want to continue (Type y or n)
n
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.