Question: In this problem, you will complete another implementation of this interface one that uses separate chaining instead of open addressing. Its class will be called
In this problem, you will complete another implementation of this interface one that uses separate chaining instead of open addressing. Its class will be called ChainedHashTable.We have given you a skeleton for this class in the ps folder. It includes two private fields:one called numKeys for the number of keys in the hash tableone called table that holds a reference to the array that you will use for the hash table. However, rather than having each element of the array store a single entry as we do in open addressing, each element of the array must serve as a bucket or chain of entries.We have given you a private inner class called Node for the nodes in each chain. Each element of the table array must hold a reference to the first node in its linked list of nodes, and you will need to create and manipulate these nodes within your hash table methods.Each Node object has fields that allow it to store:a single keythe collection of values associated with that key, using an instance of our LLQueue classa reference to the next node in the linked list if anyWe have also given you:a copy the h method from our OpenHashTable class, which you must use as the hash function of your implementation Note: Because separate chaining allows all entries whose keys are hashed to a given position in the table to stay in that position, you will not need to perform any probing. As a result, you wont need the second hash function h from OpenHashTable.a toString method that will allow you to see the current contents of the table. It returns a string of the formtable table tablesize where each position of the table is represented by either:the key or keys in that positions chain of entries, separated by semicolons and surrounded by curly braces.the word null if there are no keys in that position.Your tasksReview all of the provided code, and make sure that you understand itAdd a constructor that takes the size of the hash table as its only parameter and initializes the hash table accordingly. It should throw an IllegalArgumentException if the size is not positive.Implement each method from the HashTable interface as efficiently as possible. Make sure that each of these methods has the same basic functionality as the corresponding method from the OpenHashTable class discussed in lecture. In particular, they should throw exceptions for the same reasons that the OpenHashTable methods doAs you add or remove items from the table, make sure that you update numKeys accordingly.Because a given chain can grow to an arbitrary length, the hash table will never overflow, and thus your insert method can always return true.For example, if you run this test code:ChainedHashTable table new ChainedHashTable;table.inserthowdy;table.insertgoodbye;System.out.printlntableinsertapple;System.out.printlntable;you should see:trueapple; howdy null, null, goodbye nullNote that:Position has a chain with two keys, because both "howdy" and "apple" are assigned a hash code of by h when the table size is Position has a chain with one key, because "goodbye" is assigned a hash code of by h when the table size is Define an accessor method for the number of keys. Call this method getNumKeys For example, this test code:ChainedHashTable table new ChainedHashTable;table.inserthowdy;table.insertgoodbye;table.insertapple;System.out.printlntablegetNumKeys;table.inserthowdy; insert a duplicateSystem.out.printlntablegetNumKeys;should print:because inserting a duplicate does not change the number of keys.Although a hash table that uses separate chaining wont overflow, it can become too full to offer decent performance. To allow clients to measure the degree of fullness, add a method called load that takes no parameters and that returns a value of type double that represents the load factor of the table: the number of keys in the table divided by the size of the table.For example, this test code:ChainedHashTable table new ChainedHashTable;table.inserthowdy;table.insertgoodbye;table.insertapple;System.out.printlntableload;table.insertpear;System.out.printlntableload;should print:To allow clients to obtain all of the keys in the hash table, add a method called getAllKeys that takes no parameters and that returns an array of type Object containing all of the keys in the hash table.For example, the following test code:import java.util.;ChainedHashTable table new ChainedHashTable;table.inserthowdy;table.insertgoodbye;table.insertapple;table.inserthowdy; insert a duplicateObject keys table.getAllKeys;System.out.printlnArraystoStringkeys;should print:apple howdy, goodbyeTo deal with situations in which the table becomes too full, add a method called resize that takes an integer representing the new size, and that grows the table to have that new size. It should not return a value.As discussed in lecture, it is not possible to simply copy every element of the current table into a new, larger table. This is because a given key may belong in a different position in the larger table. As a result, you will need to rehash the current keys in the hash table to ensure that they end up in the correct position in the resized table.For example, the following test code:ChainedHashTable table new ChainedHashTable;table.inserthowdy;table.insertgoodbye;table.insertapple;System.out.printlntable;table.resize;System.out.printlntable;should print:apple; howdy null, null, goodbye nullnullapple null, null, null, howdygoodbyeNote that in this case, resizing the table causes all three keys to end up in different positions!Special cases:The method should throw an IllegalArgumentException if the specified new size is less than the tables current size.If the specified new size is the same as the tables current size, the method should return without doing anything.Add a main method that performs at least two unit tests for each of the methods in the class. Use the same unittest format that we specified in Problem of Problem Set
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
