Java 二进制堆ADT的increaseKey()方法
我必须在二进制堆优先级队列ADT中实现increaseKey()方法,但我不知道如何实现。BinaryHeap只接受扩展Comparable的对象,这允许我使用a.compareTo(b),但是没有明显的方法来增加对象的键Java 二进制堆ADT的increaseKey()方法,java,eclipse,adt,priority-queue,binary-heap,Java,Eclipse,Adt,Priority Queue,Binary Heap,我必须在二进制堆优先级队列ADT中实现increaseKey()方法,但我不知道如何实现。BinaryHeap只接受扩展Comparable的对象,这允许我使用a.compareTo(b),但是没有明显的方法来增加对象的键 public class PrQueue <E extends Comparable<? super E>> { // Parameters private static final int DEAFULT_CAPACITY = 10;
public class PrQueue <E extends Comparable<? super E>>
{
// Parameters
private static final int DEAFULT_CAPACITY = 10;
private E[] theItems;
private int theSize;
// Constructors
/**
* Creates an empty priority queue
*/
public PrQueue();
/**
* Creates a priority queue from an array
* @param e Array of elements
*/
public PrQueue(E[] e);
/**
* This Method orders the elements of the Array
* from smallest to biggest
*/
private void orderPr();
// Methods
/**
* Inserts e in the priority queue
* @param e the element to insert in the
* queue according to its priority value
*/
public void insert(E e);
private void ensureCapacity (int capacity);
/**
* Obtains the element with the lowest priority value
* @return the element with he lowest priority
*/
public E findMin();
/**
* Removes the element with the lowest priority value
* @return the element with the lowest priority value
* prior to the deletion
*/
public E deleteMin();
/**
* Removes all the elements of the queue
*/
public void makeEmpty();
/**
* Tells if the queue is empty
* @return true if this queue is empty, false otherwise
*/
public boolean isEmpty();
/**
* Lowers the value of the item at position p by d amount
* @param p position of the item
* @param d amount
* @return
*/
public boolean decreaseKey(int p, int d)
{
}
/**
*
* @param p
* @param d
* @return
*/
public boolean increaseKey(int p, int d)
{
}
/**
* Removes the element at pth position
* @param p position
* @return deleted element
*/
public E delete(int p);
}
public class PrQueue更改元素的优先级值,然后调用orderPr
以确保它在数据结构中的正确位置,如果不在,则将其移动到那里
但是,既然E
没有提供这样做的方法,那么如何更改元素的优先级值呢?好。。。显而易见的解决方案是向E
添加一个新方法,您可以通过使其扩展一个新接口(使用一个新方法setPriority
)来实现,该接口扩展了Comparable
还有其他可能的解决方案,但它们并不那么优雅。您应该考虑使用orderPr()
方法。orderPr()方法只是组织队列以确保其有序,它不会更改任何元素的优先级值。嗯,好的。所以,正如我所想,没有一种更通用的方法来改变一个可寓言对象的键。