
package hashset;
import java.util.Iterator;
public class MySet {
// implements a set using a separate chaining hash table
privateclass Node {
private Integer element;
private Node next;
private Node(Integer e, Node n) {
element = e;
next = n;
}
}
private Node table[]; //an array of linked list
privateinttableSize; //current number of lists in the table
privateintnumElements; //number of elements in the set
privatefinalintprimes[] = {7, 23, 59, 131, 271, 563, 1171,
2083, 4441, 8839, 16319, 32467,
65701, 131413, 263983, 528991};
privateintprimeIndex; //last prime used
publicint getSize() {
returntableSize;
}
privateint nextPrime(intp) {
//finds the next prime from the list above
//used for resizing and the initial size
while (primes[primeIndex] <= p)
primeIndex++;
returnprimes[primeIndex];
}
public MySet(ints) {
//s is a hint for the initial size
primeIndex = 0;
tableSize = nextPrime(s);
table = new Node[tableSize];
numElements = 0;
}
//return the hash function value for k
privateint hash(Integer k) {
return Math.abs(k.hashCode() % tableSize);
}
//"double" the table size and reinsert the values stored in the
//current table. the table size should remain prime
privatevoid resize()
{
// Double the table size
intnewSize = nextPrime(tableSize * 2);
Node[] newTable = new Node[newSize];
for (inti = 0; i < tableSize; i++) {
Node current = table[i];
while (current != null) {
Node next = current.next;
intindex = hash(current.element);
current.next = newTable[index];
newTable[index] = current;
current = next;
}
}
table = newTable;
tableSize = newSize;
}
//returns true when e is in the set, otherwise returns false
publicboolean find(Integer e)
{
//Calculate the hash value of the argument.
intindex = hash(e);
Node current = table[index];
// Traverse the linked list to check if the element exists.
while (current != null) {
if (current.element.equals(e)) {
returntrue; // Element found, return true.
}
current = current.next;
}
// If the element is not found, the method returns false.
returnfalse;
}
//if e is not in the set add e to the set otherwise the set does not change
//if after adding the new element numElements > 2*tableSize then call resize
publicvoid addElement(Integer e)
{
// Calculate the hash value for the input element
intindex = hash(e);
// Traverse the linked list at the index corresponding to the hash value
Node current = table[index];
while (current != null) {
// If the element is already in the set, do nothing and return
if (current.element.equals(e)) {
return;
}
current = current.next;
}
// If the element is not already in the set, add it to the linked list at the appropriate index
Node newNode = new Node(e, table[index]);
table[index] = newNode;
numElements++;
// If the number of elements in the set is greater than twice the table size, resize the table
if (numElements > 2 * tableSize) {
resize();
}
}
//returns a string representation for the set
//the string representation of the set is { followed by a comma delimiter list of set
//elements followed by a }. The string for the empty set is {}
//For example, {1,2,3}.
//Note that you SHOULD NOT have any spaces in your String
/*
* Example:
* table[0]: 2 -> 4
* table[1]: 1 -> 3
*
* The string representation of this set is {2,4,1,3}
*/
//toString method
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("{");
MySetIterator iter = new MySetIterator();
while (iter.hasNext()) {
sb.append(iter.next());
if (iter.hasNext()) {
sb.append(",");
}
}
sb.append("}");
returnsb.toString();
}
publicclass MySetIterator implements Iterator<Integer> {
//implements an iterator for the set
privateintcurrentList;
private Node currentNode;
//helper method that finds the next non empty bucket
privatevoid nextList() {
while (currentList < tableSize && table[currentList] == null) {
currentList++;
}
currentNode = (currentList < tableSize) ? table[currentList] : null;
}
public MySetIterator() {
currentList = 0;
nextList();
}
//returns true if the iteration has more elements.
publicboolean hasNext() {
returncurrentNode != null;
}
//Returns the next element in the iteration.
public Integer next() {
Integer rVal = currentNode.element;
if (currentNode.next != null) {
//what should the currentNode be?
currentNode = currentNode.next;
}
else {
//No more elements in the current bucket
//I need to get the next bucket
currentList++;
nextList();
}
returnrVal;
}
}
}
Here Is the Question below:
Consider the lines of code below
MySet test = new MySet(6); for(int i = 0;i<=14;i++) test.addElement(i); a) Draw the hashtable right before the resize method is triggered b) Draw the hashtable after the for loop completes. Note: In the above code, I do have tableSize = nextPrime(s) |

