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 find(int 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 -1. 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 (i.e., 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 0 & the entry as active. **/
public HashEntry(){
element =0;
isActive = true;
}
/** Constructor that initializes the element with the given value & sets the entry as active. **/
public HashEntry(int v){
element = v;
isActive = true;
}
/** Constructor that initializes the element with the given value & the active flag with the given status. **/
public HashEntry(int 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 QuadraticProbingHashTable(int size){
this.currentSize =0;
HashTable = new HashEntry[size];
}
/** Inserts an integer into the hash table. If the item is already present,
* this method does nothing. If the load factor exceeds 0.75, 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 insert(int x){
if ((this.currentSize +1>=.75* HashTable.length) &&
(this.currentSize !=0)){
rehash();
}
int index = hash(x, HashTable.length);
int detected_colision =0;
while ((HashTable[index]!= null) &&
(HashTable[index].isActive) &&
(HashTable[index].element != x)){
detected_colision++;
index =(hash(x, HashTable.length)+ detected_colision * detected_colision)% HashTable.length;
}
HashTable[index]= new HashEntry(x, true);
currentSize = currentSize +1;
}
/*** 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 HashEntry[currentSize];
TempHash = HashTable;
HashTable = new HashEntry[2* HashTable.length];
for (int i =0; i < TempHash.length; i++){
if (TempHash[i]!= null){
insert(TempHash[i].element);
}
}
}
/** A simple HashTable(x) function for integer values. This method returns a hash value
* between 0 and `tableSize -1` inclusive, using the modulo operator. The returned
* hash value is computed to handle both positive and negative integers. ***/
public int hash(int value, int tableSize){
if (value <0){
return -1* 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 blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!