peek.add,
peek, and poll in a heap.Is a sorted array a heap?
Here’s a barebones Heap class that just stores
integers:
public class Heap {
int[] A;
int size;
// other methods here
/** Return the smallest value in the heap without modifying the heap.
* Precondition: size >= 1 */
public int peek() {
// code here
}
}Implement the peek operation. Hint: it’s not a lot of
code!
For the following problems, you may use without proof the fact that the height of a heap is O(\(\log n\)).
Analyze the runtime of add.
Analyze the runtime of peek.
Analyze the runtime of poll.
Consider the changePriority operation of the A3
PriorityQueue interface, which has the following spec and
header:
/** Change the priority associated with value v to p.
* Precondition: v is in the heap. */
public void changePriority(V v, P p);In A3, a value (of type V) and its associated priority
(of type P) are stored together in a wrapper class called
Entry, which simply has a value field and a
priority field. The backing storage is then an array of
type Entry, so each node in the tree carries around both
its value and its priority.
Devise an algorithm to implement changePriority as
efficiently as possible and give its runtime. Hint: it may not
be all that efficient.