Question

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 hash table class, you must provide comparable functionality. Your hash table must support a parameter to the constructor to determine table size. The default size should be 100. For this exercise, set your hash table size to 178,000. Keys will normally be unique, but there is no guarantee of this. To deal with duplicates, your functions should match only the first match found. If you are coding in Java, you may not use the hashcode member function (from Object) unless you have overridden it with your own (appropriate) code. You may find it helpful to store the entire record as a string for easier output.

The starter code uses an abstract base class (C++) or Interface (Java) to allow for somewhat generic data type storage, but this is not required in your code. You may use another method of making your code somewhat generic, or you may "hard code" your hash table to work with a specific data object type that you create.

C++ stub class hashTable{ public: hashTable(int=100); // build a table void insert(record *); // insert copy of record void delete(int); // delete a record // return pointer to a copy of found record, or 0 record *search(int); private: // find return value: some type of index record *find(int); // helper search fn int hash(record *); // hash value for record int hash(int); // hash value for key const int m; // size of table // array of m lists that hold records // other members as desired }; // abstract base class for data // also overload operator= if necessary // and/or copy constructor, destructor // clone method invokes copy constructor // to make a copy of the object class record{ public: virtual int getID()=0; // or something similar virtual record *clone()=0; // or something similar // anything else that you think is appropriate };

Create a "main program" that presents the user with a menu that allows the user to issue any of the following commands. You must use the noted integer value (with a newline) to select each option:


insert data from a file by entering the file name (merging with any entries already in the table) (prompt for the filename to load)


insert a new entry from the keyboard (prompt to enter record all on one line)


delete a record by key value (prompt for the key) and report results


search for a record by key value (prompt for the key) (if found, display entire record, otherwise indicate that the data was not found)


clear the entire hash table


save the entire hash table to disk (prompt for filename). Make sure that you use the same format specified for input (see below) so that your resulting file could be used as input in a subsequent session of your (or another) program.


quit


INPUT FILES


An ascii text file containing one nine digit integer (leading zeros are a possibility) followed by a space followed by additional unspecified information. Line length is guaranteed to be less than 256 characters. The id number is the key. Each record is on one line. The number of records is not specified. Do not modify the input file or make more than one pass through the file.

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

import java.util.ArrayList;
class HashNode<K, V>
{
   K key;
   V value;  
   HashNode<K, V> next;
   public HashNode(K key, V value)
   {
       this.key = key;
       this.value = value;
   }
}  
class Map<K, V>
{  
   private ArrayList<HashNode<K, V>> bucketArray;
   private int numBuckets;
   private int size;

public Map()
   {
       bucketArray = new ArrayList<>();
       numBuckets = 10;
       size = 0;

for (int i = 0; i < numBuckets; i++)
           bucketArray.add(null);
   }

   public int size() { return size; }
   public boolean isEmpty() { return size() == 0; }

private int getBucketIndex(K key)
   {
       int hashCode = key.hashCode();
       int index = hashCode % numBuckets;
       return index;
   }

public V remove(K key)
   {
int bucketIndex = getBucketIndex(key);
       HashNode<K, V> head = bucketArray.get(bucketIndex);

       HashNode<K, V> prev = null;
       while (head != null)
       {
if (head.key.equals(key))
               break;
           prev = head;
           head = head.next;
       }

if (head == null)
           return null;

size--;

if (prev != null)
           prev.next = head.next;
       else
           bucketArray.set(bucketIndex, head.next);

       return head.value;
   }

public V get(K key)
   {
int bucketIndex = getBucketIndex(key);
       HashNode<K, V> head = bucketArray.get(bucketIndex);

while (head != null)
       {
           if (head.key.equals(key))
               return head.value;
           head = head.next;
       }

return null;
   }

public void add(K key, V value)
   {
int bucketIndex = getBucketIndex(key);
       HashNode<K, V> head = bucketArray.get(bucketIndex);

while (head != null)
       {
           if (head.key.equals(key))
           {
               head.value = value;
               return;
           }
           head = head.next;
       }

size++;
       head = bucketArray.get(bucketIndex);
       HashNode<K, V> newNode = new HashNode<K, V>(key, value);
       newNode.next = head;
       bucketArray.set(bucketIndex, newNode);

if ((1.0*size)/numBuckets >= 0.7)
       {
           ArrayList<HashNode<K, V>> temp = bucketArray;
           bucketArray = new ArrayList<>();
           numBuckets = 2 * numBuckets;
           size = 0;
           for (int i = 0; i < numBuckets; i++)
               bucketArray.add(null);

           for (HashNode<K, V> headNode : temp)
           {
               while (headNode != null)
               {
                   add(headNode.key, headNode.value);
                   headNode = headNode.next;
               }
           }
       }
   }

public static void main(String[] args)
   {
       Map<String, Integer>map = new Map<>();
       map.add("this",1 );
       map.add("coder",2 );
       map.add("this",4 );
       map.add("hi",5 );
       System.out.println(map.size());
       System.out.println(map.remove("this"));
       System.out.println(map.remove("this"));
       System.out.println(map.size());
       System.out.println(map.isEmpty());
   }
}

Add a comment
Know the answer?
Add Answer to:
PROGRAM DESCRIPTION Implement a hash table class. Your hash table should resolve collisions by chaining and...
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...

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

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

  • Following class is only part of my program that uses a hash table to simulate a...

    Following class is only part of my program that uses a hash table to simulate a market's client database, client class is my main class which asks user to input client names and then creates a hash table which then users can look up client names. wasn't able to upload everything as it is too much code, but what I need is to modify my client class instead of me inputting data line by line, the program should read from...

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

  • In C++ I am trying to implement a log base 2 hash table utilizing the shift...

    In C++ I am trying to implement a log base 2 hash table utilizing the shift function. So, the code determines the position of where the value will be stored using log2. But am having issues using the shift part as everything just wants to pile on to zero. //ChainingHash - log2 #include<iostream> #include<vector> #include<iterator> #include<string> #include<cmath> using namespace std; #define BUCKET 10 //no. of buckets class Hash {    vector<int>*table; //ptr containing buckets       public:        Hash();...

  • Design a primary hash table data structure. a. Each hash table has an array of node...

    Design a primary hash table data structure. a. Each hash table has an array of node pointers and can point a head of a linked list. Suppose the node class is given to you. The data type of each node is int. b. Override the default constructor with a constructor that has one default parameter called capacity. What happens to your default constructor? Create some instances of the sequence class using the new constructor in multiple ways. c. Design the...

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

  • Java using data structures The objective is to create your own Hash Table class to hold...

    Java using data structures The objective is to create your own Hash Table class to hold a list of employees and their ID numbers. I've provided the TableEntry class which will be each data object in the hash table. The list of employees will be provided as a .txt file and must be read with the code. please create a .txt file called Employees.txt with the info provided so that the java code can read it in. Employees.txt: (No WhiteSpace...

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

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