0

Binary Heap プライオリティ キュー ADT に increaseKey() メソッドを実装する必要がありますが、その方法がわかりません。BinaryHeap は、a.compareTo(b) を使用できるようにする Comparable を拡張するオブジェクトを受け入れるだけですが、オブジェクトのキーを増やす明確な方法はありません。

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 で検索しましたが、これまでに見たバイナリヒープの実装では、これらのメソッドが含まれていないか、エラーが返されます。

4

1 に答える 1

0

要素の優先度の値を変更し、 を呼び出しorderPrてデータ構造内の適切な場所にあることを確認し、そうでない場合はそこに移動します。

しかし、要素の優先度の値を変更するにはどうすればよいEでしょうか。そのためのメソッドが提供されていないためです。ええと...明白な解決策は、新しいメソッドを に追加するEことです。これは、 を拡張する新しいインターフェイスを (新しいメソッドでsetPriority)拡張することで実行できますComparable

他にも可能な解決策がありますが、それらはそれほどエレガントではありません。

于 2013-12-01T22:26:19.683 に答える