Answered step by step
Verified Expert Solution
Question
1 Approved Answer
the language is Java. Thank you 5. Assume we use a max-on-top heap to implement a priority queue: we insert elements into the queue and
the language is Java. Thank you
5. Assume we use a max-on-top heap to implement a priority queue: we insert elements into the queue and remove elements in the order of their priority (max priority first). a) Give an example scenario showing that for a classic max-on-top heaps considered in class, elements within a given priority may not be removed in FIFO order. In other words, give a scenario where elements x and y have the same priority, x is inserted into the heap before y, but then removeMax invocations remove y before x. (3 points) b) Assume we want to enhance our heap implementation to ensure that we remove elements in the FIFO order within a given priority. In other words, for any two elements with the same priority, the element that was inserted first will be removed first. The methods involved in the heap insertion and removal are "void insert(T item)", "T removeMax(", "void siftUp(int i)", and "void siftDown(int i)". Write these methods to support the above functionality. (3 points) 5. Assume we use a max-on-top heap to implement a priority queue: we insert elements into the queue and remove elements in the order of their priority (max priority first). a) Give an example scenario showing that for a classic max-on-top heaps considered in class, elements within a given priority may not be removed in FIFO order. In other words, give a scenario where elements x and y have the same priority, x is inserted into the heap before y, but then removeMax invocations remove y before x. (3 points) b) Assume we want to enhance our heap implementation to ensure that we remove elements in the FIFO order within a given priority. In other words, for any two elements with the same priority, the element that was inserted first will be removed first. The methods involved in the heap insertion and removal are "void insert(T item)", "T removeMax(", "void siftUp(int i)", and "void siftDown(int i)". Write these methods to support the above functionality. (3 points)Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started