Question

1) Using Java. Insert the key sequence [29, 33, 1, 37, 32, 26, 48, 11 ,...

1) Using Java. Insert the key sequence [29, 33, 1, 37, 32, 26, 48, 11 , 40, 17, 36, 12, 41, 25, 30, 23, 28, 39, 6, 43] with hashing by chaining in a hash table with size 17. Use the hash function: h(k) = k mod 17.

a) Show the final table.

b) Indicate at which insertion the first collision occurred.

c) Indicate which index that has the longest chain.

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

1)following code impliments the hashing by chaining in java.

public class LinkedHashEntry {
private int key;
private int value;
private LinkedHashEntry next;

LinkedHashEntry(int key, int value) {
this.key = key;
this.value = value;
this.next = null;
}

public int getValue() {
return value;
}

public void setValue(int value) {
this.value = value;
}

public int getKey() {
return key;
}

public LinkedHashEntry getNext() {
return next;
}

public void setNext(LinkedHashEntry next) {
this.next = next;
}
}
public class HashMap {
private final static int TABLE_SIZE = 128;

LinkedHashEntry[] table;

HashMap() {
table = new LinkedHashEntry[TABLE_SIZE];
for (int i = 0; i < TABLE_SIZE; i++)
table[i] = null;
}

public int get(int key) {
int hash = (key % TABLE_SIZE);
if (table[hash] == null)
return -1;
else {
LinkedHashEntry entry = table[hash];
while (entry != null && entry.getKey() != key)
entry = entry.getNext();
if (entry == null)
return -1;
else
return entry.getValue();
}
}

public void put(int key, int value) {
int hash = (key % TABLE_SIZE);
if (table[hash] == null)
table[hash] = new LinkedHashEntry(key, value);
else {
LinkedHashEntry entry = table[hash];
while (entry.getNext() != null && entry.getKey() != key)
entry = entry.getNext();
if (entry.getKey() == key)
entry.setValue(value);
else
entry.setNext(new LinkedHashEntry(key, value));
}
}

public void remove(int key) {
int hash = (key % TABLE_SIZE);
if (table[hash] != null) {
LinkedHashEntry prevEntry = null;
LinkedHashEntry entry = table[hash];
while (entry.getNext() != null && entry.getKey() != key) {
prevEntry = entry;
entry = entry.getNext();
}
if (entry.getKey() == key) {
if (prevEntry == null)
table[hash] = entry.getNext();
else
prevEntry.setNext(entry.getNext());
}
}
}
}

following image explains the final hash table and collision chain that occurs.

here have even choose the 17 as the table size since it is the prime number.

