Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA Language please using eclipse. Your main task is to implement two priority queues. All two implementations should implement the provided PriorityQueue interface (include implements

JAVA Language please using eclipse.

Your main task is to implement two priority queues. All two implementations should implement the provided PriorityQueue interface (include implements PriorityQueue in your Java code), which means they should work with priorities of type double and there are no corresponding items attached to the priorities. Your implementations should be as fsollows: A class ThreeHeap that implements a min-heap where each non-leaf node has 3 children. You should still use a contiguous portion of an array to store the conceptual complete tree. We suggest you make a copy of BinaryHeap class (discussed in class) and make changes as necessary. A class DWayHeap that implements a d-heap where d is the number of children for nonleaf nodes. Your class should implement the same priority queue interface and it should use a contiguous array portion as in your first implementation. It should include an empty constructor and additional constructor that takes d as an argument, work correctly for any d greater than or equal to 2, and use d as the number of children for nodes. Put your two implementations in two separate Java files, ThreeHeap.java and DWayHeap.java. Your priority queues should allow duplicates. That is, two or more copies of the same value should be allowed to exist in the heap at the same time. For example, if you call deleteMin and you have {3.0, 3.0, 6.0, 7.0} in the heap, it would just return one of the 3.0 values, then on the next deleteMin it would return the other 3.0. It does not matter "which" 3.0 is returned first. According to our definition of priority queue, what must be guaranteed is that both 3.0 values will be returned before a 6.0 or 7.0 is returned, and that the 6.0 would be returned before the 7.0. Your implementations should automatically grow as necessary. (If interested, you may also have them shrink when appropriate; this is optional.) For any arrays, you should start with a small array (say, 10 elements) and resize to use an array twice as large whenever the array becomes full, copying over the elements in the smaller array. Do the copying with a for loop rather than any Java library methods (even though using the library is how one would normally do it). You may use the length field of an array as needed. Be sure to test your solutions thoroughly. For instance, you may generate 1000 random numbers, insert them into a priority queue, and keep deleteMin() as long as the priority queue is not empty. Part of the grading will involve thorough testing including any difficult cases. For this assignment, we will be grading more strictly for things like style and efficiency.

Priority queue class :

/** * Base interface for priority queue implementations for doubles. Throw * exceptions as appropriate. */ public interface PriorityQueue { /** * Returns true if priority queue has no elements * * @return true if the priority queue has no elements */ public boolean isEmpty();

/** * Returns the number of elements in this priority queue. * * @return the number of elements in this priority queue. */ public int size();

/** * Returns the minimum element in the priority queue * * @return the minimum element * @throws EmptyPQException * if priority queue contains no elements */ public double findMin();

/** * Inserts a new element into the priority queue. Duplicate values ARE * allowed. * * @param x * element to be inserted into the priority queue. */ public void insert(double x);

/** * Removes and returns the minimum element from the priority queue. * * @return the minimum element * @throws EmptyPQException * if priority queue contains no elements */ public double deleteMin();

/** * Resets the priority queue to appear as not containing any elements. */ public void makeEmpty();

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Students also viewed these Databases questions