Question: Java please! Create a class BoardingHeap with the following fields and methods: //You may store additional private fields as needed private Passenger[] heap; //array of

Java please! Create a class BoardingHeap with the following fields and methods:

//You may store additional private fields as needed private Passenger[] heap; //array of passengers currently in the heap public BoardingHeap() //This should be your only constructor

//Public heap methods. You may add private methods public void enqueue(Passenger p) public Passenger dequeue() public boolean isEmpty()

3.1. The Heap Itself

We will directly test the contents of your heap array, and we encourage you to test it too. Store the highest priority element in the heap at index 1. Index 0 should be unused. Unused indexes (0, plus any past the current size of your heap) should contain null. Your heap array should expand to fit however many passengers it is given. You may choose when to expand, how, and by how much. (One option is to double the size of the array by naively copying all elements into a new 2x-sized array when you run out of space.)

3.2. The Constructor

Your BoardingHeap constructor should take no arguments and produce an empty BoardingHeap.

3.3. Enqueue

Calling enqueue(ps) should insert a Passenger ps into the BoardingHeap according to pss priority. In the event of a tie in priority between ps and its parent, do not swap. Priority is calculated according to the following rules:

A passenger with a higher preferredBoarding number always has higher priority.

Otherwise, a passengers priority is equal to the earliest time they could complete boarding, based on the passengers that have already dequeued and begun boarding.

Note that we only calculate a passengers priority once, immediately before they are enqueued, so it is very likely that priority values will not match actual boarding completion times. This gives an sneaky priority advantage to passengers that show up early.

3.4. Dequeue

Calling dequeue() should remove the highest priority item from the heap, while maintaining the heaps shape and ordering constraints. If, while maintaining the ordering constraint, the left and right children have the same priority, swap with the right child. (This is the child located at index 2*c+1 where c is the current index.) If there is a tie with the parent, DO swap for dequeue. (The current passenger we are considering was originally the last passenger, so we want to give the early arrival advantage to any other passenger in the heap.)

3.5. IsEmpty

This method should behave as expected for a min-heap.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!