Key - Practice Questions - Exam 2 (2)
.docx
keyboard_arrow_up
School
Arizona State University *
*We aren’t endorsed by this school
Course
310
Subject
Computer Science
Date
May 26, 2024
Type
docx
Pages
5
Uploaded by ColonelDanger14164
CPSC2108- Data Structures Practice Sheet
1.
Write a method that takes an array of keys and an array of values and creates a corresponding Hashmap.
public HashMap makeMap(E[] keys, E[] values)
{
HashMap map = new HashMap();
for(int i = 0; i < keys.size(); i++)
{
map.put(keys[i], values[i]);
}
return map;
}
2. A hash table has buckets 0 to 9 and uses a hash function of key % 10. If the table is initially empty and the following inserts are applied in the order shown, the insert of which item results in a collision? HashInsert(hashTable, item 55)
HashInsert(hashTable, item 90)
HashInsert(hashTable, item 95)
First and third 3.
Knowing that hash table size is 10 and knowledge of the expected keys is 10, 20, 30, 40,…. How do you think the hash function key % 10 will perform (well or poor)? Why?
Poor. High collision rate. 4. Suppose that you would like to create an instance of a new Map that has an iteration order that
is the same as the iteration order of an existing instance of a Map. Which concrete implementation of the Map interface should be used for the new instance?
A.
TreeMap
B.
HashMap
C.
LinkedHashMap
D.
The answer depends on the implementation of the existing instance.
Option C Explanation: The iteration order of a Collection is the order in which an iterator moves through the elements of the Collection.
5. We have a hash table of size 7 to store integer keys, with linear probing and a hash function
h(x) = x mod 7 (x mod 7 return the remainder of the integer division with 7). Show the content of the hashtable after inserting the keys 0,11,3,7,1,9 in the given order.
6. Mark all properties that are TRUE for a hashtable with n elements? (a) an ideal hash table using array doubling has worst-case time complexity of O(1) for every insert operation (b) an ideal hash table using array doubling has average-case time complexity of O(1) for lookups
(c) can be used to sort an array of n real numbers with average-case time complexity O(n) (d) it is possible to have different keys being hashed to the same position in the array (b) and (d) are true
8. Indicate the complexity for all methods that is not marked x (Big O) 4.
Method Definition
5.
LinkedList <E>
6.
ArrayList <E>
7.
CircularQueues
<E>
8.
LinkedStack<E
>
9.
get(int index)
O (n)
O(n)
n/a
n/a
add (int data)
O(1)
O(n)
n/a
n/a
remove ()
O(1)
O(1)
O(1)
O(1)
peek()
x
x
O(1)
O(1)
9.
Suppose a single-linked list contains three Nodes with data “him”, “her”, and “it” and head references the first element. What is the effect of the following fragment (use drawing to illustrate your answer): Node<String> nodeRef = tail; nodeRef.data = ”she”;
10.
Would we use a stack or a queue to solve a postfix expression? Explain how would you use your chosen data structure to evaluate a postfix expression.
Stack. Check the book for explanation. 11.
Pick the queue implementation (circular array, single-linked list, double-linked list) that is most appropriate for each of the following conditions. This implementation is normally most efficient in use of storage. ( single linke dlist
)
This is an existing class in the Java API. ( double linked list
)
Storage must be reallocated when the queue is full. ( circuler queue
)
12. Consider the stack below to indicate the result of each operation and show the new stack if it is changed.
names.push("Jane");
0
5
4
3
2
1
0
1
11 9
3
7
0
tail
head
she
her
him
String top = names.pop(); String nextTop = names.peek(); names.push("Alie");
Jane Phillip Alie
Phillip Dustin Phillip
Dustin Robin Dustin
Robin Bebbie Robin Bebbie Rich Bebbie
Rich Rich
13.
Assume that a linked queue q of capacity 5 contains the five characters +, *, -, & and # (all wrapped in Character objects), where + is the first character inserted. Assume that + is stored in the first position in the queue. What is the value of head.data? What is the value of tail.data? head.data= +
tail.data = #
14.
Remove the first element from the queue in the above question and insert the characters \ then %. Draw the new linked queue. What is the value of head.data? What is the value of tail.data? head.data = *
tail.data = %
15.
Determine the order of magnitude (big-O) for an algorithm whose running time is given by the equation T(n)= 3n
4
– 2n
4
log n + 100n + 37
O(
n
4
)
16.
Write a main function that creates two queues of Integer objects and a stack of Integer objects. Store the numbers -1, 15, 23, 44, 4, 99 in an array. Read the numbers from the array and store them in the first queue. The tail of the queue should store 99. Write a loop to get each number from the first queue and store it
in the second queue and the stack. Use stack and queue operations to implement this 17.
Implement the pop method for the ArryStack ADT 18.
Implement the enqueue method for the LinkedQueue ADT
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
Related Questions
Exercise 3 - Simple hash table
Develop a simple hashtable with specified size (parameter) that accepts key-value
pairs and stores them in an internal structure.
●
• The key has to be a string
●
●
Use your hash function from exercise 2
• The value can be an object
Demonstrate how your hashtable works with multiple inputs
Implement add (key, value), get(key), and print () methods
arrow_forward
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…
arrow_forward
C++
Design a HashMap without using any built-in hash table libraries.
Implement the MyHashMap class:
MyHashMap() initializes the object with an empty map.
void put(int key, int value) inserts a (key, value) pair into the HashMap. If the key already exists in the map, update the corresponding value.
int get(int key) returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key.
void remove(key) removes the key and its corresponding value if the map contains the mapping for the key.
Example 1:
Input ["MyHashMap", "put", "put", "get", "get", "put", "get", "remove", "get"] [[], [1, 1], [2, 2], [1], [3], [2, 1], [2], [2], [2]]
Output [null, null, null, 1, -1, null, 1, null, -1]
arrow_forward
This is the code below:
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
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…
arrow_forward
This is the code below:
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
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…
arrow_forward
This is the code below:
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
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…
arrow_forward
This is the code below:
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
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…
arrow_forward
This is the code below:
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
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…
arrow_forward
This is the code below:
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
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…
arrow_forward
This is the code below:
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
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…
arrow_forward
import 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_forward
HashTable Data Type: By having each bucket contain a linked list of elements that are hashed to that bucket. Usage: >>> table = SeparateChainingHashTable() # Create a new, empty map. >>> table.put('hello', 'world') # Add a new key-value pair. >>> len(table) # Return the number of key-value pairs stored in the map. 1 >>> table.get('hello') # Get value by key. 'world' >>> del table['hello'] # Equivalent to `table.del_('hello')`, deleting key-value pair. >>> table.get('hello') is None # Return `None` if a key doesn't exist. True """ _empty = None
def __init__(self, size=11): self.size = size self._len = 0 self._table = [self._empty] * size
def put(self, key, value): hash_ = self.hash(key) node_ = self._table[hash_] if node_ is self._empty: self._table[hash_] = Node(key, value) else: while node_.next is not None:…
arrow_forward
Class HashTable:
Implement a hash table to store integers (including negative ones). stored in the table int[] data.
Use the hash function: h(x) = (x · 701) mod 2000.
The table size is 2000.
Ensure non-negative indices between 0 and 1999.
Implement the following methods:
insert(int key): Inserts the integer into the table. Returns true if successful, false if the element is already in the table.
search(int key): Searches for the integer in the table. Returns true if found, false otherwise.
delete(int key): Deletes the integer from the table. Returns true if successful, false otherwise.
Class HashTable2:
Implement a second hash table using a different hash function and collision resolution strategy.
Keys are integers (including negative ones).
Use the hash function: ℎ(�)=(�⋅53)mod 100h(x)=(x⋅53)mod100.
The table size is 100.
Ensure non-negative indices between 0 and 99.
Implement the following methods:
insert(int key): Inserts the integer into the table. Returns true if…
arrow_forward
###__write the proper code in C language.__##.
arrow_forward
Java
Given main() in the ShoppingList class, define an insertAtEnd() method in the ItemNode class that adds an element to the end of a linked list. DO NOT print the dummy head node.
Ex. if the input is:
4
Kale
Lettuce
Carrots
Peanuts
where 4 is the number of items to be inserted; Kale, Lettuce, Carrots, Peanuts are the names of the items to be added at the end of the list.
The output is:
Kale
Lettuce
Carrots
Peanuts
Second image is ItemNode
arrow_forward
Computer Science
java program
arrow_forward
BooksMan
import java.util.HashMap;
import java.util.ArrayList;
class Book{
String title, ISBN;
ArrayList authors;
public Book (String title, String ISBN, String authors){
this.title = title;
this.ISBN = ISBN;
this.authors = new ArrayList();
if (authors != null) {
String [] authorArray = authors.split(", ");
for (int i = 0; i < authorArray.length; i++) {
this.authors.add(authorArray[i]);
}
}
}
public String getISBN() {
return ISBN;
}
public String getTitle() {
return title;
}
public ArrayList getAuthors() {
return authors;
}
}
public class Main {
public static HashMap buildMap(Book[] s) {
HashMap books = new HashMap();
if (s != null) {
// TODO Write the statements here: to manage each book from the parameter array reference to the hashmap
}
return books;
}
public static void main(String argv[]) {
Book[] bookArray = new Book[4];
for (int i = 0; i < 4; i++) {…
arrow_forward
Java
arrow_forward
In java, how do I implement the following methods into a linkedlist class.
1) removeLast() – remove the last number of the list
2) removeLeast()- remove least number in the list
3) removeMax()- remove largest number in the list
4) removeFirst() – remove the first number of the list
5) addNum()- add another number to the list
arrow_forward
Remove dublicates.
import java.util.ArrayList;import java.util.HashMap;
/*remove duplicates from the array and return the unique values in A ArrayList,always nmaintain the order*/public class RemoveDuplicates { public static ArrayList<Integer> removeDuplicates(int arr[]) { ArrayList<Integer> output = new ArrayList<>(); HashMap<Integer, Boolean> seen = new HashMap<>(); for (Integer element : arr) { if (seen.containsKey(element)) { continue; } output.add(element); seen.put(element, true); } return output; }
// Driver code to check our function public static void main(String[] args) { int arr[] = {1,3,1,4,5,100000, 200, 5,100000, 4}; ArrayList<Integer> output = removeDuplicates(arr); for(int i=0;i<output.size();i++){ System.out.print(output.get(i)+" "); //1 3 4 5 100000 200 is the output, the order of the array…
arrow_forward
Java Objects and Linked Data: Select all of the following statements that are true.
The Java Class Library's class ArrayList implements the LinkedList interface.
In Java, when one reference variable is assigned to another reference variable,
both references then refer to the same object.
In Java, the "equals"-method that any object inherits can be overwritten to
compare objects field by field.
arrow_forward
Hashcode may implement from the Algorithm Robert Sedgwick book.
arrow_forward
What is the contents of the variable `map` after the following operations.
1
import java.util.HashMap;
2
import java.util.Map;
public class MapTest {
4
public static void main(String[] args) {
Map map = new HashMap);
map.put(1, "A");
map.put(4, "B");
map.put(3, "C");
map.put(2, "D");
map.put(5, null);
5
8
10
11
12
13
}
14
15
}
O {1=A, 2=D, 3=C, 4=B}
Compilation Error
(А, В, С, D}
{1=A, 2=D, 3=C, 4=B, 5=null}
arrow_forward
Java Programming: There are lots of errors in this code. Please help me fix them. Attached is images of what the code must have and circled the errors in the code.
Interpreter.java
public class Interpreter { private HashMap<String, Object> variables;
public void interpretFunction(FunctionNode fn) { // Create a hash map for local variables variables = new HashMap<String, Object>();
for(Node constantNode : fn.getConstants()) { Object constant = constantNode.getValue(); variables.put(constantNode.getName(), constant); } for(Node localVarNode : fn.getVariables()) { Object localVar = null; variables.put(localVarNode.getName(), localVar); } interpretBlock(fn.getStatements(), variables); }
public void interpretBlock(List<StatementNode> statementNodes, HashMap<String, Object> variables) { for(StatementNode statement : statementNodes) {…
arrow_forward
please fix code to match "enter patients name in lbs"
thank you
import java.util.LinkedList;import java.util.Queue;import java.util.Scanner;
interface Patient {
public String displayBMICategory(double bmi);
public String displayInsuranceCategory(double bmi);}
public class BMI implements Patient {
public static void main(String[] args) {
/* * local variable for saving the patient information */ double weight = 0; String birthDate = "", name = ""; int height = 0; Scanner scan = new Scanner(System.in); String cont = ""; Queue<String> patients = new LinkedList<>();
// do while loop for keep running program till user not entered q do {
System.out.print("Press Y for continue (Press q for exit!) "); cont = scan.nextLine(); if (cont.equalsIgnoreCase("q")) {
System.out.println("Thank you for using BMI calculator"); break;…
arrow_forward
Explain code and complete.
//the third and final class which implements Hashmapspublic class OurMapUse { public static void main(String[] args) { Map<String, Integer> map = new Map<>(); for(int i=0;i<20;i++) { map.insert("abc"+i+1+i); System.out.print("i = "+i+"lf = "+map.loadFactor()); } } }.
arrow_forward
Starter code for ShoppingList.java
import java.util.*;import java.util.LinkedList;
public class ShoppingList{ public static void main(String[] args) { Scanner scnr=new Scanner(System.in); LinkedList<ListItem>shoppingList=new LinkedList<ListItem>();//declare LinkedList String item; int i=0,n=0;//declare variables item=scnr.nextLine();//get input from user while(item.equals("-1")!=true)//get inputuntil user not enter -1 { shoppingList.add(new ListItem(item));//add into shoppingList LinkedList n++;//increment n item=scnr.nextLine();//get item from user } for(i=0;i<n;i++) { shoppingList.get(i).printNodeData();//call printNodeData()for each object } }}
class ListItem{ String item; //constructor ListItem(String item) { this.item=item; } void printNodeData() { System.out.println(item); }}
arrow_forward
INSTRUCTION: make this a flowchart
import java.util.HashSet;import java.util.Scanner;import java.util.Set;
public class elements { public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
// Creating two sets Set<Integer> set1 = new HashSet<Integer>();
Set<Integer> set2 = new HashSet<Integer>();
System.out.println("Enter the element of set A [max-5 elements]: "); for (int i = 0; i < 5; i++) { int num = sc.nextInt(); set1.add(num); } System.out.println("Enter the element of set B [max-5 elements]: "); for (int i = 0; i < 5; i++) { int num1 = sc.nextInt(); set2.add(num1); } // creating a menu of opeartions System.out.println("a. Identify elements of set"); System.out.println("b. Check Union"); System.out.println("c. Check Difference"); System.out.println("d. Check Intersection"); System.out.println("e. Check Subset"); System.out.println("f. If set A and B are…
arrow_forward
Write a function that is passed an array of numbers and returns a hash table containing the
minimum, maximum and average value. @{min=????; max-???; average=???}
Test by passing the function 3 different arrays of randomly generated numbers. If the array is
empty set all 3 values to zero. (3)
arrow_forward
2. By using the above class definition, write a program statement for each of the
operation:
// Import the LinkedList class
import java.util.LinkedList;
public class linkedListl {
public static void main(String[] args) {
LinkedList cars = new LinkedList ();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");
}
}
a) Use removeFirst () remove the first item from the list
b) Use removeLast () remove the last item from the list
c) Use getFirst () to display the first item in the list
d) Use getLast () to display the last item in the list
arrow_forward
Add equals and hashCode methods to any class used in a List.
Current Java Code:
package parkingsystem4;import java.util.LinkedList;import java.util.List;
public class ParkingOffice {
String name; String address; List<Car> cars = new LinkedList<Car>(); List<Customer> customers = new LinkedList<Customer>(); List<ParkingLot> lots = new LinkedList<ParkingLot>(); List<ParkingCharge> charges = new LinkedList<ParkingCharge>(); // Default constructor ParkingOffice(){ }
// Parameterized constructor ParkingOffice(String name, String address, List<Car> parkedCars, List<ParkingLot> lots, List<ParkingCharge> charges){ this.name = name; this.address = address; this.cars = parkedCars; this.lots = lots; this.charges = charges; } public void registerCustomer(Customer registeringCustomer){ customers.add(registeringCustomer); } public void…
arrow_forward
SEE MORE QUESTIONS
Recommended textbooks for you
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)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education
Related Questions
- Exercise 3 - Simple hash table Develop a simple hashtable with specified size (parameter) that accepts key-value pairs and stores them in an internal structure. ● • The key has to be a string ● ● Use your hash function from exercise 2 • The value can be an object Demonstrate how your hashtable works with multiple inputs Implement add (key, value), get(key), and print () methodsarrow_forwardpackage 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…arrow_forwardC++ Design a HashMap without using any built-in hash table libraries. Implement the MyHashMap class: MyHashMap() initializes the object with an empty map. void put(int key, int value) inserts a (key, value) pair into the HashMap. If the key already exists in the map, update the corresponding value. int get(int key) returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key. void remove(key) removes the key and its corresponding value if the map contains the mapping for the key. Example 1: Input ["MyHashMap", "put", "put", "get", "get", "put", "get", "remove", "get"] [[], [1, 1], [2, 2], [1], [3], [2, 1], [2], [2], [2]] Output [null, null, null, 1, -1, null, 1, null, -1]arrow_forward
- This is the code below: 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 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…arrow_forwardThis is the code below: 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 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…arrow_forwardThis is the code below: 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 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…arrow_forward
- This is the code below: 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 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…arrow_forwardThis is the code below: 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 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…arrow_forwardThis is the code below: 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 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…arrow_forward
- This is the code below: 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 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…arrow_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_forwardHashTable Data Type: By having each bucket contain a linked list of elements that are hashed to that bucket. Usage: >>> table = SeparateChainingHashTable() # Create a new, empty map. >>> table.put('hello', 'world') # Add a new key-value pair. >>> len(table) # Return the number of key-value pairs stored in the map. 1 >>> table.get('hello') # Get value by key. 'world' >>> del table['hello'] # Equivalent to `table.del_('hello')`, deleting key-value pair. >>> table.get('hello') is None # Return `None` if a key doesn't exist. True """ _empty = None def __init__(self, size=11): self.size = size self._len = 0 self._table = [self._empty] * size def put(self, key, value): hash_ = self.hash(key) node_ = self._table[hash_] if node_ is self._empty: self._table[hash_] = Node(key, value) else: while node_.next is not None:…arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education
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)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education