Question

To implement a HashTable class in C++ that allows: - the definition of the size (M) of the static...

To implement a HashTable class in C++ that allows:

- the definition of the size (M) of the static array for the hash table

- the definition of a hash function HASH(X) suitable for storing string keys (not integers). The student is expected to propose his/her own hash table.

- a method for inserting keys in the hash table. The solution MUST consider chaining (e.g. linked lists, trees, etc.) as the collision response criteria.

- a method for searching keys in the hash table. The solution has also to determine how many comparisons were necessary to find a given key or to decide that the given is absent.

- a method for deleting keys from the hash table.

- The resulting program should insert 10,000 distinct keys in the table and then present a report of the total number of collisions that occurred in the process.

- the resulting program should search 1,000 distinct keys and determine the average number of comparisons while finding these keys

- the resulting program should delete 1,000 distinct keys

- insertions, searches and deletions should occur at random times (meaning not to perform them as sequences).

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

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package printerjob;

import java.util.Hashtable;
import java.util.Scanner;
import java.util.Set;

/**
*
* @author Harsh
*/
public class HashFunctionDemo {
    // return index of hashtable
    static int collisioncount;
    static int HashFunction(String str,int MAX){
        int index=(str.length()*3)%MAX;// function for hash
       return index;
    }
static boolean ExistElement (Hashtable<Integer,String> hashtable,String str){
     Set<Integer> keys = hashtable.keySet();//print all key and value from hashtable
        for(Integer key: keys){
            if(hashtable.get(key).equals(str))
                return false;
          
        }
        return true;// return true if not exist
   
}

static void SearchinHashtable (Hashtable<Integer,String> hashtable,String str){
     Set<Integer> keys = hashtable.keySet();//print all key and value from hashtable
     int f=0,com=0;
        for(Integer key: keys){
            com++;
            if(hashtable.get(key).equals(str))
                System.out.println("Total Comparison required for searching:"+com);
            f=1;
            break;
          
        }
        if(f==1){
            System.out.println("Not Found in Hashtable");
             System.out.println("Total Comparison required for searching:"+com);
        }
     
}
    public static void main(String[] args) {
        Hashtable<Integer,String> hashtable=new Hashtable<>();
        Scanner scanner=new Scanner(System.in);
        String ch;
        int MAX=10;
        for(int i=0;i<MAX;i++){
            hashtable.put(i, "empty");//insert empty intially
        }
        do{
            System.out.println("1.Insert String 2.Search String 3. Deletion");
            int cho=(int)((Math.random()*10)%3);
            cho++;
            switch(cho){
                case 1:
                    System.out.println("Enter String");
            String str=scanner.next();//accept string
            boolean re=ExistElement(hashtable,str);
            if(re==true){
            int index=HashFunction(str,MAX);
            if(hashtable.get(index).equals("empty")){// insert 1st time
                hashtable.replace(index, str);// replace string
            }
            else{// if collision is there
                System.out.println("Collision Occure");
                collisioncount++;
                for(int i=index+1;i<MAX;i++){
                if(hashtable.get(i).equals("empty")){
                    hashtable.replace(i, str);// replace string
                    System.out.println("Inserted at Location "+i);
                    break;
                }
                collisioncount++;
                System.out.println("Collision Occure again for next");
               }
              
            }
            }
            else{
                System.out.println("Element Alredy Exist try with unique element");
            }
                    break;
                case 2:
                    System.out.println("Enter String to be search");
                    str=scanner.next();//accept string
                    SearchinHashtable(hashtable,str);
                    break;
                case 3:
                    for(int i=0;i<MAX;i++){
                          hashtable.put(i, "empty");//delelte all string
                    }
                    System.out.println("Deleted all String");
                    break;
            }
          
          
            System.out.println("Do you want to continue yes/no");
            ch=scanner.next();
          
        }while(ch.equals("yes") || ch.equals("Yes"));
        Set<Integer> keys = hashtable.keySet();//print all key and value from hashtable
        for(Integer key: keys){
            System.out.println(""+key+" :: "+hashtable.get(key));
        }
        System.out.println("Total collisioncount:"+collisioncount);
    }
  
}

/*

output

1.Insert String 2.Search String 3. Deletion
Enter String to be search
bhushan
Not Found in Hashtable
Total Comparison required for searching:1
Do you want to continue yes/no
yes
1.Insert String 2.Search String 3. Deletion
Enter String to be insert
bhushan
Do you want to continue yes/no
yes
1.Insert String 2.Search String 3. Deletion
Enter String to be search
mahajan
Not Found in Hashtable
Total Comparison required for searching:1
Do you want to continue yes/no
yes
1.Insert String 2.Search String 3. Deletion
Enter String to be search
mahajan
Not Found in Hashtable
Total Comparison required for searching:1
Do you want to continue yes/no
yes
1.Insert String 2.Search String 3. Deletion
Enter String to be search
bhuh
Not Found in Hashtable
Total Comparison required for searching:1
Do you want to continue yes/no
yes
1.Insert String 2.Search String 3. Deletion
Deleted all String
Do you want to continue yes/no
yes
1.Insert String 2.Search String 3. Deletion
Deleted all String
Do you want to continue yes/no
no
9 :: empty
8 :: empty
7 :: empty
6 :: empty
5 :: empty
4 :: empty
3 :: empty
2 :: empty
1 :: empty
0 :: empty
Total collisioncount:0

*/

Add a comment
Know the answer?
Add Answer to:
To implement a HashTable class in C++ that allows: - the definition of the size (M) of the static...
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
  • 5. Hashing (a) Consider a hash table with separate chaining of size M = 5 and...

    5. Hashing (a) Consider a hash table with separate chaining of size M = 5 and the hash function h(x) = x mod 5. i. (1) Pick 8 random numbers in the range of 10 to 99 and write the numbers in the picked sequence. Marks will only be given for proper random numbers (e.g., 11, 12, 13, 14 ... or 10, 20, 30, 40, .. are not acceptable random sequences). ii. (2) Draw a sketch of the hash table...

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

  • == Programming Assignment == For this assignment you will write a program that reads in the...

    == Programming Assignment == For this assignment you will write a program that reads in the family data for the Martian colonies and stores that data in an accessible database. This database allows for a user to look up a family and its immediate friends. The program should store the data in a hashtable to ensure quick access time. The objective of this assignment is to learn how to implement and use a hashtable. Since this is the objective, you...

  • C++ Binary Search Tree question. I heed help with the level 2 question please, as level...

    C++ Binary Search Tree question. I heed help with the level 2 question please, as level 1 is already completed. I will rate the answer a 100% thumbs up. I really appreciate the help!. Thank you! searching.cpp #include <getopt.h> #include <iostream> #include <sstream> #include <stdlib.h> #include <unistd.h> using namespace std; // global variable for tree operations // use to control tree maintenance operations enum Mode { simple, randomised, avl } mode; // tree type // returns size of tree //...

  • C++ Binary Search Tree question. I heed help with the level 2 question please, as level...

    C++ Binary Search Tree question. I heed help with the level 2 question please, as level 1 is already completed. I will rate the answer a 100% thumbs up. I really appreciate the help!. Thank you! searching.cpp #include <getopt.h> #include <iostream> #include <sstream> #include <stdlib.h> #include <unistd.h> using namespace std; // global variable for tree operations // use to control tree maintenance operations enum Mode { simple, randomised, avl } mode; // tree type // returns size of tree //...

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