Java Quadratic Probing Hash table HELP!
Complete the Map and Entry class provided in Map.java. Create a tester/driver class to show the Map class is working as intended.
public class Map {
public Map()
public void put(KeyType key, ValueType val)
public ValueType get(KeyType key)
public boolean isEmpty()
public void makeEmpty()
private QuadraticProbingHashTable> items;
private static class Entry
{
KeyType key;
ValueType value;
//appropriate constructions,
etc.
}
}
public class QuadraticProbingHashTable
{
/**
* Construct the hash table.
*/
public QuadraticProbingHashTable( )
{
this( DEFAULT_TABLE_SIZE );
}
/**
* Construct the hash table.
* @param size the approximate initial size.
*/
public QuadraticProbingHashTable( int size )
{
allocateArray( size );
doClear( );
}
/**
* Insert into the hash table. If the item is
* already present, do nothing.
* @param x the item to insert.
*/
public boolean insert( AnyType x )
{
// Insert x as active
int currentPos = findPos( x );
if( isActive( currentPos ) )
return false;
array[ currentPos ] = new HashEntry<>( x, true );
theSize++;
// Rehash; see Section 5.5
if( ++occupied > array.length / 2 )
rehash( );
return true;
}
/**
* Expand the hash table.
*/
private void rehash( )
{
HashEntry [ ] oldArray = array;
// Create a new double-sized, empty table
allocateArray( 2 * oldArray.length );
occupied = 0;
theSize = 0;
// Copy table over
for( HashEntry entry : oldArray )
if( entry != null && entry.isActive )
insert( entry.element );
}
/**
* Method that performs quadratic probing resolution.
* @param x the item to search for.
* @return the position where the search terminates.
*/
private int findPos( AnyType x )
{
int offset = 1;
int currentPos = myhash( x );
while( array[ currentPos ] != null &&
!array[ currentPos ].element.equals( x ) )
{
currentPos += offset; // Compute ith probe
offset += 2;
if( currentPos >= array.length )
currentPos -= array.length;
}
return currentPos;
}
/**
* Remove from the hash table.
* @param x the item to remove.
* @return true if item removed
*/
public boolean remove( AnyType x )
{
int currentPos = findPos( x );
if( isActive( currentPos ) )
{
array[ currentPos ].isActive = false;
theSize--;
return true;
}
else
return false;
}
/**
* Get current size.
* @return the size.
*/
public int size( )
{
return theSize;
}
/**
* Get length of internal table.
* @return the size.
*/
public int capacity( )
{
return array.length;
}
/**
* Find an item in the hash table.
* @param x the item to search for.
* @return the matching item.
*/
public boolean contains( AnyType x )
{
int currentPos = findPos( x );
return isActive( currentPos );
}
public AnyType get( AnyType x )
{
int currentPos = findPos( x
);
if( isActive( currentPos ))
return
array[currentPos].element;
else
return
null;
}
/**
* Return true if currentPos exists and is active.
* @param currentPos the result of a call to findPos.
* @return true if currentPos is active.
*/
private boolean isActive( int currentPos )
{
return array[ currentPos ] != null && array[ currentPos
].isActive;
}
/**
* Make the hash table logically empty.
*/
public void makeEmpty( )
{
doClear( );
}
private void doClear( )
{
occupied = 0;
theSize = 0;
for( int i = 0; i < array.length; i++ )
array[ i ] = null;
}
private int myhash( AnyType x )
{
int hashVal = x.hashCode( );
hashVal %= array.length;
if( hashVal < 0 )
hashVal += array.length;
return hashVal;
}
private static class HashEntry
{
public AnyType element; // the element
public boolean isActive; // false if marked deleted
public HashEntry( AnyType e )
{
this( e, true );
}
public HashEntry( AnyType e, boolean i )
{
element = e;
isActive = i;
}
}
private static final int DEFAULT_TABLE_SIZE = 101;
private HashEntry [ ] array; // The array of elements
private int occupied; // The number of occupied cells
private int theSize; // Current size
/**
* Internal method to allocate array.
* @param arraySize the size of the array.
*/
private void allocateArray( int arraySize )
{
array = new HashEntry[ nextPrime( arraySize ) ];
}
/**
* Internal method to find a prime number at least as large as
n.
* @param n the starting number (must be positive).
* @return a prime number larger than or equal to n.
*/
private static int nextPrime( int n )
{
if( n % 2 == 0 )
n++;
for( ; !isPrime( n ); n += 2 )
;
return n;
}
/**
* Internal method to test if a number is prime.
* Not an efficient algorithm.
* @param n the number to test.
* @return the result of the test.
*/
private static boolean isPrime( int n )
{
if( n == 2 || n == 3 )
return true;
if( n == 1 || n % 2 == 0 )
return false;
for( int i = 3; i * i <= n; i += 2 )
if( n % i == 0 )
return false;
return true;
}
}
when compiling the above code. we will get the errors as follows
1. Expected ";" for the constructor Map() and remaining classes
2.Accessing private class will give error as it belongs to only that particular class.
3.Paramteres are passing in isEmpty() and makeEmpty() whereas those two functions aren't assed any parameters in the main class.
Main.java:10: error: ';' expected
public Map()
^
Main.java:12: error: ';' expected
public void put(KeyType key, ValueType val)
^
Main.java:13: error: ';' expected
public ValueType get(KeyType key)
^
Main.java:14: error: ';' expected
public boolean isEmpty()
^
Main.java:15: error: ';' expected
public void makeEmpty()
^
Main.java:17: error: expected
private QuadraticProbingHashTable> items;
^
Main.java:17: error: expected
private QuadraticProbingHashTable> items;
^
Main.java:9: error: class Map is public, should be declared in a
file named Map.java
public class Map {
^
Main.java:27: error: class QuadraticProbingHashTable is public,
should be declared in a file named
QuadraticProbingHashTable.java
public class QuadraticProbingHashTable
^
Main.java:12: error: cannot find symbol
public void put(KeyType key, ValueType val);
^
symbol: class KeyType
location: class Map
Main.java:12: error: cannot find symbol
public void put(KeyType key, ValueType val);
^
symbol: class ValueType
location: class Map
Main.java:13: error: cannot find symbol
public ValueType get(KeyType key);
^
symbol: class KeyType
location: class Map
Main.java:13: error: cannot find symbol
public ValueType get(KeyType key);
^
symbol: class ValueType
location: class Map
Main.java:52: error: cannot find symbol
public boolean insert( AnyType x )
^
symbol: class AnyType
location: class QuadraticProbingHashTable
Main.java:92: error: cannot find symbol
private int findPos( AnyType x )
^
symbol: class AnyType
location: class QuadraticProbingHashTable
Main.java:114: error: cannot find symbol
public boolean remove( AnyType x )
^
symbol: class AnyType
location: class QuadraticProbingHashTable
Main.java:150: error: cannot find symbol
public boolean contains( AnyType x )
^
symbol: class AnyType
location: class QuadraticProbingHashTable
Main.java:155: error: cannot find symbol
public AnyType get( AnyType x )
^
symbol: class AnyType
location: class QuadraticProbingHashTable
Main.java:155: error: cannot find symbol
public AnyType get( AnyType x )
^
symbol: class AnyType
location: class QuadraticProbingHashTable
Main.java:190: error: cannot find symbol
private int myhash( AnyType x )
^
symbol: class AnyType
location: class QuadraticProbingHashTable
Main.java:203: error: cannot find symbol
public AnyType element; // the element
^
symbol: class AnyType
location: class HashEntry
Main.java:206: error: cannot find symbol
public HashEntry( AnyType e )
^
symbol: class AnyType
location: class HashEntry
Main.java:211: error: cannot find symbol
public HashEntry( AnyType e, boolean i )
^
symbol: class AnyType
location: class HashEntry
Main.java:21: error: cannot find symbol
KeyType key;
^
symbol: class KeyType
location: class Entry
Main.java:22: error: cannot find symbol
ValueType value;
^
symbol: class ValueType
location: class Entry
Main.java:10: error: missing method body, or declare abstract
public Map();
^
Main.java:12: error: missing method body, or declare abstract
public void put(KeyType key, ValueType val);
^
Main.java:13: error: missing method body, or declare abstract
public ValueType get(KeyType key);
^
Main.java:14: error: missing method body, or declare abstract
public boolean isEmpty();
^
Main.java:15: error: missing method body, or declare abstract
public void makeEmpty();
^
Main.java:59: error: cannot infer type arguments for
HashEntry
array[ currentPos ] = new HashEntry<>( x, true );
^
reason: cannot use '<>' with non-generic class HashEntry
Java Quadratic Probing Hash table HELP! Complete the Map and Entry class provided in Map.java. Create...
The task of this project is to implement in Java Hash Table structure using Linear Probing Collision Strategy. You can assume that no duplicates or allowed and perform lazy deletion (similar to BST). Specification Create a generic class called HashTableLinearProbe <K,V>, where K is the key and V is the value. It should contain a private static class, HashEntry<K,V>. Use this class to create array to represent Hashtable: HashEntry<K,V> hashtable[]; Implement all methods listed below and test each method...
Write a method in the HashIntSet class called addAll that accepts another hash set as a parameter and adds all of the elements from the other set into the current set. For example, if the set stores [-5, 1, 2, 3] and the method is passed [2, 3, 6, 44, 79], your set would store [-5, 1, 2, 3, 6, 44, 79]. Write a method in the HashIntSet class called containsAll that accepts another hash set as a parameter and...
Identify the letters and anything associated with it. It is a hash map so please feel free to point out the other important parts beside the arrows. I apologize for the order of the pictures class HashMap private: HashEntry table ; public: HashMap() table-new HashEntry * [TABLE_SIZE]; for (int 1-0; 1< TABLE SIZE: i++) table [i] = NULL; Hash Function int HashFunc(int key) return key % TABLE-SIZE; Insert Element at a key void Insert(int key, int value) int hashHashFunc(key); while...
Currently working on a Java Assignment. I have written most codes for swap, reverse and insert. Just need a. itemCount receives a value and returns a count of the number of times this item is found in the list. c. sublist receives two indexes and returns an ArrayList of node values from the first index to the second index, provided the indexes are valid. d. select receives a variable number of indexes, and returns an ArrayList of node values corresponding...
IN JAVA LANGUAGE:
For this lab you are required for implement the following
methods:
1. public QuadraticProbingHashTable( int size ):
As the signature of this method suggests, this is a constructor
for this class. This constructor will initialize status of an
object from this class based on the input parameter (i.e., size).
So, this function will create the array HashTable with the
specified size and initialize all of its elements to be null.
2. public int hash(int value, int tableSize...
JAVA (advanced data structures) write a completed program using
HashIntSet and HashMain
include the method in the main if possible. those are just the
sample HashIntSet and HashMain (don't have to be the same but
follow the Q requirement. thanks
HashIntSet.java
public class HashIntSet {
private static final double MAX_LOAD_FACTOR = 0.75;
private HashEntry[] elementData;
private int size;
// Constructs an empty set.
public HashIntSet() {
elementData = new HashEntry[10];
size = 0;
}
// Adds the given element to...
JAVA
Submit: HashSet.java
with the following methods added:
HashSet.java
// Implements a set of objects using a hash table.
// The hash table uses separate chaining to resolve
collisions.
// Original from buildingjavaprograms.com supplements
public class HashSet<E> {
private static final double MAX_LOAD_FACTOR = 0.75;
private HashEntry<E>[] elementData;
private int size;
// Constructs an empty set.
@SuppressWarnings("unchecked")
public HashSet() {
elementData = new HashEntry[10];
size = 0;
}
// ADD METHODS HERE for exercise solutions:
// Adds the...
* The Map class is used to create and manipulate voting maps. The value of a * cell on the map denotes the party for which the majority of the population * of that cell votes for. For instance, in the following map, PARTY_X is the * choice of voters in three cells, while PARTY_O is preferred in the rest of * the map: * O X O * X O X * O O O * A map is...
Hash Tables. (Hint: Diagrams might be helpful for parts a) and b). ) When inserting into hash table we insert at an index calculated by the key modulo the array size, what would happen if we instead did key mod (array_size*2), or key mod (array_size/2)? (Describe both cases). Theory answer Here Change your hashtable from the labs/assignments to be an array of linkedlists – so now insertion is done into a linkedlist at that index. Implement insertion and search. This...
Implement the remove() and the rehash() methods for the following externally chained hash table. Note you should rehash when "size > arraySize" at the start of the put() method. The new size of the arraySize should be "(2 * old array size) + 1" import java.util.ArrayList; import java.util.Hashtable; import java.util.LinkedList; import java.util.Map; public class ExternalChainingHashTable<K,V> { private class Mapping { private Mapping next; private K key; private V value; private Mapping(K key, V value) { this.key = key; this.value =...