Question: I am having trouble getting the below program to run and have tried many different ways to fix the errors. Here are the instructions: 1.
I am having trouble getting the below program to run and have tried many different ways to fix the errors. Here are the instructions:
1. First, implement the hash table class described in the text book. You WILL need to modify the code in one specific area to make this test valid!
2. Next, write a test method (you can use the main() method of your HashTable class) that adds values to the hash table. You can generate random integers, or read words from a file, or anything convenient for you (I generated random numbers using StdRandom.uniform(1000000)). Remember that you must call the hashcode() method for whatever key you are adding. Each time a value is added, you will need to measure and record the displacement (how far each entry had to be shifted from its natural position, due to collision).
3. I used M = 1000 for my testing and it seemed to work well.
4. Generate a data set consisting of the average displacement across the whole data set (all N keys). Actually, you will need to do this a number of times.
5. Run step 3 for a variety of values of N for a given M.
6. Produce a graph: the X values are the different ratios of N/M, and the Y values are the average displacement across a number of data sets. The completeness and accuracy of your graph will be a significant part of your grade (3 or 4 data points won't cut it).
7. Finally, discuss your findings.
linearProbingHashST.java = https://algs4.cs.princeton.edu/34hash/LinearProbingHashST.java.html
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ package com.company; import java.lang.*; import edu.princeton.cs.algs4.*; import java.lang.Object; import java.security.Key; public class Main { private int N; // number of key-value pairs in the table private int M = 16; // size of linear-probing table private Key[] keys; // the keys private Value[] vals; // the values public Main() { keys = (Key[]) new Object[M]; vals = (Value[]) new Object[M]; } private int hash(Key key) { return (key.hashCode() & 0x7fffffff) % M; } private void resize() {} // See code below public void put(Key key, Value val) { if (N >= M/2) resize(2*M); // double M int i; for (i = hash(key); keys[i] != null; i = (i + 1) % M) if (keys[i].equals(key)) { vals[i] = val; return; } keys[i] = key; vals[i] = val; N++; } public Value get(Key key) { for (int i = hash(key); keys[i] != null; i = (i + 1) % M) if (keys[i].equals(key)) return vals[i]; return null; } private void resize(int cap) { LinearProbingHashST t; t = new LinearProbingHashST(cap); for (int i = 0; i < M; i++) if (keys[i] != null) t.put(keys[i], vals[i]); keys = t.keys; vals = t.vals; M = t.M; } public static void main(String[] args) { HashTable table = new HashTable(2); String key,value; } } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
