Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Implement change method inside MaxPQ. Make sure the implementation meet the specified performance requirement MaxPQ : public class MaxPQ > { private static int DEFAULT_CAPCITY=10;

Implement change method inside MaxPQ. Make sure the implementation meet the specified performance requirement image text in transcribed MaxPQ :

public class MaxPQ> {

private static int DEFAULT_CAPCITY=10;

// heap-ordered complete binary tree

// in pq[1..N] with pq[0] unused

private Key[] pq;

private int size = 0;

public void insert(Key v) {

pq[++size] = v;

swim(size);

}

public boolean isEmpty() {

return size == 0;

}

public int size() {

return size;

}

public Key max() {

if (size==0) {

return null;

} else {

return pq[1];

}

}

public Key delMax() {

if (size==0) {

return null;

} else {

Key max = pq[1]; // Retrieve max key from top.

swap(1, size--); // Exchange with last item.

pq[size + 1] = null; // Avoid loitering.

sink(1); // Restore heap property.

return max;

}

}

/**

* Change key at index i to newKey and return the old key.

* After this change this priority queue is still a max priority queue

*

* The worst-case running time should not exceed o(lgn),

* and the worst-case space complexity should not exceed O(1),

* where n is the number of elements in the priority queue

*/

public Key change(int i, Key newKey) {

// provide implementation here

return null;

}

// bottom-up repheapify

private void swim(int k) {

while (k > 1 && isLessThan(pq[k/2], pq[k])) {

swap(k/2, k);

k = k/2;

}

}

// top-down repheapify

private void sink(int k) {

while (2*k

int j = 2*k;

if (j

j++; // j is the index of the largest children

if (!isLessThan(pq[k], pq[j]))

break;

swap(k, j);

k = j;

}

}

public MaxPQ() {

this(DEFAULT_CAPCITY);

}

@SuppressWarnings("unchecked")

public MaxPQ(int maxN) {

pq = (Key[]) new Comparable[maxN + 1];

size = 0;

}

@SuppressWarnings("unchecked")

public MaxPQ(Key[] a) {

pq = (Key[]) new Comparable[a.length + 1];

for (int i=0; i

insert(a[i]);

}

}

private void swap(int i, int j) {

Key t = pq[i];

pq[i] = pq[j];

pq[j] = t;

}

private boolean isLessThan(Key v, Key w) {

return v.compareTo(w)

}

Key[] getKeys() {

return pq;

}

}

s k *Change key at index i to newKey and return the old key. *After this change this priority queue is still a max priority queue exceed oCLan *and the worst-case space complexity should not exceed 0(1), *where n is the number of elements in the priority queue public Key changeCint i, Key newKey) // provide implementation here return null; s k *Change key at index i to newKey and return the old key. *After this change this priority queue is still a max priority queue exceed oCLan *and the worst-case space complexity should not exceed 0(1), *where n is the number of elements in the priority queue public Key changeCint i, Key newKey) // provide implementation here return null

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_2

Step: 3

blur-text-image_3

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

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2010 Barcelona Spain September 2010 Proceedings Part 1 Lnai 6321

Authors: Jose L. Balcazar ,Francesco Bonchi ,Aristides Gionis ,Michele Sebag

2010th Edition

364215879X, 978-3642158797

More Books

Students also viewed these Databases questions