Question

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 ):

This method accepts an integer value and returns the hash value based on the table size. Note that you are supposed to use mod operator as we saw in class for your hash function. That means the hash value you are returning is going to be a number between 0 and tableSize-1. The input value can be positive or negative. This function is not supposed to handle the probing. Collision handling and probing should be part of your insert function (or another helper function).

3. public void insert( int x )

This function accepts a value (called x) and insert it into the hash table. If the item is already in the table, this method does nothing and returns. You need to resolve collision using quadratic probing. It is highly recommended that you write a helper function to do this (but this is not mandatory). Note that other fields of this class has to be updated accordingly after the insert. If the load factor of the table is 0.75 or higher after the insert, this function should perform rehashing as instructed below. For this assignment, check the load factor first (assuming that the value will be added to the table), if rehash is required, rehash first and then insert the value.

4. public void rehash( )

This function will increase the size of the hash table by a factor of two and rehash the elements into the new hash table (your insert method should call the rehash function as soon as the load factor of the hash table is equal or greater than 0.75.

do NOT change any signature (parameters) in any method.

no need to actually run the program. other files are required but not for this portion just yet.

package lab10; public class QuadraticProbingHashTable public HashEntry HashTable; I/ The array that holds the hash table public int currentSize; // The number of occupied cells // constructor to create the HashTable of initiaľ size = size // and sets all of its elements to null public QuadraticProbingHashTable ( int size ) // FILL IN // insert into the hash table // if the item is already present, do nothing and simply return // be careful you might need to rehash - reshape when the load factor is .75 // Hint: first check the load factor after add if the size is beyond rehash first! public void insert( int x) //FILL IN // this function will increase the size of the hash table by a factor of two /7 and do the rehash of the current elements inside the hash table public void rehash) // FILL IN // a simple hash function for int values /1 the hash value should be a number between and tablesize-1 // use the mod operator as suggested in class public int hash(int value, int tableSize) // FILL IN DO NOT RETURN-1 return -1

0 0
Add a comment Improve this question Transcribed image text
Answer #1

PROGRAM

public class QuadraticProbingHashTable

{

public HashEntry[] HashTable;

public int currentSize;

double load;

int size;

public QuadraticProbingHashTable(int size)

{

currentSize=0;

load=0;

this.size=size;

HashTable=new HashTable[size];

for(int i=0;i<size;i++)

HashTable[i]=null;

}

public void insert(int x)

{

if(load>=0.75)

rehash();

else

{

if(!search(x))

{

int h=hash(x,size);

HashTable[h]=x;

currentSize++;

load=(double)currentSize/(double)size;

if(load>=0.75)

rehash();

}

}

}

public boolean search(int x)

{

for(int i=0;i<size;i++)

{

if(x==HashTable[i])

{

return true;

}

}

return false;

}

public int hash(int value,int tablesize)

{

int h=value%tablesize;

if(HashTable[h]==null)

return h;

else

h=quadratic(value,tablesize);

return h;

}

public void rehash()

{

HashTable[] ht=new HashTable[size];

ht=HashTable;

HashTable=new HashTable[size*2];

size=size*2;

for(int i=0;i<size/2;i++)

{

if(ht[i]!=null)

{

insert(ht[i],size);

}

}

}

public int quadratic(int x,int tablesize)

{

int h,i=1;

h=x%tablesize;

while(true)

{

if(HashTable[h+i*i]==null)

return h+i*i;

else

i++;

}

}

}

public class QuadraticProbingHashTable public HashEntryL] HashTable public int currentSize; double load; int size; public QuadraticProbingHashTable(int size) currentSize-0 load-0; this.size-size; HashTable-new HashTable[size]; for(int i-0;i<size;i++) HashTableľi1-null: public void insert(int x) if(load>-0.75) rehash(); else if( search(x)) int h-hash(x,size); HashTable [h1-X; currentSize++; load-(double)currentSize/ (double)size; if(load>-0.75) rehash(); public boolean search(int x) for(int i-0;i<size;i++) if(x--HashTable[i]) return true;

Add a comment
Know the answer?
Add Answer to:
IN JAVA LANGUAGE: For this lab you are required for implement the following methods: 1. public...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The task of this project is to implement in Java Hash Table structure using Linear Probing...

    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...

  • Your task is to go through and implement the methods getBucketIndex, add, get, and remove. Follow...

    Your task is to go through and implement the methods getBucketIndex, add, get, and remove. Follow the comments inside respective methods. import java.util.ArrayList; import java.util.Scanner; public class HashtableChaining<K, V> {    // Hashtable bucket    private ArrayList<HashNode<K, V>> bucket;    // Current capacity of the array list    private int numBuckets;    // current size of the array list    private int size;       public HashtableChaining(int buckets){        bucket = new ArrayList<>();        numBuckets = buckets;   ...

  • Follow the TODOs and complete the insertItem(), searchItem() and printTable() functions in hash.cpp. The hash function...

    Follow the TODOs and complete the insertItem(), searchItem() and printTable() functions in hash.cpp. The hash function has already been implemented for you. // hash.CPP program to implement hashing with chaining #include<iostream> #include "hash.hpp" using namespace std; node* HashTable::createNode(int key, node* next) { node* nw = new node; nw->key = key; nw->next = next; return nw; } HashTable::HashTable(int bsize) { this->tableSize= bsize; table = new node*[tableSize]; for(int i=0;i<bsize;i++) table[i] = nullptr; } //function to calculate hash function unsigned int HashTable::hashFunction(int key)...

  • Hash Tables. (Hint: Diagrams might be helpful for parts a) and b). ) When inserting into...

    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...

  • Please answer this problem in C++, Thank you! Read and study the sample program: "hashing with chaining using singly...

    Please answer this problem in C++, Thank you! Read and study the sample program: "hashing with chaining using singly linked lists", as well as "hashing with chaining using doubly linked lists". Notice this program is complete except it doesn't include the remove function. Write the remove function and test it with the rest of the program including the given main program. Upload your C++ source file. ---Here is the referenced program: "hashing with chaining using singly linked lists". Below this...

  • You are given the following data 659, 230, 751, 291, 433, 955, 518, 34, 189, 239...

    You are given the following data 659, 230, 751, 291, 433, 955, 518, 34, 189, 239 to insert into a table of size 10. The hash function is hash(key) = key mod tableSize. Assume that the table is allowed to get full without starting a rehashing. Draw the resulting hash table when open addressing with quadratic probing is used. You must write the indices calculated by the hash function hash(key) and by the probing strategy hi(key) for all reattempts, for...

  • Need help on problems 3 and 4 only thanks in advance. Define the following terms: hash...

    Need help on problems 3 and 4 only thanks in advance. Define the following terms: hash table hash function perfect hash function What is a collision? Explain three ways of handling collisions (a program is not needed; a clear brief explanation suffices). Consider a hashing scheme that uses linear probing to resolve collisions. Design an algorithm (no code necessary) to delete an item from the hash table. What precautions do you need to take to make it work properly? Given...

  • ^b Given input( 66, 28, 43, 29, 44, 69, 19) and a hash function h(x) =...

    ^b Given input( 66, 28, 43, 29, 44, 69, 19) and a hash function h(x) = x mod 10, show the resulting hash table 1) Using Separate Chaining 2) Using Linear Probing 3) Using Quadratic Probing 4) Starting with the following hash function: h2(x) 7- (x mod 7), applv Rehash ary course slides ing as described in the prim Rehashing Increases the size of the hash table when load factor becomes "too high" (defined by a cutoff) - Anticipating that...

  • java The following code is an implementation of a HeapPriorityQueue. You are to implement the methods...

    java The following code is an implementation of a HeapPriorityQueue. You are to implement the methods commented with: // TODO: TIP: Do not just go from memory of your assignment implementation, be sure to consider carefully the constructor and method implementation provided to you. NOTE: You do not have to provide an implementation for any methods intentionally omitted public class Heap PriorityQueue implements PriorityQueue private final static int DEFAULT SIZE 10000 private Comparable [ ] storage private int currentSize: public...

  • JAVA Submit:    HashSet.java    with the following methods added: HashSet.java // Implements a set of...

    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...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT