Q4. Given the following code below, that loads a list of student numbers and com
ID: 3583795 • Letter: Q
Question
Q4. Given the following code below, that loads a list of student numbers and compute a simple hash code for each. [5 points]
Just run it and what is the output. After you add another student to the linked list with following ID
int std3 = 1234567890;
What is the Output of this program after you add the student std3
import java.util.*;
publicclass HashCodeList
{
publicstaticint computeHash(intID)
{
inthcode = ID % 51;
if (hcode< 0) { hcode = - hcode; }
returnhcode;
}
publicstaticvoid main(String[] args)
{
intarraySize = 51;
// create a linkedlist of student IDs
LinkedList<Integer>sIDlist = new LinkedList<Integer>();
intstd1 = 1300131500;
intstd2 = 1402144010;
sIDlist.add(std1);
sIDlist.add(std2);
ListIterator<Integer>iter = sIDlist.listIterator();
while ( iter.hasNext() )
{
intid = iter.next();
System.out.println("Student ID: “ + id + “ hashcode is: " + computeHash(id) );
}
}
}
Q5.Which of the following binary trees are BSTs? If a tree is not a BST, say why [10 points]
(1) (2) (3) (4)
A 10 cat 15
/ / / /
B C 5 bat rat 5 22
/ /
-3 ant 20 30
Explanation / Answer
Solution:
HashCodeList.java
/**
Q4. Given the following code below, that loads a list of student numbers and compute a simple hash code for each.
Just run it and what is the output. After you add another student to the linked list with following ID
int std3 = 1234567890;
What is the Output of this program after you add the student std3 **/
import java.util.*;
public class HashCodeList
{
public static int computeHash(int ID)
{
int hcode = ID % 51;
if (hcode< 0) { hcode = - hcode; }
return hcode;
}
public static void main(String[] args)
{
int arraySize = 51;
// create a linkedlist of student IDs
LinkedList<Integer>sIDlist = new LinkedList<Integer>();
int std1 = 1300131500;
int std2 = 1402144010;
int std3 = 1234567890;
sIDlist.add(std1);
sIDlist.add(std2);
sIDlist.add(std3);
ListIterator<Integer>iter = sIDlist.listIterator();
while ( iter.hasNext() )
{
int id = iter.next();
System.out.println("Student ID: " + id + " hashcode is: " + computeHash(id) );
}
}
}
Sample Output:
Student ID: 1300131500 hashcode is: 26
Student ID: 1402144010 hashcode is: 41
Student ID: 1234567890 hashcode is: 27
Q5.
1) It is not BinarySearchTree, why because B is greater than the A. So it has to ordered to right sub child of A.
2) It is not binarySearchTree because binarySeachTree doesn't allow negative values.
3)It is a binary Sarch Tree.
4)It is not BinarySearchTree because 20 > 15 <22, so it has to ordered to left SubChild of 22.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.