Question

In: Computer Science

Ex: Write a program to randomly generate 31 positive integers below 100 to find the median...

Ex: Write a program to randomly generate 31 positive integers below 100 to find the median and display it on the root, and display the remaining integers as BST (Binary Search Tree).
(Generate JTextArea panels at the bottom to display the results of the potential, intermediate, and rearward tours.)

Write java code (Using GUI,no javafx)and show me the output.

If you can't understand the question, let me know. :)

Thank you :)

Solutions

Expert Solution

#include <iostream>

using namespace std;

  

// Heap capacity

#define MAX_HEAP_SIZE (128)

#define ARRAY_SIZE(a) sizeof(a)/sizeof(a[0])

  

//// Utility functions

  

// exchange a and b

inline

void Exch(int &a, int &b)

{

    int aux = a;

    a = b;

    b = aux;

}

  

// Greater and Smaller are used as comparators

bool Greater(int a, int b)

{

    return a > b;

}

  

bool Smaller(int a, int b)

{

    return a < b;

}

  

int Average(int a, int b)

{

    return (a + b) / 2;

}

  

// Signum function

// = 0 if a == b - heaps are balanced

// = -1 if a < b   - left contains less elements than right

// = 1 if a > b   - left contains more elements than right

int Signum(int a, int b)

{

    if( a == b )

        return 0;

  

    return a < b ? -1 : 1;

}

  

// Heap implementation

// The functionality is embedded into

// Heap abstract class to avoid code duplication

class Heap

{

public:

    // Initializes heap array and comparator required

    // in heapification

    Heap(int *b, bool (*c)(int, int)) : A(b), comp(c)

    {

        heapSize = -1;

    }

  

    // Frees up dynamic memory

    virtual ~Heap()

    {

        if( A )

        {

            delete[] A;

        }

    }

  

    // We need only these four interfaces of Heap ADT

    virtual bool Insert(int e) = 0;

    virtual int GetTop() = 0;

    virtual int ExtractTop() = 0;

    virtual int GetCount() = 0;

  

protected:

  

    // We are also using location 0 of array

    int left(int i)

    {

        return 2 * i + 1;

    }

  

    int right(int i)

    {

        return 2 * (i + 1);

    }

  

    int parent(int i)

    {

        if( i <= 0 )

        {

            return -1;

        }

  

        return (i - 1)/2;

    }

  

    // Heap array

    int   *A;

    // Comparator

    bool (*comp)(int, int);

    // Heap size

    int   heapSize;

  

    // Returns top element of heap data structure

    int top(void)

    {

        int max = -1;

  

        if( heapSize >= 0 )

        {

            max = A[0];

        }

  

        return max;

    }

  

    // Returns number of elements in heap

    int count()

    {

        return heapSize + 1;

    }

  

    // Heapification

    // Note that, for the current median tracing problem

    // we need to heapify only towards root, always

    void heapify(int i)

    {

        int p = parent(i);

  

        // comp - differentiate MaxHeap and MinHeap

        // percolates up

        if( p >= 0 && comp(A[i], A[p]) )

        {

            Exch(A[i], A[p]);

            heapify(p);

        }

    }

  

    // Deletes root of heap

    int deleteTop()

    {

        int del = -1;

  

        if( heapSize > -1)

        {

            del = A[0];

  

            Exch(A[0], A[heapSize]);

            heapSize--;

            heapify(parent(heapSize+1));

        }

  

        return del;

    }

  

    // Helper to insert key into Heap

    bool insertHelper(int key)

    {

        bool ret = false;

  

        if( heapSize < MAX_HEAP_SIZE )

        {

            ret = true;

            heapSize++;

            A[heapSize] = key;

            heapify(heapSize);

        }

  

        return ret;

    }

};

  

// Specilization of Heap to define MaxHeap

class MaxHeap : public Heap

{

private:

  

public:

    MaxHeap() : Heap(new int[MAX_HEAP_SIZE], &Greater) { }

  

    ~MaxHeap() { }

  

    // Wrapper to return root of Max Heap

    int GetTop()

    {

        return top();

    }

  

    // Wrapper to delete and return root of Max Heap

    int ExtractTop()

    {

        return deleteTop();

    }

  

    // Wrapper to return # elements of Max Heap

    int GetCount()

    {

        return count();

    }

  

    // Wrapper to insert into Max Heap

    bool Insert(int key)

    {

        return insertHelper(key);

    }

};

  

// Specilization of Heap to define MinHeap

class MinHeap : public Heap

{

private:

  

public:

  

    MinHeap() : Heap(new int[MAX_HEAP_SIZE], &Smaller) { }

  

    ~MinHeap() { }

  

    // Wrapper to return root of Min Heap

    int GetTop()

    {

        return top();

    }

  

    // Wrapper to delete and return root of Min Heap

    int ExtractTop()

    {

        return deleteTop();

    }

  

    // Wrapper to return # elements of Min Heap

    int GetCount()

    {

        return count();

    }

  

    // Wrapper to insert into Min Heap

    bool Insert(int key)

    {

        return insertHelper(key);

    }

};

  

// Function implementing algorithm to find median so far.

int getMedian(int e, int &m, Heap &l, Heap &r)

