asked 116k views
4 votes
I need help with the question below.

import java.util.*;

public class TreeExample2 {

public static void main (String[] argv)
{
// Make instances of a linked-list and a trie.
LinkedList intList = new LinkedList ();
TreeSet intTree = new TreeSet ();

// Number of items in each set.
int collectionSize = 100000;

// How much searching to do.
int searchSize = 1000;

// Generate random data and place same data in each data structure.
int intRange = 1000000;
for (int i=0; i 0)
r_seed = t;
else
r_seed = t + m;
return ( (double) r_seed / (double) m );
}

// U[a,b] generator
public static double uniform (double a, double b)
{
if (b > a)
return ( a + (b-a) * uniform() );
else {
System.out.println ("ERROR in uniform(double,double):a="+a+",b="+b);
return 0;
}
}

// Discrete Uniform random generator - returns an
// integer between a and b
public static long uniform (long a, long b)
{
if (b > a) {
double x = uniform ();
long c = ( a + (long) Math.floor((b-a+1)*x) );
return c;
}
else if (a == b)
return a;
else {
System.out.println ("ERROR: in uniform(long,long):a="+a+",b="+b);
return 0;
}
}

public static int uniform (int a, int b)
{
return (int) uniform ((long) a, (long) b);
}

public static double exponential (double lambda)
{
return (1.0 / lambda) * (-Math.log(1.0 - uniform()));
}

public static double gaussian ()
{
return rand.nextGaussian ();
}


public static double gaussian (double mean, double stdDeviation)
{
double x = gaussian ();
return mean + x * stdDeviation;
}

} // End of class RandTool

I need help with the question below. import java.util.*; public class TreeExample-example-1
I need help with the question below. import java.util.*; public class TreeExample-example-1
I need help with the question below. import java.util.*; public class TreeExample-example-2

1 Answer

3 votes

The given code is a Java program that includes a class named TreeExample2 with a main method. It demonstrates the usage of a linked list and a tree set data structure to store and search for elements.

The program begins by creating instances of a linked list (LinkedList) and a tree set (TreeSet). Then, it defines two variables: collectionSize and searchSize. collectionSize represents the number of items to be stored in each data structure, while searchSize determines the number of search operations to be performed.

Next, the program generates random data within the range of intRange (which is set to 1000000) and inserts the same data into both the linked list and the tree set.

The program uses a set of utility methods to generate random numbers and perform various operations. These methods include:

uniform(): Generates a random double between 0 and 1 using a linear congruential generator.

uniform(double a, double b): Generates a random double within the range [a, b).

uniform(long a, long b): Generates a random long within the range [a, b].

uniform(int a, int b): Generates a random integer within the range [a, b].

exponential(double lambda): Generates a random number from an exponential distribution with the specified lambda parameter.

gaussian(): Generates a random number from a standard Gaussian (normal) distribution.

gaussian(double mean, double stdDeviation): Generates a random number from a Gaussian distribution with the specified mean and standard deviation.

Overall, the code serves as an example of using a linked list and a tree set in Java, along with utility methods for generating random numbers from various distributions.

answered
User Roman Black
by
8.2k points