Add a comment
Know the answer?
Add Answer to:
1) Using Java. Insert the key sequence [29, 33, 1, 37, 32, 26, 48, 11 ,...
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
  • Please use Java, thank you! 5. Hashing 1) Insert the keys E X A M Q U S T I O N in that order into an initially empty ta...

    Please use Java, thank you! 5. Hashing 1) Insert the keys E X A M Q U S T I O N in that order into an initially empty table of M = 5 lists, using separate chaining. Use the hash function 11 k % M to transform the kth letter of the alphabet into a table index. Show the hash table after each insertion. hown in the following table Use A-1, B 2,. as 20 21 22 23 24...

  • 1. Using closed hashing with double hashing to resolve collisions insert the following keys into a...

    1. Using closed hashing with double hashing to resolve collisions insert the following keys into a table with 11 slots, numbered 0 through 10. The hash functions to be used are H1(k)k(mod11) and H2(k)- Rev(k + 1) (mod11) The function REV reverses the decimal digits like Rev(376) 673. Show the hash table after all nine keys have been inserted. Be sure to indicate how H1 and H2 are used. Keys: 4, 3, 2, 8, 33, 17, 24, 35, 46

  • Insert elements into a hash table implemented using chain hashing, as an array of linked list...

    Insert elements into a hash table implemented using chain hashing, as an array of linked list in which each entry slot is as a linked list of key/value pairings that have the same hash (outcome value computed using certain hash function). You are allowed to use “Hash Function”, h(k) = k % x where, x is the value you will need to decide to use, that you find appropriate for this implementation. The main requirements are the following: 1. Input:...

  • 1 Overview For this assignment you are required to write a Java program that plays (n,...

    1 Overview For this assignment you are required to write a Java program that plays (n, k)-tic-tac-toe; (n, k)-tic- tac-toe is played on a board of size n x n and to win the game a player needs to put k symbols on adjacent positions of the same row, column, or diagonal. The program will play against a human opponent. You will be given code for displaying the gameboard on the screen. 2 The Algorithm for Playing (n, k)-Tic-Tac-Toe The...

  • Java programing Write a program that deals a deck of card into 4 hands – Each...

    Java programing Write a program that deals a deck of card into 4 hands – Each hand having 13 cards. The program should be doing the followings use an array to randomly select 52 cards and deal it into 4 hands. Print each hand unsorted Identify the face value of each hand and display it. You should create a class called Card, and all the methods should be defined within the card class. Hints – Import below java utility to...

  • JAVA Submit:    HashSet.java    with the following methods added: HashSet.java // Implements a set of...

    JAVA Submit:    HashSet.java    with the following methods added: HashSet.java // Implements a set of objects using a hash table. // The hash table uses separate chaining to resolve collisions. // Original from buildingjavaprograms.com supplements public class HashSet<E> { private static final double MAX_LOAD_FACTOR = 0.75; private HashEntry<E>[] elementData; private int size; // Constructs an empty set. @SuppressWarnings("unchecked") public HashSet() { elementData = new HashEntry[10]; size = 0; } // ADD METHODS HERE for exercise solutions: // Adds the...

  • 1. Given the following physical addresses and value in memory: add 0 val 9 10 11 12 13 1415161181...

    1. Given the following physical addresses and value in memory: add 0 val 9 10 11 12 13 14151611819 2021 22 23 18 24 20 32 0 40 8 32 245458 10 36 34 3230 40 35 3028 add 24 25 26 27 28 29 30 31 32 33 34 | 35 | 36 37 38 39 40 | 41 | 42 | 43 44 45 46 47 8 40 35 1614 12 12 22 24417 21 23 25 27...

  • Game   Point_Differential   Assists   Rebounds   Turnovers   Personal_Fouls 1   15   15   38   11   9 2   36   20   43 &

    Game   Point_Differential   Assists   Rebounds   Turnovers   Personal_Fouls 1   15   15   38   11   9 2   36   20   43   8   13 3   16   21   29   7   13 4   45   22   46   11   11 5   12   11   40   7   22 6   -10   10   31   13   26 7   11   19   45   11   7 8   12   16   32   16   14 9   3   16   27   18   15 10   19   9   34   17   17 11   40   16   41   9   17 12   44   12   29   9   22 13   16  ...

  • Q Search Sheet 9+ Share Σ AutoSum , 49 Q H Fill .00 0 Insert Delete...

    Q Search Sheet 9+ Share Σ AutoSum , 49 Q H Fill .00 0 Insert Delete Format .00 Conditional Format Formatting as Table Cell Styles Clear Sort & Filter Find & Select Home Insert Draw Page Layout Formulas Data Review View X Cut Calibri (Body) 11 A- A+ Wrap Text General Copy Paste Β Ι Ο А = + Format Merge & Center $ B6 4 X v fx The following is a list of transactions for 2020 for Amazing...

  • Copy the following java codes and compile //// HighArray.java //// HighArrayApp.java Study carefully the design and...

    Copy the following java codes and compile //// HighArray.java //// HighArrayApp.java Study carefully the design and implementation HighArray class and note the attributes and its methods.    Create findAll method which uses linear search algorithm to return all number of occurrences of specified element. /** * find an element from array and returns all number of occurrences of the specified element, returns 0 if the element does not exit. * * @param foundElement   Element to be found */ int findAll(int...

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