{

    // Are heaps balanced? If yes, sig will be 0

    int sig = Signum(l.GetCount(), r.GetCount());

    switch(sig)

    {

    case 1: // There are more elements in left (max) heap

  

        if( e < m ) // current element fits in left (max) heap

        {

            // Remore top element from left heap and

            // insert into right heap

            r.Insert(l.ExtractTop());

  

            // current element fits in left (max) heap

            l.Insert(e);

        }

        else

        {

            // current element fits in right (min) heap

            r.Insert(e);

        }

  

        // Both heaps are balanced

        m = Average(l.GetTop(), r.GetTop());

  

        break;

  

    case 0: // The left and right heaps contain same number of elements

  

        if( e < m ) // current element fits in left (max) heap

        {

            l.Insert(e);

            m = l.GetTop();

        }

        else

        {

            // current element fits in right (min) heap

            r.Insert(e);

            m = r.GetTop();

        }

  

        break;

  

    case -1: // There are more elements in right (min) heap

  

        if( e < m ) // current element fits in left (max) heap

        {

            l.Insert(e);

        }

        else

        {

            // Remove top element from right heap and

            // insert into left heap

            l.Insert(r.ExtractTop());

  

            // current element fits in right (min) heap

            r.Insert(e);

        }

  

        // Both heaps are balanced

        m = Average(l.GetTop(), r.GetTop());

  

        break;

    }

  

    // No need to return, m already updated

    return m;

}

  

void printMedian(int A[], int size)

{

    int m = 0; // effective median

    Heap *left = new MaxHeap();

    Heap *right = new MinHeap();

  

    for(int i = 0; i < size; i++)

    {

        m = getMedian(A[i], m, *left, *right);

  

        cout << m << endl;

    }

  

    // C++ more flexible, ensure no leaks

    delete left;

    delete right;

}

// Driver code

int main()

{

    int A[] = {5, 15, 1, 3, 2, 8, 7, 9, 10, 6, 11, 4};

    int size = ARRAY_SIZE(A);

  

    // In lieu of A, we can also use data read from a stream

    printMedian(A, size);

  

    return 0;

}


Related Solutions

Write a program that does the following: Generate an array of 20 random integers between -100...
Write a program that does the following: Generate an array of 20 random integers between -100 and 100. Compute the average of the elements of the array and find the number of elements which are above the average. For example, if the elements of the array were 5 2 4 1 3 then your program should output The average is 3.0 There are two elements above the average Find the smallest element of the array as well as its index...
Consider all positive integers less than 100. Find the number of integers divisible by 3 or...
Consider all positive integers less than 100. Find the number of integers divisible by 3 or 5? Consider strings formed from the 26 English letters. How many strings are there of length 5? How many ways are there to arrange the letters `a',`b', `c', `d', and `e' such that `a' is not immediately followed by`e' (no repeats since it is an arrangement)?
Write a program in Java that reads in a set of positive integers and outputs how...
Write a program in Java that reads in a set of positive integers and outputs how many times a particular number appears in the list. You may assume that the data set has at most 100 numbers and -999 marks the end of the input data. The numbers must be output in increasing order. For example, for the data 15 40 28 62 95 15 28 13 62 65 48 95 65 62 65 95 95 -999 The output is...
JAVA Write a program that reads the integers between -100 and 100 and counts the occurrences...
JAVA Write a program that reads the integers between -100 and 100 and counts the occurrences of each with ascending order. input: line1:number of figures line2:number Sample Input 5 -3 100 -1 -2 -1 Sample Output -3 1 -2 1 -1 2 100 1
Write a program that uses a custom function to generate a specified number of random integers...
Write a program that uses a custom function to generate a specified number of random integers in a specified range. This custom function should take three arguments; the number of integers to generate, the lower limit for the range, and the upper limit for the range. Values for these arguments should be entered by the user in main. The custom function should display the random integers on one line separated by single spaces. The function should also report how many...
JAVA PROGRAM Write program that will prompt user generate two random integers with values from 1...
JAVA PROGRAM Write program that will prompt user generate two random integers with values from 1 to 10 then subtract the second integer from the first integer. Note, if the second integer is greater than the first integer, swap the two integers before making the subtraction.Then prompt user for the answer after the subtraction. If the answer is correct, display “Correct”otherwise display “Wrong”.You will need to do this in a loop FIVE times and keep a count of how many...
Write two versions of a program that reads from the user a sequence of positive integers...
Write two versions of a program that reads from the user a sequence of positive integers ending with -1, and another positive integer num that the user wishes to search for. The program should then print all the line numbers in sequence entered by the user, that contain num, or a message saying that num does not show at all in the sequence. Your program should interact with the user exactly as it shows in the following example: Please enter...
For this assignment, write a program that will generate three randomly sized sets of random numbers...
For this assignment, write a program that will generate three randomly sized sets of random numbers using DEV C++ To use the random number generator, first add a #include statement for the cstdlib library to the top of the program: #include <cstdlib> Next, initialize the random number generator. This is done by calling the srand function and passing in an integer value (known as a seed value). This should only be done ONE time and it MUST be done before...
#Write a program in Python that given a list of positive integers, return another list where...
#Write a program in Python that given a list of positive integers, return another list where each element corresponds to the sum of the digits of the elements o#f the given list. #Example: # input # alist=[124, 5, 914, 21, 3421] # output # sum=[7, 5, 14, 3, 18]
Write a Java program with comments that randomly generates an array of 500,000 integers between 0...
Write a Java program with comments that randomly generates an array of 500,000 integers between 0 and 499,999, and then prompts the user for a search key value. Estimate the execution time of invoking the linearSearch method in Listing A below. Sort the array and estimate the execution time of invoking the binarySearch method in Listing B below. You can use the following code template to obtain the execution time: long startTime = System.currentTimeMillis(); perform the task; long endTime =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT