2.1 Hashing With Chaining We search/insert/delete in a hashtable in the following way. First use the getHashValue method to get the hash value. Now use this hash value to get hold of a hash table entry, which is a linked list. These functions have been written and you do not need to modify them: • gethashValue: Uses the hash function (37 ∗ val + 61)%T ABLE SIZE. • getList: The hashtable is an array of linked list. So, this method simply returns the linked list at a particular index of the hash table. We will use the inbuilt linked-list of C++ or Java. This is an implementation of a doubly- linked-list (with links going both forward and backward). It supports all the standard operations (inserting at front or end, deleting head or tail, traversing the list, etc.) In the next sections (Java), I’ll highlight some of the usages (not all may be required). Since we will deal with integers, I will only discuss integer linked lists, but lists of any type can be created. 2.3 Java • Syntax to create an integer list: LinkedList nameOfList = new LinkedList(); • To get the size of the list, the syntax is: nameOfList.size(); • To add a number at the end, the syntax is: nameOfList.addLast(15); To add a number at the beginning, the syntax is: nameOfList.addFirst(15); • To remove the last number, the syntax is: nameOfList.removeLast(); To remove the first number, the syntax is: nameOfList.removeFirst(); To traverse the list (say for retrieving a value or searching or printing or deleting), one can use an iterator as shown next. Here, print prints the list and remove removes the node at index. static void print(LinkedList numbers) { // printing the content Iterator it = numbers.iterator(); while (it.hasNext()) System.out.print(it.next() + " "); } static void remove(LinkedList numbers, int index) { // remove node at index if (numbers.size() == 0) // get the size of the list return; // nothing to remove if (index == 0) numbers.removeFirst(); // remove node at index 0 else if (index == numbers.size() - 1) numbers.removeLast(); // remove node at last index else { int i = 0; Iterator it = numbers.iterator(); while (it.hasNext()) {it.next(); if (i == index) { // if i = index, we are at desired node it.remove(); // delete current node return; } i++; } } } • Iterator it = numbers.iterator(); declares an iterator it on the list numbers and the iterator points to the first number on the list. • while (it.hasNext()) ensures that the iterator traverses until the last node. • it.next() retrieves the value of the node at which the iterator is pointing as well as moves the iterator to the next node. Thus System.out.print(it.next()) prints the value of the node at which the iterator is pointing and moves the iterator to the next node. • it.remove() deletes the current node, i.e., the node at index. 2.4 Few things that you should know but not use for this assignment Both in C++ and Java, there are inbuilt functions that allow you to search a list for a particular number or remove an occurrence of a particular number. However, these are riddled with issues or totally necessary for our purposes because of the overhead involved. For example, to remove an number in Java, one has to create an Integer object for the number and then use it as argument to the remove method. In C++, one has to use the find method in the algorithms header to get an iterator to the occurrence of a number, and then remove the iterator. Remember that over here we want to learn the usage of iterator; so, you are prohibited to use in-built methods other than the ones mentioned previously. 2.5 Pseudo-code search • First obtain the hash value for the key using getHashValue function. • Use getList to get the linked list from the hashT able[ ] for this hash value. • Now, iterate through this linked list using an iterator. If the iterator’s value equals key, then the list contains the key; so, return true. • Once the iteration completes, return f alse. insert • Remember that your hash table should contain a number only once. So, first use search to check if the hash table already contains val. If it does, then return f alse. • Obtain the hash value for val using getHashValue function. • Use getList to get the linked list from the hashT able[ ] for this hash value. • Now, insert the value at the end of the linked list, and return true. remove • First obtain the hash value for val using getHashValue function. • Use getList to get the linked list from the hashT able[ ] for this hash value. • Now, iterate through this linked list using an iterator. If the iterator’s value equals val, then the list contains val; so, delete using the iterator and return true. • Once the iteration completes, return false.   What I have for Code so far is in image that is attached.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

2.1 Hashing With Chaining
We search/insert/delete in a hashtable in the following way. First use the getHashValue method
to get the hash value. Now use this hash value to get hold of a hash table entry, which is a linked
list. These functions have been written and you do not need to modify them:
• gethashValue: Uses the hash function (37 ∗ val + 61)%T ABLE SIZE.
• getList: The hashtable is an array of linked list. So, this method simply returns the linked
list at a particular index of the hash table.

We will use the inbuilt linked-list of C++ or Java. This is an implementation of a doubly-
linked-list (with links going both forward and backward). It supports all the standard operations

(inserting at front or end, deleting head or tail, traversing the list, etc.) In the next sections (Java), I’ll highlight some of the usages (not all may be required). Since we will deal with integers,
I will only discuss integer linked lists, but lists of any type can be created.

2.3 Java
• Syntax to create an integer list: LinkedList<Integer> nameOfList = new LinkedList<Integer>();
• To get the size of the list, the syntax is: nameOfList.size();
• To add a number at the end, the syntax is: nameOfList.addLast(15); To add a number at
the beginning, the syntax is: nameOfList.addFirst(15);
• To remove the last number, the syntax is: nameOfList.removeLast(); To remove the first
number, the syntax is: nameOfList.removeFirst();
To traverse the list (say for retrieving a value or searching or printing or deleting), one can use
an iterator as shown next. Here, print prints the list and remove removes the node at index.


static void print(LinkedList<Integer> numbers) { // printing the content
Iterator<Integer> it = numbers.iterator();
while (it.hasNext())
System.out.print(it.next() + " ");
}
static void remove(LinkedList<Integer> numbers,
int index) { // remove node at index
if (numbers.size() == 0) // get the size of the list
return; // nothing to remove
if (index == 0)
numbers.removeFirst(); // remove node at index 0
else if (index == numbers.size() - 1)
numbers.removeLast(); // remove node at last index
else {
int i = 0;
Iterator<Integer> it = numbers.iterator();
while (it.hasNext()) {it.next();
if (i == index) { // if i = index, we are at desired node
it.remove(); // delete current node
return;
}
i++;
}
}
}

• Iterator<Integer> it = numbers.iterator(); declares an iterator it on the list numbers
and the iterator points to the first number on the list.
• while (it.hasNext()) ensures that the iterator traverses until the last node.
• it.next() retrieves the value of the node at which the iterator is pointing as well as moves
the iterator to the next node. Thus System.out.print(it.next()) prints the value of the
node at which the iterator is pointing and moves the iterator to the next node.
• it.remove() deletes the current node, i.e., the node at index.
2.4 Few things that you should know but not use for this assignment
Both in C++ and Java, there are inbuilt functions that allow you to search a list for a particular
number or remove an occurrence of a particular number. However, these are riddled with issues
or totally necessary for our purposes because of the overhead involved. For example, to remove an
number in Java, one has to create an Integer object for the number and then use it as argument to
the remove method. In C++, one has to use the find method in the algorithms header to get an
iterator to the occurrence of a number, and then remove the iterator.
Remember that over here we want to learn the usage of iterator; so, you are prohibited to use
in-built methods other than the ones mentioned previously.
2.5 Pseudo-code
search
• First obtain the hash value for the key using getHashValue function.
• Use getList to get the linked list from the hashT able[ ] for this hash value.
• Now, iterate through this linked list using an iterator.
If the iterator’s value equals key, then the list contains the key; so, return true.
• Once the iteration completes, return f alse.

insert
• Remember that your hash table should contain a number only once. So, first use
search to check if the hash table already contains val. If it does, then return f alse.
• Obtain the hash value for val using getHashValue function.

• Use getList to get the linked list from the hashT able[ ] for this hash value.
• Now, insert the value at the end of the linked list, and return true.


remove
• First obtain the hash value for val using getHashValue function.
• Use getList to get the linked list from the hashT able[ ] for this hash value.
• Now, iterate through this linked list using an iterator. If the iterator’s value equals
val, then the list contains val; so, delete using the iterator and return true.
• Once the iteration completes, return false.

 

What I have for Code so far is in image that is attached.

 

ERE →
1-import java.utii.Iterator;
2 import java.util.LinkedList;
3 public class HashingWithChaining {
Linked List<Integer> hashTable [];
int TABLE_SIZE;
6
10
15
21
22
29
38
41
42
43
TT +
@SuppressWarnings("unchecked")
public HashingWithChaining (int tableSize) {
TABLE_SIZE = tableSize;
private int getHashValue(int val) (
}
hashTable = (LinkedList<Integer>[]) new LinkedList[TABLE_SIZE]; // this ain't a good method but java does not support array of generics;
// should ideally use an array list with space reserved
for (int i = 0; 1 < TABLE_SIZE; 1++)
hashTable i| = new LinkedList<Integer>();
return (37 * val + 61) % TABLE_SIZE;
public LinkedList<Integer> getList(int index) {
return hashTable[index];
DOO
public boolean search(int key) { // complete this method
}
public boolean insert(int val) { // complete this method
}
public boolean remove(int val) { // complete this method
public void printStatistics() {
}
int maxSize - hashTable[0].size();
int minSize = maxSize, total= maxSize;
for (int i = 1; 1 < TABLE_SIZE; 1+-) {
int size = hashTable[1].size();
if (size > maxSize)
maxSize = size;
else if (size < minSize)
minSize = size;
total += size;
System.out.printf(
"Max length of a chain = %d%n" + "Min length of a chain = %d%n" + "Avg length of chains = %d\n",
maxSize, minŠize, total / TABLE_SIZE);
101
Transcribed Image Text:ERE → 1-import java.utii.Iterator; 2 import java.util.LinkedList; 3 public class HashingWithChaining { Linked List<Integer> hashTable []; int TABLE_SIZE; 6 10 15 21 22 29 38 41 42 43 TT + @SuppressWarnings("unchecked") public HashingWithChaining (int tableSize) { TABLE_SIZE = tableSize; private int getHashValue(int val) ( } hashTable = (LinkedList<Integer>[]) new LinkedList[TABLE_SIZE]; // this ain't a good method but java does not support array of generics; // should ideally use an array list with space reserved for (int i = 0; 1 < TABLE_SIZE; 1++) hashTable i| = new LinkedList<Integer>(); return (37 * val + 61) % TABLE_SIZE; public LinkedList<Integer> getList(int index) { return hashTable[index]; DOO public boolean search(int key) { // complete this method } public boolean insert(int val) { // complete this method } public boolean remove(int val) { // complete this method public void printStatistics() { } int maxSize - hashTable[0].size(); int minSize = maxSize, total= maxSize; for (int i = 1; 1 < TABLE_SIZE; 1+-) { int size = hashTable[1].size(); if (size > maxSize) maxSize = size; else if (size < minSize) minSize = size; total += size; System.out.printf( "Max length of a chain = %d%n" + "Min length of a chain = %d%n" + "Avg length of chains = %d\n", maxSize, minŠize, total / TABLE_SIZE); 101
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 5 images

Blurred answer
Knowledge Booster
Hash Table
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education