Question

Write a hash function to implement a digit-folding approach in the hash function. Your program should...

Write a hash function to implement a digit-folding approach in the hash function. Your program should work for any array size and any key length. Use linear probing. Accessing a group of digits in a number may be easier than you think. Does it matter if the array size is not a multiple of 10?(Written in Java)

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

//please find the below code

import java.io.*;

class FoldingHashTable
{
   private HashDataItem[] hashArray;
   private int arraySize;
   private HashDataItem nonItem;
  
   public FoldingHashTable(int size)
   {
       arraySize = size;
       hashArray = new HashDataItem[arraySize];
       nonItem = new HashDataItem(-1); //deleted item key is -1
   }
  
   public void displayTable()
   {
       System.out.print("Table: ");
       for(int j = 0; j < arraySize; j++)
       {
           if(hashArray[j] != null)
               System.out.print(hashArray[j].getKey() + " ");
           else
               System.out.print("** ");
       }
       System.out.println("");
   }
  
   //folds the key into groups of digits and adds the groups
   public int hashFunc(int key)
   {
       int groupSize = 1;
       int temp = arraySize;
       int hashVal = 0;
      
       while(temp > 0)
       {
           temp /= 10;
           groupSize *= 10;
       }
      
       while(key > 0)
       {
           hashVal += key % groupSize;
           key /= groupSize;
       }
      
       return hashVal;
   }
  
   public void insert(HashDataItem item)
   {
       //assumes table not full
       int key = item.getKey();
       int hashVal = hashFunc(key);
       while(hashArray[hashVal] != null && hashArray[hashVal].getKey() != -1)
       {
           ++hashVal;
           hashVal %= arraySize;
       }
       hashArray[hashVal] = item;
   }
  
   public HashDataItem delete(int key)
   {
       int hashVal = hashFunc(key);
      
       while(hashArray[hashVal] != null)
       {
           if(hashArray[hashVal].getKey() == key)
           {
               HashDataItem temp = hashArray[hashVal];
               hashArray[hashVal] = nonItem;
               return temp;
           }
           ++hashVal;
           hashVal %= arraySize;
       }
       return null;
   }
  
   public HashDataItem find(int key)
   {
       int hashVal = hashFunc(key);
       while(hashArray[hashVal] != null)
       {
           if(hashArray[hashVal].getKey() == key)
               return hashArray[hashVal];
           ++hashVal;
           hashVal %= arraySize;
       }
       return null;
   }
} //end class HashTable

public class FoldingHashTableApp
{
   public static void main(String[] args) throws IOException
   {
       HashDataItem aDataItem;
       int aKey, size, n, keysPerCell;
      
       System.out.print("Enter size of hash table: ");
       size = getInt();
       System.out.print("Enter initial number of items: ");
       n = getInt();
       keysPerCell = 10;
      
       HashTable theHashTable = new HashTable(size);
      
       for(int j=0; j<n; j++)
       {
           aKey = (int)(java.lang.Math.random() * keysPerCell * size);
           aDataItem = new HashDataItem(aKey);
           theHashTable.insert(aDataItem);
       }
      
       while(true)
       {
           System.out.print("Enter first letter of show, insert, delete, or find: ");
           char choice = getChar();
           switch(choice)
           {
           case 's':
               theHashTable.displayTable();
               break;
           case 'i':
               System.out.print("Enter key value to insert: ");
               aKey = getInt();
               aDataItem = new HashDataItem(aKey);
               theHashTable.insert(aDataItem);
               break;
           case 'd':
               System.out.print("Enter key value to delete: ");
               aKey = getInt();
               theHashTable.delete(aKey);
               break;
           case 'f':
               System.out.print("Enter key value to find: ");
               aKey = getInt();
               aDataItem = theHashTable.find(aKey);
               if(aDataItem != null)
                   System.out.println("Found " + aKey);
               else
                   System.out.println("Could not find " + aKey);
               break;
           default:
               System.out.println("Invalid entry!");
           }
       }
   }//end main
  
   public static String getString() throws IOException
   {
       InputStreamReader isr = new InputStreamReader(System.in);
       BufferedReader br = new BufferedReader(isr);
       String s = br.readLine();
       return s;
   }
  
   public static char getChar() throws IOException
   {
       String s = getString();
       return s.charAt(0);
   }
  
   public static int getInt() throws IOException
   {
       String s = getString();
       return Integer.parseInt(s);
   }
} //end HashTableApp


import java.io.*;

class HashDataItem
{
   private int iData;
   public HashDataItem(int ii)
       { iData = ii; }
  
   public int getKey()
       { return iData; }
}

class HashTable
{
   private HashDataItem[] hashArray;
   private int arraySize;
   private HashDataItem nonItem;
   private double numItems;   //used to determine loadFactor
  
   public HashTable(int size)
   {
       numItems = 0;
       arraySize = size;
       hashArray = new HashDataItem[arraySize];
       nonItem = new HashDataItem(-1); //deleted item key is -1
   }
  
   public void displayTable()
   {
       System.out.print("Table: ");
       for(int j = 0; j < arraySize; j++)
       {
           if(hashArray[j] != null)
               System.out.print(hashArray[j].getKey() + " ");
           else
               System.out.print("** ");
       }
       System.out.println("");
   }
  
   public double getLoadFactor()
   { return (numItems / (double)arraySize); }
  
   public HashTable rehash()
   {
       //create a new table based on the original one
       int newSize = getPrime(arraySize*2);
       HashTable newTable = new HashTable(newSize);
       newTable.setNumItems(numItems); //transfer numItems from oldtable to newtable
      
       //for all the values in the old hash table, re-insert them into the new table.
       for(int j = 0; j < arraySize; j++)
       {
           if(hashArray[j] != null && hashArray[j].getKey() != -1)
               newTable.rehashInsert(hashArray[j], newSize);
       }
      
      
       //then return that table
       return newTable;
   }
  
