Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I would greatly appreciate if you could come up with a solution for this. Object

ID: 3804000 • Letter: I

Question

I would greatly appreciate if you could come up with a solution for this.

Objective Work with hash tables by creating a hash table using linear probing Description: Create a generic class called Linear ProbingHashTable K, V It should contain a private static class, K, Java an array generic class, create the array for the table like this: Entry K, V table declare generic table new Entry size] create as non-generic Note that this will generate a warning message when compiled Your class should have the following methods. The methods should all operate on the object making the call (none are static) Perform checking of the parameters and throw exceptions where appropriate You may use code from the textbook, but all other code must be your own

Explanation / Answer

import java.util.*;
import java.io.*;

class linearProbingHashTable<K,V>{
   int size;
   int totalElement;
   int[] arrayKey;
   V[] arrayValue;       //arrayValue is an array of genric type

   public linearProbingHashTable(){
       size=13;   //initial size set to 13
       totalElement=0;       //initially total elements will be 0
       arrayKey=new int[size];
       arrayValue=new V[size];       //two seprate arrays for key value pair instead of two dimensional array
   }

   public boolean insert(int k, V value){
       if(totalElement>=size/2){   //check if array is half full
           size*=2;
           int[] tempKey=arrayKey;
           V[] tempValue=arrayValue;
           arrayKey=new int[size];
           arrayValue=new V[size];
           for(int i=0; i<size/2; i++){   //rehash each element to new table of double size
               insert(tempKey[i],tempValue[i]);
           }
       }
       else{
           int x=k%size;   //hash function h=key%size
           if(arrayValue[x]==null){       //check if cell at position x is empty
               arrayKey[x]=k;
               arrayValue[x]=value;
               totalElement++;
               return true;
           }
           else{   //check next closest empty cell (linear probing)
               while(arrayValue[x%size]!=null){       //x%size: once value of x exceed the size, by taking a mod(x%size) it will continue search from index 1.
                   x++;
               }
               arrayKey[x%size]=k;
               arrayValue[x%size]=value;
               return true;
           }
       }
       return false;
   }

   public V find(int k){
       int x=k%size;
       if(arrayKey[x]==k){
           return arrayValue[x];
       }
       else{
           int y=x-1;
           while(x%size!=y){       //this nested loop will exactly check n key i.e complete one circular cycle
               if(arrayKey[x%size]==k){
                   return arrayValue[x];
               }
               else{
                   x++;
               }
           }
       }
       return null;
   }

   public boolean delete(int k){
       int x=k%size;
       if(arrayKey[x]==k){
           arrayKey[x]=-1;       //set flag -1
           arrayValue[x]=null;       //set cell to null
           return true;
       }
       else{
           int y=x-1;
           while(x%size!=y){
               if(arrayKey[x%size]==k){
                   arrayKey[x]=-1;
                   arrayValue[x]=null;
                   return true;
               }
               else{
                   x++;
               }
           }
       }
       return false;
   }

   public int getLocation(int k){
       int x=k%size;
       if(arrayKey[x]==k){
           return x;
       }
       else{
           int y=x-1;
           while(x%size!=y){
               if(arrayKey[x%size]==k){
                   return x%size;
               }
               else{
                   x++;
               }
           }
       }
       return -1;  
   }

}

class linearHash{
   public static void main(String[] args){

   }
}