我需要将百万个哈希码插入树中。对于百万个哈希码中的每一个,捕获确定链接列表是否包含哈希码所花费的时间。使用System.nanoTime()以纳秒为单位获取时间。确定用于搜索的链接列表的定时性能的均值和标准差 同样,确定用于搜索的树的定时性能的均值和标准偏差。
我有用于将百万个哈希码输入链接列表并找到平均值和标准差的代码,但我坚持将其放入树中。有人能指出我正确的方向吗?
import java.util.*;
public class DataStructureTiming
{
public static void main(String[] args)
{
int MAXIMUM = 5;//initializing the maximum integer
int MINIMUM = 1;//initializing the minimum integer
Random randomGenerator = new Random();//initializing the generation of random integers
int range = MAXIMUM - MINIMUM + 1;//setting the range of integers from 1 to 1,000,000
ArrayList<Integer> randomNumbersHashCodes = new ArrayList<Integer>(5);//initializing an ArrayList to store the generated random integers with the capacity of 1,000,000
ArrayList<Integer> randomNumbersStorage = new ArrayList<Integer>(5);
//ArrayList<String> randomNumbersStrg = new ArrayList<String>();//initializing an ArrayList to store store the generated hashcodes into a string
for (int index = 1; index <= 5; ++index)//for loop to generate 1,000,000 random integers in a range from 1 to 1,000,000
{
int randomInt = randomGenerator.nextInt(range) + MINIMUM;
randomNumbersStorage.add(randomInt);
randomNumbersHashCodes.add(String.valueOf(randomInt).hashCode());//storing randomly generated numbers in a ArrayList
}//end of for loop for random number generation and storage in an ArrayList
System.out.println("Random generated number:" + randomNumbersStorage);
System.out.println("Hash codes for random numbers:" + randomNumbersHashCodes);
ArrayList<String> randomNumbersStrg = new ArrayList<String>(randomNumbersStorage.size());
for (Integer myInt : randomNumbersStorage)
{
randomNumbersStrg.add(String.valueOf(myInt));
}
List<String> linkedList = new LinkedList<String>();//initializing Linked List
linkedList.addAll(randomNumbersStrg);//adding generated hashcodes to Linked List
System.out.println(linkedList);
List<Long> durations = new ArrayList<>(randomNumbersHashCodes.size());
for (int n : randomNumbersHashCodes)
{
long start = System.nanoTime();
randomNumbersHashCodes.contains(n); // true
long end = System.nanoTime();
durations.add(end - start);
}
***this is where i am having problems********
Tree< Integer > tree = new Tree< Integer >();
for (int x = 0; x <= 4; x++)
{
tree.insertNode(randomNumbers);
}
System.out.println(durations);
//to calculate mean and SD of Durations
long sum = 0;
double sd = 0;
for (int toCalcSum = 0; toCalcSum < durations.size(); toCalcSum++)
{
sum += durations.get(toCalcSum);
}
for (int toCalcSD = 0; toCalcSD < durations.size(); toCalcSD++)
{
sd += ((durations.get(toCalcSD) - sum) *
(durations.get(toCalcSD) - sum)) / (durations.size() - 1);
}
System.out.println("Sum of time: " + sum);
System.out.println("Mean: " + (sum/durations.size()));
System.out.println("SD: " + sd);
}//end of main method
}