Answered step by step
Verified Expert Solution
Link Copied!

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 HashtableChaining(int buckets){
bucket = new ArrayList<>();
numBuckets = buckets;
size =0;
// create empty chains
for(int i =0; i < numBuckets; i++){
bucket.add(null);
}
}
public int size(){
return size;
}
public boolean isEmpty(){
return size()==0;
}
private int getBucketIndex(K key){
// Implement Multiplicative hash
// Assume INITIAL_VALUE =7 HASH_MULTIPLIER =3
}
//Adds a key value pair to hash
public void add(K 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 (0.75), then double the hash size
// and rehash the existing items
// print "Doubling the size, load factor >0.75" when you double the hash size.
}
// Returns value for a key
public V get(K 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 remove(K 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 main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Enter the initial capacity:");
int size = input.nextInt();
HashtableChaining hashtable = new HashtableChaining<>(size);
System.out.println("Adding items to hashtable:");
hashtable.add("FALL2017",3);
hashtable.add("SPRING2018",3);
hashtable.add("SUMMER2018",1);
hashtable.add("FALL2018",4);
hashtable.add("SPRING2019",2);
hashtable.add("SPRING2019",3);
System.out.printf("Size of the hashtable is %d%n", hashtable.size());
System.out.println("Removing key SUMMER2018");
System.out.printf("The value is %d%n", hashtable.remove("SUMMER2018"));
System.out.printf("Now the size of the hashtable is %d%n",
hashtable.size());
System.out.println("Looking up key SUMMER2018");
System.out.printf("The value is %s%n",
hashtable.get("SUMMER2018"));
System.out.printf("Is the hashtable empty? :%s%n",
hashtable.isEmpty()? "True" : "False");
input.close();
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2010 Barcelona Spain September 2010 Proceedings Part 2 Lnai 6322

Authors: Jose L. Balcazar ,Francesco Bonchi ,Aristides Gionis ,Michele Sebag

2010th Edition

364215882X, 978-3642158827

More Books

Students also viewed these Databases questions

Question

(2) Determining the general form of the model. Pg45

Answered: 1 week ago

Question

5. Identify the logical fallacies, deceptive forms of reasoning

Answered: 1 week ago

Question

6. Choose an appropriate organizational strategy for your speech

Answered: 1 week ago