Question

(in Java) We wish to insert the following strings into a hash table: BEN AL SUE...

(in Java) We wish to insert the following strings into a hash table: BEN AL SUE CHUCK KAREN MIKE SAM MIKE ALICE TARA TIM BOB Let's make the hash table size p=19 ( 19 is a prime ). Use letter encodings where A maps to 1, B maps to 2, etc and strings map to powers of 26. For example, "ABC" would map to 1*(26^2) + 2*(26^1)+ 3*(26^0) mod 19 (you might find the web site Wolfram Alpha helpful. Just type in the expression as shown here).

a) show the hash tanle using linear probing

b) show the hash table using quadratic probing

c) show the hash table using a rehashing function. The rehashing function should be stepSize = 7 - (f(s) mod 7)

d) which of the three resolution techniques worked the best? Support your answer.

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

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {
  
static int linear_probing(int ar[], int n, int p){
int count = 0;
String hash[] = new String[p];
  
for(int i=0;i<p;i++){
hash[i] = -1;
}
  
for(int i=0;i<n;i++){
long int hash_value = 0;
for(int j=0;j<ar[i].length();j++){
hash_value += (ar[i].charAt(j) - 64) * Math.pow(26, j+1);
}
int index = hash_value % p;
if(hash[index] == -1){
hash[index] = ar[i];
}else{
count++;
while(hash[index] != -1){
index = (index + 1) % p;
}
hash[index] = ar[i];
}
}
return count;
}
  
static int quadratic_probing(int ar[], int n, int p){
int count = 0;
String hash[] = new String[p];
  
for(int i=0;i<p;i++){
hash[i] = -1;
}
  
for(int i=0;i<n;i++){
long int hash_value = 0;
for(int j=0;j<ar[i].length();j++){
hash_value += (ar[i].charAt(j) - 64) * Math.pow(26, j+1);
}
int index = hash_value % p;
if(hash[index] == -1){
hash[index] = ar[i];
}else{
count++;
int k = 1;
while(hash[index] != -1){
index = (index + power(k, 2)) % p;
k++;
}
hash[index] = ar[i];
}
}
return count;
}
  
static int rehashing(int ar[], int n, int p){
int count = 0;
String hash[] = new String[p];
  
for(int i=0;i<p;i++){
hash[i] = -1;
}
  
for(int i=0;i<n;i++){
long int hash_value = 0;
for(int j=0;j<ar[i].length();j++){
hash_value += (ar[i].charAt(j) - 64) * Math.pow(26, j+1);
}
int index = hash_value % p;
if(hash[index] == -1){
hash[index] = ar[i];
}else{
count++;
while(hash[index] != -1){
index = (index + (7 - (hash_value % 7)) % p;
}
hash[index] = ar[i];
}
}
return count;
}

public static void main(String[] args) {
int n;
Scanner s = new Scanner(System.in);
n = s.netInt();
String ar = new String[n];
for(int i=0;i<n;i++){
ar[i] = s.next();
}
int p;
s = p.nextint();
int linear_collisions = linear_probing(ar, n, p);
int quadratic_collisions = quadratic_collisions(ar, n, p);
int rehashing_collisions = rehashing(ar, n, p);
}
}

When we run the algorithm, we can evidently see that the number of collision in linaer probing is greater than the number of collisions in quadratic probing is greater than the number of collisions in rehashing. Hence, rehashing is a better algorithm when compared to other algorithms.

Add a comment
Know the answer?
Add Answer to:
(in Java) We wish to insert the following strings into a hash table: BEN AL SUE...
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
  • ^b Given input( 66, 28, 43, 29, 44, 69, 19) and a hash function h(x) =...

    ^b Given input( 66, 28, 43, 29, 44, 69, 19) and a hash function h(x) = x mod 10, show the resulting hash table 1) Using Separate Chaining 2) Using Linear Probing 3) Using Quadratic Probing 4) Starting with the following hash function: h2(x) 7- (x mod 7), applv Rehash ary course slides ing as described in the prim Rehashing Increases the size of the hash table when load factor becomes "too high" (defined by a cutoff) - Anticipating that...

  • Suppose we are inserting strings into a hash table of size 9. Suppose we have two...

    Suppose we are inserting strings into a hash table of size 9. Suppose we have two hash functions, h, and h2. The hash values for certain strings of these functions are shown in the table below: Fill in the hash table below assuming that we are using open-address, linear-probing style hashing, given that the table starts as it appears below, the hash function is h_1 and the order of insertion is "Fred", "Chloe", "Adam", "Rebecca" and "Reggie". Fill in the...

  • please use Java!! We are storing the inventory for our fruit stand in a hash table....

    please use Java!! We are storing the inventory for our fruit stand in a hash table. The attached file shows all the possible key/fruit combinations that can be inserted into your hash table. These are the real price lookup values for the corresponding fruit. Problem Description In this programming assignment, you will implement a hash table class fruitHash. Your hash table must include the following methods: 1) hashFunction(key) This method takes a key as an argument and returns the address...

  • Show how the following data would be stored in a hash table of size 7 (the...

    Show how the following data would be stored in a hash table of size 7 (the size is fixed) using quadratic probing. If any insertion fails, indicate that, but keep trying to insert all the remaining data. The hash function is hash(x) = x mod 7. Here is the data to be stored: 17, 10, 31, 45, 3, 53, 14. Insert the data in the order given. Data: Index: 0 1 2 3 4 5 6 4 data: Index: 31...

  • 4. Hashing and Hash Tables. You need to use the ASCII table in the last page for this question. Study the following hash functions for ASCII C strings that are at least 3-char long unsigned hash1(con...

    4. Hashing and Hash Tables. You need to use the ASCII table in the last page for this question. Study the following hash functions for ASCII C strings that are at least 3-char long unsigned hash1(const char, unsigned unsigned vto]+01997 return (v % m); unsigned hash2Cconst char unsigned) unsigned v-o]k(2] 877 return 1 + (v % ( -1)); (a) Given that m-, 7, compute the hash values and fill the following table (3%) String k hash1k, ) hash2(k, 7) aph...

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