Question

JAVA CODE Write a JAVA program to do the following. Input an integer x. (Should work...

JAVA CODE

Write a JAVA program to do the following.

Input an integer x. (Should work with “big” numbers.)

Create a completely-skewed BST S containing 1, 2, . . . , x.

Create a BST R containing x integers without repetitions gen- erated at random. (To minimize the risk of repetitions, you can multiply the value returned by random() by a big number.) Given that the numbers are generated uniformly at random, the tree will likely be balanced.

Measure the time to search in S for a number that is not in the tree.

Measure the time to search in R for a new random number.

Display the time taken for each search.

Thank you in advance

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Generate random numbers without repetitions

public static List<int> GetRandomNumbers(int count)
{
    List<int> randomNumbers = new List<int>(); 

    for (int i=0; i<count; i++) 
    {    
        int number;

        do number = random.Next();
        while (randomNumbers.Contains(number));

        randomNumbers.Add(number);
    }

    return randomNumbers;
}

Randomized selection.

We return to finding the ismallest item for a fixed but arbitrary integer 1 ≤ i ≤ n, which we call the rank of that item. We can use the splitting function of quicksort also for selection. As in quicksort, we choose a random pivot and split the array, but we recurse only for one of the two sides. We invoke the function with the range of indices of the current subarray and the rank of the desired item, i. Initially, the range consists of all indices between ℓ = 1 and r = n, limits included.

int RSELECT(int ℓ, r, i)

q = RSPLIT(ℓ, r);

m = q − ℓ + 1;

if i < m

then

return RSELECT(ℓ, q − 1, i)

elseif i = m

then

return q

else return RSELECT(q + 1, r, i − m)

endif.

For small sets, the algorithm is relatively ineffective and its running time can be improved by switching over to sorting when the size drops below some constant threshold. On the other hand, each recursive step makes some progress so that termination is guaranteed even without special treatment of small sets. Expected running time. For each 1 ≤ m ≤ n, the probability that the array is split into subarrays of sizes m − 1 and n − m is 1 n . For convenience we assume that nis even.

The expected running time increases with increasing number of items, T (k) ≤ T (m) if k ≤ m.

Hence, T (n) ≤ n + 1 n Xn m=1 max{T (m − 1),

T (n − m)} ≤ n + 2 n Xn m= n 2 +1 T (m − 1).

Assume inductively that T (m) ≤ cm for m < n and a sufficiently large positive constant c. Such a constant c can certainly be found for m = 1, since for that case the running time of the algorithm is only a constant. This establishes the basis of the induction. The case of n items reduces to cases of m < n items for which we can use the induction hypothesis.

We thus get T (n) ≤ n + 2c n Xn m= n 2 +1 m − 1 = n + c · (n − 1) − c 2 · n 2 + 1 = n + 3c 4 · n − 3c 2 . Assuming c ≥ 4 we thus have T (n) ≤ cn as required. Note that we just proved that the expected running time of RSELECT is only a small constant times that of RSPLIT. More precisely, that constant factor is no larger than four.

Add a comment
Know the answer?
Add Answer to:
JAVA CODE Write a JAVA program to do the following. Input an integer x. (Should work...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • write a java program Accept a positive integer n from keyboard and then create an array...

    write a java program Accept a positive integer n from keyboard and then create an array or arraylist containing n random elements within the range [-n,n). Print out the random array or arraylist, and then find out and print out the number of inversions and the maximum subarray (index range of the maximum subarray along with the maximum subarray sum) in the array or arraylist using divide and conquer, respectively. For example, suppose we accept integer 6 from keyboard, then...

  • Homework description::::: Write JAVA program with following description. Sample output with code will be helful... A...

    Homework description::::: Write JAVA program with following description. Sample output with code will be helful... A compiler must examine tokens in a program and decide whether they are reserved words in the Java language, or identifiers defined by the user. Design a program that reads a Java program and makes a list of all the identifiers along with the number of occurrences of each identifier in the source code. To do this, you should make use of a dictionary. The...

  • Please show that the code is working at the end. Your program should read from the standard input...

    Please show that the code is working at the end. Your program should read from the standard input a sequence of integer values, with each value separated by a space. Your task is to: Build a binary search tree using these values in the order they are entered. Print 3 traversals: pre-, in-, and post-order. Allow the user to insert/delete a value. Once a new tree is generated, print it in-order. Find predecessor of a given value. The predecessor is...

  • Please complete this code for java Lab Write a program as follows: Create an array with...

    Please complete this code for java Lab Write a program as follows: Create an array with 10 elements. The array should contain numbers generated randomly between 1 and 100 Ask the user to enter any number - Search the array to see if the user entered number is in the array If the user-entered number is in the array, print a 'match found' message. Also print at which index the match was found, else print a 'match not found' message...

  • Please write a Java program that does the following: Create an array of 100 integers. Store...

    Please write a Java program that does the following: Create an array of 100 integers. Store 100 random integers (between 1 and 100) in the array. Print out the elements of the array. Sort the array in ascending order. Print out the sorted array. Prompt the user to enter a number between 1 and 100. Search the array for that number and then display "Found" or "Not Found" message. Display each number from 1 to 100 and the number of...

  • Create a program using the Random class and While loops in Java. The program should generate...

    Create a program using the Random class and While loops in Java. The program should generate a random number from 50 through 100. The loop should add the five random numbers generated. On each iteration of the loop the program should print out the number generated, how many numbers have been generated thus far, and the sum so far. On the last iteration, the printout should say the The final total is.... Teach comment the last time I did it:...

  • Please help! We use Java JGrasp. Write a Java program to take the input of 5...

    Please help! We use Java JGrasp. Write a Java program to take the input of 5 numbers and output the mean (average) and standard deviation of the numbers. If the numbers are x1, x2, x3, x4, and x5, the formula for mean is X = (x1 + x2 + x3 + x4 + x5)/5 And the formula for standard deviation is You can also break standard deviation down like this: 1. Work out the Mean (the simple average of the...

  • 1. Write a C code that do the following: Generate array of random integer numbers of...

    1. Write a C code that do the following: Generate array of random integer numbers of size 25 Fill the array with random integers ranging from [1-100] Find the maximum number of the array and its location Find the minimum number of the array and its location Search for a number in the array Print the array Flip the array Sort this array Quit the program when the user choose to exit

  • Write a script that works in vim: We are given a Java program RandomTest.java that generates...

    Write a script that works in vim: We are given a Java program RandomTest.java that generates specified number of random integers in the range [0...99]. The program prints FAIL if either 0 or 99 is generated (along with a count of how many times). Otherwise it prints PASS. The program takes the number of random integers to generate as a command line argument as shown below. Note that the given results each time is is run. [an@localhost ~] java Random...

  • Write a program in either C++ or Java meeting these requirements Description: In this program, we...

    Write a program in either C++ or Java meeting these requirements Description: In this program, we assume that our data comes in dynamically. We need to maintain our data in a heap, after every insertion and deletion. We also need to handle the underlying array dynamically. whenever we detect an overflow in our array, we will create a new array with size doubling the previous size. Requirements: 1. (Dynamic Data) when we generate the data, we simulate dynamic data. We...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT