我目前正在使用泛型类型实现二项式最小堆优先级队列。
我给出了以下 binomialminheap.java:
class BinomialMinHeap <K extends Comparable<? super K>, P>
{
public static class Entry <K, P> {
private P priority;
private K key;
private Node<P, K> node;
private Entry (K k, P p)
{
priority = p;
key= k;
}
public P priority () { return priority; }
public K key() { return key; }
}
private static class Node <K, P> {
private Entry<K, P> entry;
private Node<K, P> parent;
.
private Node<K, P> child;
private Node<K, P> sibling;
private int degree;
private Node (Entry<K, P> e)
{
entry = e;
e.node = this;
}
private P priority ()
{
return entry.priority;
}
}
}
如您所见,我有泛型类型 P 和 K。这里的问题是,我不知道如何将泛型类型实现到我的二进制堆中。“Entry”、“Node”和“BinomialHeap”如何协同工作?
我从以下构造函数和一些方法开始:
private Node<P,D> Nodes;
private int size;
public BinomialMinHeap()
{
Nodes = null;
size = 0;
}
public boolean isEmpty ()
{
return Nodes == null;
}
public int size()
{
return size;
}
我必须添加以下方法:
boolean contains (Entry<K, P> e)
Entry<K, P> insert (K k, P p)
boolean changePriority (Entry<K, P> e, P p)
Entry<K, P> minimum ()
Entry<K, P> extractMinimum ()
boolean remove (Entry<K, P> e)