Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Scenario Develop an algorithm to search and remove data from a hash table using the open addressing technique. Aim Implement a hash table using open

Scenario
Develop an algorithm to search and remove data from a hash table using the open addressing technique.
Aim
Implement a hash table using open addressing with linear probing.
Prerequisites
To solve this activity, you have to implement the following methods in the OpenAddrHashTable.java file:
public void put(K key, V value){
//...
}
private int searchPosition(K key){
//...
}
public void remove(K key){
//...
}
public Optional get(K key){
//...
}
Steps for Completion
Study the pseudocode shown in Snippet 3.3 and Snippet 3.4(shown below) and implement them in Java.
The container class OpenAddrPair will hold your key and value in the hash table.
Have a flag on this container to indicate when an item is deleted.
Use this flag in the put operation to overwrite it if it is deleted. You can use this flag to optimize your get method using the filter method.
insert(key, value, array)
s = length(array)
hashValue = hash(key, s)
i =0
while (i < s and array[(hashValue + i) mod s]!= null)
i = i + l
if (i < s) array[(hashValue + i) mod s]=(key, value)
Snippet 3.3: Psuedocode for inserting using linear probing
search(key, array)
s = length(array)
hashValue = hash(key, s)
i =0
while (i < s and array[(hashValue + i) mod s]!= null and array[(hashValue + i) mod s].key != key)
i = i +1
keyValue = array[(hashValue + i) mod s]
if (keyValue != null && keyValue.key == key) return keyValue.value
else return null
Snippet 3.4: Solution pseudocode for searching using linear probing
Grading
Complete each task listed below. Each task contains automated checks which are used to calculate your grade. When you have completed each task by clicking the checkbox, open the task list panel on the left navigation bar and click the "Submit" button.
codes provided:
HashProvider.java
public interface HashProvider {
int hashKey(K key, int tableSize);
}
HashTable.java
import java.util.Optional;
public interface HashTable {
void put(K key,V value);
Optional get(K key);
void remove(K key);
}
OpenAddrHashTable.java
import java.util.Optional;
public class OpenAddrHashTable implements HashTable {
private final HashProvider hashProvider;
private Pair[] array;
public OpenAddrHashTable(int capacity, HashProvider hashProvider){
array = new Pair[capacity];
this.hashProvider = hashProvider;
}
public void put(K key, V value){
// write your code here
}
private int searchPosition(K key){
// write your code here
}
public void remove(K key){
// write your code here
}
public Optional get(K key){
// write your code here
return Optional.empty();
}
}
OpenAddrPair.java
public class OpenAddrPair {
private final K key;
private final V value;
private boolean deleted;
public OpenAddrPair(K key, V value){
this.key = key;
this.value = value;
this.deleted = false;
}
public K getKey(){
return key;
}
public V getValue(){
return value;
}
public boolean isDeleted(){
return deleted;
}
public void setDeleted(boolean deleted){
this.deleted = deleted;
}
}
Pair.java
public class Pair {
private final K key;
private final V value;
public Pair(K key, V value){
this.key = key;
this.value = value;
}
public K getKey(){
return key;
}
public V getValue(){
return value;
}
}
RemainderHashing.java
public class RemainderHashing implements HashProvider{
public int hashKey(Integer key, int tableSize){
return key % tableSize;
}
public static void main(String[] args){
RemainderHashing remainderHashing = new RemainderHashing();
System.out.println(remainderHashing.hashKey(337481990,1000));
System.out.println(remainderHashing.hashKey(116241990,1000));
System.out.println(remainderHashing.hashKey(983611990,1000));
System.out.println(remainderHashing.hashKey(201031990,1000));
System.out.println(remainderHashing.hashKey(337481990,1447));
System.out.println(remainderHashing.hashKey(116241990,1447));
System.out.println(remainderHashing.hashKey(983611990,1447));
System.out.println(remainderHashing.hashKey(201031990,1447));
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Logic In Databases International Workshop Lid 96 San Miniato Italy July 1 2 1996 Proceedings Lncs 1154

Authors: Dino Pedreschi ,Carlo Zaniolo

1st Edition

3540618147, 978-3540618140

More Books

Students also viewed these Databases questions

Question

Demonstrate three aspects of assessing group performance?

Answered: 1 week ago