Step by stepSolved in 3 steps

- For the Queue use the java interface Queue with the ArrayDeque classFor the Stack use the java Deque interface with the ArrayDeque classFor the LinkedList, use the java LinkedList class Also, when you are asked to create and use a Queue, you can use only those methods pertaining to the general operations of a queue (Enqueue: addLast, Dequeue: removeFirst, and peek() Similarly, when you are asked to create and/or use a stack, you can use only those methods pertaining to the general operations of a Stack (push: addFirst, pop:removeFirst, and peek() ) Your code should not only do the job, but also it should be done as efficiently as possible: fast and uses no additional memory if at all possible Questions Write a method “int GetSecondMax(int[] array)” . this method takes an array of integers and returns the second max value Example: if the array contains: 7, 2, 9, 5, 4, 15, 6, 1}. It should return the value: 9arrow_forwardimport java.util.HashSet; import java.util.Set; // Define a class named LinearSearchSet public class LinearSearchSet { // Define a method named linearSearch that takes in a Set and an integer target // as parameters public static boolean linearSearch(Set<Integer> set, int target) { // Iterate over all elements in the Set for () { // Check if the current value is equal to the target if () { // If so, return true } } // If the target was not found, return false } // Define the main method public static void main(String[] args) { // Create a HashSet of integers and populate integer values Set<Integer> numbers = new HashSet<>(); // Define the target to search for numbers.add(3); numbers.add(6); numbers.add(2); numbers.add(9); numbers.add(11); // Call the linearSearch method with the set…arrow_forwardImplement a Single linked list to store a set of Integer numbers (no duplicate) • Instance variable• Constructor• Accessor and Update methods 2.) Define SLinkedList Classa. Instance Variables: # Node head # Node tail # int sizeb. Constructorc. Methods # int getSize() //Return the number of nodes of the list. # boolean isEmpty() //Return true if the list is empty, and false otherwise. # int getFirst() //Return the value of the first node of the list. # int getLast()/ /Return the value of the Last node of the list. # Node getHead()/ /Return the head # setHead(Node h)//Set the head # Node getTail()/ /Return the tail # setTail(Node t)//Set the tail # addFirst(E e) //add a new element to the front of the list # addLast(E e) // add new element to the end of the list # E removeFirst()//Return the value of the first node of the list # display()/ /print out values of all the nodes of the list # Node search(E key)//check if a given…arrow_forward
- 7. Given a Queue Q, write a non-generic method that finds the maximum element in the Queue. You may use only queue operations. No other data structure can be used other than queues. The Queue must remain intact after finding the maximum value. Assume the class that has the findMax method implements Comparable. The header of the findMax method: public Comparable findMax(Queue q)arrow_forwardIn Java. The following is a class definition of a linked list Node:class Node{int info;Node next;}Show the instructions required to create a linked list that is referenced by head and stores in order, the int values 13, 6 and 2. Assume that Node's constructor receives no parameters.arrow_forwardclass Queue { private static int front, rear, capacity; private static int queue[]; Queue(int c) { front = rear = 0; capacity = c; queue = new int[capacity]; } static void queueEnqueue(int data) { if (capacity == rear) { System.out.printf("\nQueue is full\n"); return; } else { queue[rear] = data; rear++; } return; } static void queueDequeue() { if (front == rear) { System.out.printf("\nQueue is empty\n"); return; } else { for (int i = 0; i < rear - 1; i++) { queue[i] = queue[i + 1]; } if (rear < capacity) queue[rear] = 0; rear--; } return; } static void queueDisplay() { int i; if (front == rear) { System.out.printf("\nQueue is Empty\n"); return; } for (i = front; i < rear; i++) { System.out.printf(" %d <-- ", queue[i]); } return; } static void queueFront() { if (front == rear) { System.out.printf("\nQueue is Empty\n"); return; } System.out.printf("\nFront Element is: %d", queue[front]);…arrow_forward
- Java programming language I have to create a remove method that removes the element at an index (ind) or space in an array and returns it. Thanks! I have to write the remove method in the code below. i attached the part where i need to write it. public class ourArrayList<T>{ private Node<T> Head = null; private Node<T> Tail = null; private int size = 0; //default constructor public ourArrayList() { Head = Tail = null; size = 0; } public int size() { return size; } public boolean isEmpty() { return (size == 0); } //implement the method add, that adds to the back of the list public void add(T e) { //HW TODO and TEST //create a node and assign e to the data of that node. Node<T> N = new Node<T>();//N.mData is null and N.next is null as well N.setsData(e); //chain the new node to the list //check if the list is empty, then deal with the special case if(this.isEmpty()) { //head and tail refer to N this.Head = this.Tail = N; size++; //return we are done.…arrow_forwardpublic class HeapPriQ<T> implements PriQueueInterface<T>{ protected ArrayList<T> elements; // priority queue elements protected int lastIndex; // index of last element in priority queue protected int maxIndex; // index of last position in ArrayList protected Comparator<T> comp; public HeapPriQ(int maxSize) // Precondition: T implements Comparable { elements = new ArrayList<T>(maxSize); lastIndex = -1; maxIndex = maxSize - 1; comp = new Comparator<T>() { public int compare(T element1, T element2) { return ((Comparable)element1).compareTo(element2); } }; } public HeapPriQ(int maxSize, Comparator<T> comp) // Precondition: T implements Comparable { elements = new ArrayList<T>(maxSize); lastIndex = -1; maxIndex = maxSize - 1; this.comp = comp; } public boolean isEmpty() // Returns true if this priority queue is empty; otherwise, returns false. {…arrow_forwardBelow you're given a Node class and a LinkedList class. You will implement a method for the LinkedList class named "delete48in148". That is, whenever there is a sequence of nodes with values 1, 4, 8, we delete the 4 and 8. For exCample, Before: 1 -> 4 -> 8 LAfter: 1 Before: 7 -> 1 -> 4 -> 8 -> 9 -> 4 -> 8 After: 7 -> 1 -> 9 -> 4 -> 8 Before: 7 -> 1 -> 4 -> 8 -> 4 -> 8 -> 4 -> 8 -> 9 After: 7 -> 1 -> 9 Note from the above example that, after deleting one instance of 48, there may be new instances of 148 formed. You must delete ALL of them. Requirement: Your implementation must be ITERATIVE (i.e., using a loop). You must NOT use recursion. Recursive solutions will NOT be given marks. import ... # No other import is allowedarrow_forward
- The queue operation that is required when using an array implementation, but is not required when using a linked list implementation, is ___. isFull() isEmpty() peek() dequeue()arrow_forwardAdd the following method in the BST class that returns aniterator for traversing the elements in a BST in preorder./** Return an iterator for traversing the elements in preorder */java.util.Iterator<E> preorderIterator()arrow_forwardpackage circularlinkedlist;import java.util.Iterator; public class CircularLinkedList<E> implements Iterable<E> { // Your variablesNode<E> head;Node<E> tail;int size; // BE SURE TO KEEP TRACK OF THE SIZE // implement this constructorpublic CircularLinkedList() {} // I highly recommend using this helper method// Return Node<E> found at the specified index// be sure to handle out of bounds casesprivate Node<E> getNode(int index ) { return null;} // attach a node to the end of the listpublic boolean add(E item) {this.add(size,item);return false; } // Cases to handle// out of bounds// adding to empty list// adding to front// adding to "end"// adding anywhere else// REMEMBER TO INCREMENT THE SIZEpublic void add(int index, E item){ } // remove must handle the following cases// out of bounds// removing the only thing in the list// removing the first thing in the list (need to adjust the last thing in the list to point to the beginning)// removing the last…arrow_forward
- Computer Networking: A Top-Down Approach (7th Edi...Computer EngineeringISBN:9780133594140Author:James Kurose, Keith RossPublisher:PEARSONComputer Organization and Design MIPS Edition, Fi...Computer EngineeringISBN:9780124077263Author:David A. Patterson, John L. HennessyPublisher:Elsevier ScienceNetwork+ Guide to Networks (MindTap Course List)Computer EngineeringISBN:9781337569330Author:Jill West, Tamara Dean, Jean AndrewsPublisher:Cengage Learning
- Concepts of Database ManagementComputer EngineeringISBN:9781337093422Author:Joy L. Starks, Philip J. Pratt, Mary Z. LastPublisher:Cengage LearningPrelude to ProgrammingComputer EngineeringISBN:9780133750423Author:VENIT, StewartPublisher:Pearson EducationSc Business Data Communications and Networking, T...Computer EngineeringISBN:9781119368830Author:FITZGERALDPublisher:WILEY





