Consider the following implementation of a hash function that maps keys of type
ID: 3837106 • Letter: C
Question
Consider the following implementation of a hash function that maps keys of type E to values of type int. Note that, for the sake of brevity, only relevant code is shown. Assume that you use chaining for conflict resolution, however rather than use linked-lists for the buckets you adopt Binary Search Trees. You are asked to implement the method findnum that, given a key, returns 0 if it is not in the table and its corresponding value otherwise. You may assume that you have at your disposal a method hashCode that given a key computes a hash code for it. The only ArrayList method that you will need is ArrayList.get (int index), that returns the item at the specified index. public class HashMapTree { private ArrayList table;//hashmap private int SIZE = 10;//default size private static class Node { private E key; private int value;//number of times key appears in tree public Node left; public Node right; public E getKey() { return this.key: } public int getValue () { return this. value; } } public int findNum (E key) {//implement me! } }Explanation / Answer
PROGRAM CODE:
public int findNum(E key)
{
int hashCode = hashcode(key);
Node temp = table.get(hashCode);
while(temp != null)
{
if(temp.getkey()== key)
return temp.getValue();
else if(temp.getKey() < key)
temp = temp.right;
else if(temp.getKey() > key)
temp = temp.left;
}
return 0;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.