【发布时间】:2013-12-17 11:41:00
【问题描述】:
我必须在二叉堆优先级队列 ADT 中实现 increaseKey() 方法,但我就是不知道该怎么做。 BinaryHeap 只接受扩展 Comparable 的对象,这允许我使用 a.compareTo(b),但没有明显的方法来增加对象的键。
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);
}
那么,关于如何做到这一点的任何想法?我在 google 和 here 上搜索过,但在我目前看到的 binaryheap 实现中,它们不包含这些方法,或者它们返回错误。
【问题讨论】:
-
你应该考虑使用
orderPr()方法。 -
orderPr() 方法只是组织队列以确保它是有序的,它不会改变任何元素的优先级值。
标签: java eclipse adt priority-queue binary-heap