Menu

[Solved]Java Please Complete Full Program Exactly Says Required Prompt Problem K Worst Values Find Q37099693

**********************************In Java******************Please complete the full program, and exactly as it says in therequired prompt *********************

Problem – K-Worst Values

Find the k-worst (i.e. smallest) values in a set of data. Assumeyou are given a sequence of values, one value at a time. We do notknow how many elements there are in this sequence. In fact, therecould be infinitely many. Implement the class
KWorstCounter<T extends Comparable<? super T>>implements KWorst<T> that keeps track of the k-smallestelements seen so far in a sequence of data. The class should havetwo methods:

  • public void count(T x) – process the next element in the set ofdata. This operation must run in at worst O(log k) time.

  • public List<T> kworst() – return a sorted (smallest tolargest) list of the k-smallest elements. This must run in at worstO(k log k) time. The method must not clobber the state of yourclass. This means that if you run this method twice in a row, itshould return the same values.

Your KWorstCounter.java class must implement the providedinterface KWorst.java.

Use a priority queue to implement this functionality. We suggestusing the built-in java.util.PriorityQueue, but with a twist: youwill need to use a maximum-heap to solve this problem, not aminimum-heap (try it yourself!). To implement a max-heap in Java,use the following code snippet, taking advantage of PriorityQueue’sconstructor that accepts a Comparator object: newPriorityQueue<>(Collections.reverseOrder()).

As always, feel free to implement your own tester file to ensureproper functionality.

Expert Answer


Answer to **********************************In Java****************** Please complete the full program, and exactly as it says in … . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *