Question: My code is not giving me the right output, which isExpected output: 0 - > 5 , 1 - > 2 , 2 - >

My code is not giving me the right output, which isExpected output: 0->5,1->2,2->21,4->5. What do I do to fix this? Here is my code and the interface: import java.util.function.Function;
public class DoubleHashTable implements OpenAddressTable {
private int[] table;
private int size;
private Function h1;
private Function h2;
public DoubleHashTable(int size, Function h1, Function h2){
if (size <=0){
throw new IllegalArgumentException("Table size must be greater than 0");
}
this.size = size;
this.table = new int[size];
this.h1= h1;
this.h2= h2;
}
@Override
public double loadFactor(){
int count =0;
for (int value : table){
if (value !=0){
count++;
}
}
return (double) count / size;
}
@Override
public void insert(int value){
if (value <0){
throw new IllegalArgumentException("Value must be non-negative");
}
int probe =0;
int index = hash(value, probe);
while (table[index]!=0){
probe++;
index =(index + probe * h2.apply(value))% size;
}
table[index]= value;
}
@Override
public int find(int value){
int index = h1.apply(value);
int step = h2.apply(value);
int probe =0;
while (table[index]!=0 && table[index]!= value && probe < size){
index =(index + step)% size;
probe++;
}
return (probe < size && table[index]== value)? index : -1;
}
@Override
public int hash(int key, int probenumber){
// Double hashing using h1 and h2 functions
int result =(h1.apply(key)+ probenumber * h2.apply(key))% size;
return (result <0)? result + size : result;
}
@Override
public String toString(){
StringBuilder result = new StringBuilder();
for (int i =0; i < size; i++){
if (table[i]!=0){
int index = i; // Use the current index directly
if (result.length()>0){
result.append(",");
}
result.append(index).append("->").append(table[i]);
}
}
return result.toString();
}
public static void main(String[] args){
Function h1= key -> key %10;
Function h2= key -> key %5;
DoubleHashTable t = new DoubleHashTable(5, h1, h2);
t.insert(4);
t.insert(5);
t.insert(21);
t.insert(2);
System.out.println(t.toString());
// Expected output: 0->5,1->2,2->21,4->5
}
}
Current output is: 0->5,1->21,2->2,4->4
This is the interface:
public interface OpenAddressTable {
/**
* @return the load factor of the table.
*/
double loadFactor();
/**
* The dictionary insert operation.
* @param value the value to be inserted into the dictionary.
* @throws java.lang.NullPointerException if the value is null.
*/
void insert(int value);
/**
* The dictionary search operation.
* @param value the value to search.
* @return the index associated with the given value, or -1 if the value is
* not present in the dictionary.
*/
int find(int value);
/**
* The extended hash function, which takes a pair of values (the key and the
* probe number; where a probe number i indicates the ith slot
* in the probe sequence.
* @param key the key to be stored or searched in the hash table.
* @param probenumber the number that specifies which element of the probe
* sequence is to be calculated.
* @return the slot number (i.e., index or position) to be probed in the table.
*/
int hash(int key, int probenumber);
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!