Hash Table a) You are given a hash table of initial capacity 5, and need to use
ID: 3813977 • Letter: H
Question
Hash Table
a) You are given a hash table of initial capacity 5, and need to use the hash function h(key) = key mod table capacity. Create the smallest dataset of distinct non-negative integer keys that will be inserted in the hash table so that, after inserting all the integers in the dataset, the average number of comparisons for successful search is less than 3, and the worst case number of comparisons is 6. Show the hashtable with all your dataset integers in it, and work out the average. (Assume that the load factor threshold is large enough that no rehashing will happen, and that all items are equally likely to be searched.)
b) Create the smallest dataset for the same requirements as above, plus an additional constraint that the load factor threshold is 2, and you must force exactly one rehash. Assume that a rehash doubles the capacity of the table. Show the contents of the hashtable just before rehash, as well as the final hashtable, and work out the average.
Explanation / Answer
import java.util.Scanner;
import java.math.*;
/* category LinkedHashEntry */
class HashEntry
creator */
HashEntry(String key, int value)
price;
}
}
/* category HashTable */
class HashTable
non-public int TABLE_SIZE;
non-public int size;
non-public HashEntry[] table;
non-public int primeSize;
/* builder */
public HashTable(int ts)
/* perform to urge prime quantity but table size for myhash2 perform */
public int getPrime()
reality = 0;
for (int j = 2; j <= (int) scientific discipline.sqrt(i); j++)
if (i forward == 0)
fact++;
if (fact == 0)
return i;
}
/* come back a main range */
return 3;
}
/* perform to urge range of key-value pairs */
public int getSize()
{
come back size;
}
public Boolean isEmpty()
{
come back size == 0;
}
/* perform to clear hash table */
public void makeEmpty()
/* perform to urge worth of a key */
public int get(String key)
whereas (table[hash1] != null && !table[hash1].key.equals(key))
come back table[hash1].value;
}
/* perform to insert a key worth combine */
public void insert(String key, int value)
table[hash1] = new HashEntry(key, value);
size++;
}
/* perform to get rid of a key */
public void remove(String key)
{
int hash1 = myhash1( key );
int hash2 = myhash2( key );
whereas (table[hash1] != null && !table[hash1].key.equals(key))
table[hash1] = null;
size--;
}
/* perform myhash which provides a hash worth for a given string */
non-public int myhash1(String x )
{
int hashVal = x.hashCode( );
hashVal %= TABLE_SIZE;
if (hashVal < 0)
hashVal += TABLE_SIZE;
come back hashVal;
}
/* perform myhash perform for double hashing */
non-public int myhash2(String x )
{
int hashVal = x.hashCode( );
hashVal %= TABLE_SIZE;
if (hashVal < 0)
hashVal += TABLE_SIZE;
come back primeSize - hashVal the troubles primeSize;
}
/* perform to print hash table */
public void printHashTable()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.