Using Java, code a dynamic Linear Quotient Hashing data structure that outputs the search
length, current loading factor, pseudo loading factor, and current density after any of the 4 basic
operations (fetch, delete, update, insert) are used.
Solution
import java.util.*;
class Dynamic
{
ArrayList<Integer> arr;
HashMap<Integer, Integer> hash;
public MyDS()
{
arr = new ArrayList<Integer>();
hash = new HashMap<Integer, Integer>();
}
void add(int x)
{
if (hash.get(x) != null)
return;
int s = arr.size();
arr.add(x);
hash.put(x, s);
}
void remove(int x)
{
Integer index = hash.get(x);
if (index == null)
return;
hash.remove(x);
int size = arr.size();
Integer last = arr.get(size-1);
Collections.swap(arr, index, size-1);
arr.remove(size-1);
hash.put(last, index);
}
int getRandom()
{
Random rand = new Random();
int index = rand.nextInt(arr.size());
return arr.get(index);
}
Integer search(int x)
{
return hash.get(x);
}
}
class Main
{
public static void main (String[] args)
{
Dynamic ds = new Dynamic();
ds.add(10);
ds.add(20);
ds.add(30);
ds.add(40);
System.out.println(ds.search(30));
ds.remove(20);
ds.add(50);
System.out.println(ds.search(50));
System.out.println(ds.getRandom());
}
}
Using Java- code a dynamic Linear Quotient Hashing data structure that.docx

Using Java- code a dynamic Linear Quotient Hashing data structure that.docx

  • 1.
    Using Java, codea dynamic Linear Quotient Hashing data structure that outputs the search length, current loading factor, pseudo loading factor, and current density after any of the 4 basic operations (fetch, delete, update, insert) are used. Solution import java.util.*; class Dynamic { ArrayList<Integer> arr; HashMap<Integer, Integer> hash; public MyDS() { arr = new ArrayList<Integer>(); hash = new HashMap<Integer, Integer>(); } void add(int x) { if (hash.get(x) != null) return; int s = arr.size(); arr.add(x); hash.put(x, s); } void remove(int x)
  • 2.
    { Integer index =hash.get(x); if (index == null) return; hash.remove(x); int size = arr.size(); Integer last = arr.get(size-1); Collections.swap(arr, index, size-1); arr.remove(size-1); hash.put(last, index); } int getRandom() { Random rand = new Random(); int index = rand.nextInt(arr.size()); return arr.get(index); } Integer search(int x) { return hash.get(x); } } class Main { public static void main (String[] args) { Dynamic ds = new Dynamic(); ds.add(10); ds.add(20); ds.add(30); ds.add(40); System.out.println(ds.search(30)); ds.remove(20); ds.add(50); System.out.println(ds.search(50)); System.out.println(ds.getRandom()); } }