   private int getPrime(int min)   //returns 1st prime > min
   {
       for(int j = min+1; true; j++)
       if( isPrime(j) )
           return j;
   }
  
   private boolean isPrime(int n)
   {
       for(int j = 2; (j*j <= n); j++)
           if(n%j == 0)
               return false;
       return true;
   }
  
   public int hashFunc(int key)
   {
       return key % arraySize;
   }
  
   //used by rehash() to calculate new locations for keys
   private void rehashInsert(HashDataItem item, int size)
   {
       //assumes table not full
       int key = item.getKey();
       int hashVal = key % size;
       while(hashArray[hashVal] != null && hashArray[hashVal].getKey() != -1)
       {
           ++hashVal;
           hashVal %= arraySize;
       }
       hashArray[hashVal] = item;
   }
  
   private void setNumItems(double number)
   { numItems = number; }
  
   public void insert(HashDataItem item)
   {
       //assumes table not full
       int key = item.getKey();
       int hashVal = hashFunc(key);
       while(hashArray[hashVal] != null && hashArray[hashVal].getKey() != -1)
       {
           ++hashVal;
           hashVal %= arraySize;
       }
       hashArray[hashVal] = item;
       numItems++;
   }
  
   public HashDataItem delete(int key)
   {
       int hashVal = hashFunc(key);
      
       while(hashArray[hashVal] != null)
       {
           if(hashArray[hashVal].getKey() == key)
           {
               HashDataItem temp = hashArray[hashVal];
               hashArray[hashVal] = nonItem;
               numItems--;
               return temp;
           }
           ++hashVal;
           hashVal %= arraySize;
       }
       return null;
   }
  
   public HashDataItem find(int key)
   {
       int hashVal = hashFunc(key);
       while(hashArray[hashVal] != null)
       {
           if(hashArray[hashVal].getKey() == key)
               return hashArray[hashVal];
           ++hashVal;
           hashVal %= arraySize;
       }
       return null;
   }

}

Add a comment
Know the answer?
Add Answer to:
Write a hash function to implement a digit-folding approach in the hash function. Your program should...
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
  • Write a hash function to implement a digit-folding approach in the hash function. Your program should...

    Write a hash function to implement a digit-folding approach in the hash function. Your program should work for any array size and any key length. Use linear probing. Accessing a group of digits in a number may be easier than you think. Does it matter if the array size is not a multiple of 10? #java#withcomments please

  • PROGRAM DESCRIPTION Implement a hash table class. Your hash table should resolve collisions by chaining and...

    PROGRAM DESCRIPTION Implement a hash table class. Your hash table should resolve collisions by chaining and use the multiplication method to generate hash keys. You may use your own linked list code from a previous assignment or you can use a standard sequence container. Partial definitions for the hash table class and a generic data class are provided (C++ and Java versions). You may use them as a starting point if you wish. If you choose to design your own...

  • This should be in Java Create a simple hash table You should use an array for...

    This should be in Java Create a simple hash table You should use an array for the hash table, start with a size of 5 and when you get to 80% capacity double the size of your array each time. You should create a class to hold the data, which will be a key, value pair You should use an integer for you key, and a String for your value. For this lab assignment, we will keep it simple Use...

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

  • create a hash table to implement spell checker in java 1. Create a file of 100...

    create a hash table to implement spell checker in java 1. Create a file of 100 words of varying length (max 8 characters). 2. All the words are in lower case. 3. design a hash table. 4. enter each word in the hash table. Once the hash table is created, run it as a spell checker.enter a word (interactive), find the word in the hash table. If not found enter an error message. Then prompt for next word. End the...

  • Write a Java program that is able to apply a hash function to a first and...

    Write a Java program that is able to apply a hash function to a first and last name (key) of a person, and lookup the appropriate index in a hash table modeling a telephone directory. The telephone number (value) should be returned on doing a lookup (you can pre-populate the table with 20 entries for this purpose). You can use existing Java code (or libraries) to model the hash table, however, your program should be customized to use the hash...

  • (JAVA) Develop pseudocode for the search operation in linear probing. Write pseudocode similar to Snippet 3.3,...

    (JAVA) Develop pseudocode for the search operation in linear probing. Write pseudocode similar to Snippet 3.3, included below, to show the search operation. The operation should return null if the key is not found in the hash table. The search function should have a signature as follows: search(key, array) Code Snippet 3.3 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...

  • Cryptographic hash functions are very commonly used in password verification. For example, when you use any...

    Cryptographic hash functions are very commonly used in password verification. For example, when you use any online website which requires a user login, you enter your E-mail and password to authenticate that the account you are trying to use belongs to you. When the password is entered, a hash of the password is computed which is then sent to the server for verification of the password. The passwords stored on the server are actually computed hash values of the original...

  • Write a program in Java to implement a recursive boolean function that returns True if the...

    Write a program in Java to implement a recursive boolean function that returns True if the given array contents remain the same when the array is reversed. Otherwise function must return False.

  • IN JAVA USING ECLIPSE The objective of this assignment is to create your own hash table...

    IN JAVA USING ECLIPSE The objective of this assignment is to create your own hash table class to hold employees and their ID numbers. You'll create an employee class to hold each person's key (id number) and value (name). Flow of the main program: Create an instance of your hash table class in your main method. Read in the Employees.txt file and store the names and ID numbers into Employee objects and store those in your hash table using 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