Question: Java hashtable problem ( Problem e and f) Please just solve questions e and f . Here is the program below. Some functions are already
Java hashtable problem ( Problem e and f)
Please just solve questions e and f . Here is the program below. Some functions are already done in the program below which might help you public class MyHashTable { int[] keys; int[] values; int m; //table size int n; //item size public MyHashTable(int m){ this.m = m; keys = new int[m]; values = new int[m]; } int hash(int key){ return key % this.m; } void printArray(){ System.out.println("key array: "); for (int i = 0; i = m/2) { //rehashing return; } int b = hash(key); //has the key , b is bucket //check if something is there if (this.keys[b] > 0) { //collision //collision management System.out.println ("Collision!"); } else { this.keys[b]=key; this.values[b]=val; n+=1; } return ; } public int get(int key) { int b = hash(key); if (this.keys[b]==key) { return values[b]; } else { // empty or collision System.out.println ("empty or collision); return -1; } }
//implement other functions: rehashing and findprime public static void main() { MyHashTable hashtable = new MyHashTable(11); System.out.println(hashtable.hash(5)); for(int i = 1; i
3. (60)Hashtable with Quadratic Probing. Implement a simple hashtable using quadratic probing for collision resolution. Both the keys and values are integers, assuming greater than 0. The initial table size rn should be 11 (it is too small for a real hashtable, we just use it for the purpose of this homework). Let n be the number of items in the table. When nm/2, use the technique of dynamic arrays to enlarge the table. You want to approximately double the table size but keep to the primes. The next table size m will be 23 You should use key%m as the hash function. Let b be the hash value modulo m. If bucket b is occupied, you probe (D+ 12) % m, (D+ 22) % m, (b+32) % m, . .. , (D+ (m/2)2) % m, and stop as soon as you find an empty bucket As long as n is kept less than m/2, you will find an empty bucket by the end of the probing You should at least implement the following functions (a) void put (int key, int value): insert key-value pair to the hashtable. Insert key to the key array, value to the value array based on the hash function (b) int get (int key): get the value of the key (c) boolean contains(int key): return true if the hashtable contains the key d)void remove(int key): remove the key-value pair (e) void rehashing): this method is called automatically when n 2 m/2. You should enlarge he table and use findPrime(2 m) to get the new table size. You need to compute new hash index for every key stored in the existing hash table (f) nt findPrime(int x): find the next (the smallest) prime bigger than x. For example, ndPrime(8)-11, findPrime(22) 23
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
