Question

Implement an eager delete() method for linear probing hash table. The eager delete will delete the pair from the hash table, not just set the value to null.
*Algorithhms*

In Java Language Please

I want to add the delete method to this hash table code.

public class Linear ProbingHashST<Key, Value> private int M = 30001; private Value [] vals = (Value[]) new Object[M]: private

private Value get(key key) {/ previous slide */ } public void put(key key, Value val) int i; for (i = hash (key); keys[i] !=

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

Here is the Solution :

I have added boolean remove(final long key) to delete the item & Created One Test class UsageHash to test the functionality. I have explained all important lines by using Java comments.

class HashTableEx {
final int SIZE = 300001; // size of the table
long[] a = new long[SIZE]; int[] ex = new int[SIZE];
// adds new key
boolean add(final long key) {
int i = h(key), j = 1;
for (; ex[i] != 0; i = (i + 1) % SIZE, j++)
if (a[i] == key)
return false;
ex[i] = j;
a[i] = key;
return true;
}

// returns true, if key is contained in the table
boolean contains(final long key) {
for (int i = h(key); ex[i] != 0; i = (i + 1) % SIZE)
if (a[i] == key)
return true;
return false;
}

// removes key from the table
boolean remove(final long key) {
for (int i = h(key); ex[i] != 0; i = (i + 1) % SIZE)
if (a[i] == key) {
ex[i] = 0;
compress(i);
return true;
}
return false;
}

// compresses lists
void compress(int free) {
int i = (free + 1) % SIZE, off = 1;
for (; ex[i] != 0; i = (i + 1) % SIZE, off++)
if (ex[i] > off) {
// move current element
a[free] = a[i];
ex[free] = ex[i] - off;
// mark current slot as free
ex[i] = 0;
off = 0;
free = i;
}
}

int h(final long key) {
return (int) Math.abs(key % SIZE);
}
}
  
public class UsageHash
{
public static void main(String[] args)
{
//Creating instance of HashTableEX
HashTableEx objh=new HashTableEx();
objh.add(12);
objh.add(13);
//Checking if 12 Exist !!! before Removing
System.out.println("Output Before Deletion :"
+objh.contains(12));
//Removing 12 From HashTableEx
objh.remove(12);
//Checking Hash Table after Removing 12
System.out.println("Output After Deletion :"
+objh.contains(12));
  
}
}

Add a comment
Know the answer?
Add Answer to:
Implement an eager delete() method for linear probing hash table. The eager delete will delete the...
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...

  • Identify the letters and anything associated with it. It is a hash map so please feel...

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

  • C++ assignment - Hash table with linear probing --- implement in single file - main.cpp You...

    C++ assignment - Hash table with linear probing --- implement in single file - main.cpp You are asked to implement a very specific hash table. The keys are lower-case English words (e.g., apple, pear). The length of a key is at most 10. The hash function is “simply using the last character”. That is, the hash value of apple should be e, and the hash value of pear should be r. Your hash table contains exactly 26 slots (hash value...

  • I need help fixing my java code for some reason it will not let me run...

    I need help fixing my java code for some reason it will not let me run it can someone pls help me fix it. class LinearProbingHashTable1 { private int keyname; private int valuename; LinearProbingHashTable1(int keyname, int valuename) { this.keyname = keyname; this.valuename = valuename; } public int getKey() { return keyname; } public int getValue() { return valuename; } } class LinearProbingHashTable2 { private final static int SIZE = 128; LinearProbingHashTable2[] table; LinearProbingHashTable2() { table = new LinearProbingHashTable2[SIZE]; for (int...

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

  • Write a method in the HashIntSet class called addAll that accepts another hash set as a...

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

  • Type up and get the Hash table on pages 19 - 22 to work. Show your...

    Type up and get the Hash table on pages 19 - 22 to work. Show your sample test run at the bottom of your code using a multiline comment I typed everything but the code is not working please help me fix the mistakes. These are pages 19-22. The code I have: #include <iostream> #inlcude <iomanip> #include <stack> #include <vector> #include <cstdlib> #include <ctime> using namespace std; //////////////////////////////HASH TABLE/////////////////////////////////////////////// //hash.cpp //demonstrate hash table with linear probing /////////////////////////////////////////////////////////////////////////////////////// class DataItem {...

  • Can anyone helps to create a Test.java for the following classes please? Where the Test.java will...

    Can anyone helps to create a Test.java for the following classes please? Where the Test.java will have a Scanner roster = new Scanner(new FileReader(“roster.txt”); will be needed in this main method to read the roster.txt. public interface List {    public int size();    public boolean isEmpty();    public Object get(int i) throws OutOfRangeException;    public void set(int i, Object e) throws OutOfRangeException;    public void add(int i, Object e) throws OutOfRangeException; public Object remove(int i) throws OutOfRangeException;    } public class ArrayList implements List {   ...

  • JAVA QUESTION: *******THE QUESTION:******** /** numberOfNodesAtDepth    *    * Returns the number of nodes with...

    JAVA QUESTION: *******THE QUESTION:******** /** numberOfNodesAtDepth    *    * Returns the number of nodes with depth == d    * Precondition: none    *    * param: d the depth to search for    *    * hint: use a recursive helper function    *        * ToDo 4    */    public int numNodesAtDepth(int d) {        return -1;    } **********USEFUL CODE FROM THE ASSIGNMENT:*********** public class simpleBST<Key extends Comparable<Key>, Value> {    private Node root;            ...

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