Question: Create the following method: public int find ( int x ) This function searches for a given value ( x ) in the hash table
Create the following method: public int findint x
This function searches for a given value x in the hash table and returns its index if found. If the value is not present, it returns Due to possible collisions, quadratic probing should be used to locate the value. Given the following:
Represents an entry in a hash table. Each entry contains an element & a flag
indicating if the entry is active ie not deleted
public class HashEntry
The value stored in the hash table.
public int element;
A flag indicating if the entry is active true if active, false if deleted
public boolean isActive;
Default constructor that initializes the element to & the entry as active.
public HashEntry
element ;
isActive true;
Constructor that initializes the element with the given value & sets the entry as active.
public HashEntryint v
element v;
isActive true;
Constructor that initializes the element with the given value & the active flag with the given status.
public HashEntryint v boolean b
element v;
isActive b;
public class QuadraticProbingHashTable
The array that holds the hash table entries.
public HashEntry HashTable;
The number of occupied cells in the hash table.
public int currentSize;
Constructor to create a hash table of the specified initial size.
Initializes all elements in the table to null.
public QuadraticProbingHashTableint size
this.currentSize ;
HashTable new HashEntrysize;
Inserts an integer into the hash table. If the item is already present,
this method does nothing. If the load factor exceeds the table is rehashed.
Note: Before inserting the item, the load factor is checked. If the load factor
is above the threshold, rehashing is performed first, followed by insertion.
Hint: first check the what the load factor would be after add if the size is beyond rehash first!
public void insertint x
if thiscurrentSize HashTable.length &&
thiscurrentSize
rehash;
int index hashx HashTable.length;
int detectedcolision ;
while HashTableindex null &&
HashTableindexisActive &&
HashTableindexelement x
detectedcolision;
index hashx HashTable.length detectedcolision detectedcolision HashTable.length;
HashTableindex new HashEntryx true;
currentSize currentSize ;
Increases the size of the hash table by a factor of two and rehashes
the current elements into the new, larger hash table.
public void rehash
HashEntry TempHash new HashEntrycurrentSize;
TempHash HashTable;
HashTable new HashEntry HashTable.length;
for int i ; i TempHash.length; i
if TempHashi null
insertTempHashielement;
A simple HashTablex function for integer values. This method returns a hash value
between and tableSize inclusive, using the modulo operator. The returned
hash value is computed to handle both positive and negative integers.
public int hashint value, int tableSize
if value
return value tableSize; placeholder
else
return value tableSize;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
