Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Your task is to go through and implement the methods getBucketIndex, add, get, and remove. Follow the comments inside respective methods. import java.util.ArrayList; import java.util.Scanner;
Your task is to go through and implement the methods getBucketIndex, add, get, and remove. Follow the comments inside respective methods.
import java.util.ArrayList;
import java.util.Scanner;
public class HashtableChaining
Hashtable bucket
private ArrayList bucket;
Current capacity of the array list
private int numBuckets;
current size of the array list
private int size;
public HashtableChainingint buckets
bucket new ArrayList;
numBuckets buckets;
size ;
create empty chains
forint i ; i numBuckets; i
bucket.addnull;
public int size
return size;
public boolean isEmpty
return size;
private int getBucketIndexK key
Implement Multiplicative hash
Assume INITIALVALUE HASHMULTIPLIER
Adds a key value pair to hash
public void addK key, V value
Find head of chain for given key
Check if key is already present
Insert key in chain
If load factor goes beyond threshold then double the hash size
and rehash the existing items
print "Doubling the size, load factor when you double the hash size.
Returns value for a key
public V getK key
Find head of chain for given key
Search key in chain
If key not found return null
return null;
Removes key from the hashtable
public V removeK key
Apply hash function to find index for a given key
get head of the chain
Search for key in its chain
If key is found
Reduce size
Remove key and return
Else keep moving in the chain
If key was not there return null
return null;
public static void mainString args
Scanner input new ScannerSystemin;
System.out.printlnEnter the initial capacity:";
int size input.nextInt;
HashtableChaining hashtable new HashtableChainingsize;
System.out.printlnAdding items to hashtable:";
hashtable.addFALL;
hashtable.addSPRING;
hashtable.addSUMMER;
hashtable.addFALL;
hashtable.addSPRING;
hashtable.addSPRING;
System.out.printfSize of the hashtable is dn hashtable.size;
System.out.printlnRemoving key SUMMER;
System.out.printfThe value is dn hashtable.removeSUMMER;
System.out.printfNow the size of the hashtable is dn
hashtable.size;
System.out.printlnLooking up key SUMMER;
System.out.printfThe value is sn
hashtable.getSUMMER;
System.out.printfIs the hashtable empty? :sn
hashtable.isEmpty "True" : "False";
input.close;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started