Answer:
Java Code:
public void add(K key, V value) {
int index = getBucketIndex(key);
MapNode<K, V> head = buckets.get(index);
while(head != null) {
if(head.key.equals(key)) {
head.value = value;
return;
}
head = head.next;
}
MapNode<K, V> newNode = new MapNode<K, V>(key, value);
newNode.next = buckets.get(index);
buckets.set(index, newNode);
size++;
double loadFactor = (size * 1.0) / buckets.size();
if(loadFactor > 0.7) {
rehash();
}
}
private void rehash() {
System.out.println("Rehash");
ArrayList<MapNode<K, V>> temp = buckets;
buckets = new ArrayList<MapNode<K,V>>();
for(int i = 0; i < 2*temp.size()+1; i++) {
buckets.add(null);
}
size = 0;
for(int i = 0; i < temp.size(); i++) {
MapNode<K, V> head = temp.get(i);
while(head != null) {
insert(head.key, head.value);
head = head.next;
}
}
}
public void remove(K key) {
int index = getBucketIndex(key);
MapNode<K, V> head = buckets.get(index);
MapNode<K, V> prev = null;
while(head != null) {
if(head.key.equals(key)) {
if(prev == null) {
head = head.next;
buckets.set(index, head);
}
else {
prev.next = head.next;
}
size--;
return;
}
prev = head;
head = head.next;
}
}
public V contains(K key) {
int index = getBucketIndex(key);
MapNode<K, V> head = buckets.get(index);
while(head != null) {
if(head.key.equals(key)) {
return head.value;
}
head = head.next;
}
return null;
}
write the code in java please Write code for an [add remove contains |rehash getLoad] method...
Let 'M' denote the hash table size. Consider the following four different hash table implementations: a. Implementation (I) uses chaining, and the hash function is hash(x)x mod M. Assume that this implementation maintains a sorted list of the elements (from biggest to smallest) for each chain. b. Implementation (II) uses open addressing by Linear probing, and the hash function is ht(x) - (hash(x) + f(i)) mod M, where hash(x)x mod M, and f(i)- c. Implementation (III) uses open addressing by...
secondary clustering in a hash table occurs when using a. separate chaining b. double hashing c. linear probing d. quadratic probing
This should be in Java Create a simple hash table You should use an array for the hash table, start with a size of 5 and when you get to 80% capacity double the size of your array each time. You should create a class to hold the data, which will be a key, value pair You should use an integer for you key, and a String for your value. For this lab assignment, we will keep it simple Use...
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...
Develop a java program that creates Hash Table, with collision resolution either by “Separate Chaining” or “Open Addressing” (aka “Probing”).Then reads a text file into the Hash table. The program should be able to do a search for inserted data and be modified to keep track of actual clock time for a given run, as well as counting the number of operations required. The track-worthy operations will likely include character and/or full-key comparisons.
Need help on problems 3 and 4 only thanks in advance.
Define the following terms: hash table hash function perfect hash function What is a collision? Explain three ways of handling collisions (a program is not needed; a clear brief explanation suffices). Consider a hashing scheme that uses linear probing to resolve collisions. Design an algorithm (no code necessary) to delete an item from the hash table. What precautions do you need to take to make it work properly? Given...
Given the input of (2341,4234, 2839, 430, 22, 397, 3920;, and a fixed table size of 7, and a hash-function h1(x)-x mod 7, show the resulting a. Separate chaining hash-table b. Linear probing hash-table. c. Quadratic probing hash-table. d. Hash Table with second hash function h2(x)- (2x - 1) mod 7
^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...
Given input {4371, 1323, 6173, 4199, 4344, 9679, 1989} and a hash function h(x) = x (mod () 10), show the resulting: a. Separate chaining hash table b. Hash table using linear probing c. Hash table using quadratic probing d. Hash table with second hash function h2(x) = 7 - (x mod 7) *Assume the table size is 10.
Using the Red-Black Tree applet, you input the following numbers into the Red-Black tree, keep all red-black rules (on page 433) either using color changes or rotation. The result graph should be height balance and red-black correct. (20 pts) Attach your results of screen shots for every question. Insert 50, 70, 20, 90 Insert 50, 70, 20, 90, 99 Insert 50, 70, 20, 90, 80 Insert 50, 70, 20, 80, 60, 90, 75, 95 Insert 50, 70, 20, 90, 60,...