C# 如何在创建优先级队列时修复IComparable
这是我在GitHub找到的优先级队列C# 如何在创建优先级队列时修复IComparable,c#,C#,这是我在GitHub找到的优先级队列 public class PriorityQueue<T> where T : IComparable<T> { private List<T> data; public PriorityQueue() { this.data = new List<T>(); } public void Enq
public class PriorityQueue<T> where T : IComparable<T>
{
private List<T> data;
public PriorityQueue()
{
this.data = new List<T>();
}
public void Enqueue(T item)
{
data.Add(item);
int ci = data.Count - 1; // child index; start at end
while (ci > 0)
{
int pi = (ci - 1) / 2; // parent index
if (data[ci].CompareTo(data[pi]) >= 0) break; // child item is larger than (or equal) parent so we're done
T tmp = data[ci]; data[ci] = data[pi]; data[pi] = tmp;
ci = pi;
}
}
public T Dequeue()
{
// assumes pq is not empty; up to calling code
int li = data.Count - 1; // last index (before removal)
T frontItem = data[0]; // fetch the front
data[0] = data[li];
data.RemoveAt(li);
--li; // last index (after removal)
int pi = 0; // parent index. start at front of pq
while (true)
{
int ci = pi * 2 + 1; // left child index of parent
if (ci > li) break; // no children so done
int rc = ci + 1; // right child
if (rc <= li && data[rc].CompareTo(data[ci]) < 0) // if there is a rc (ci + 1), and it is smaller than left child, use the rc instead
ci = rc;
if (data[pi].CompareTo(data[ci]) <= 0) break; // parent is smaller than (or equal to) smallest child so done
T tmp = data[pi]; data[pi] = data[ci]; data[ci] = tmp; // swap parent and child
pi = ci;
}
return frontItem;
}
public T Peek()
{
T frontItem = data[0];
return frontItem;
}
public int Count()
{
return data.Count;
}
public override string ToString()
{
string s = "";
for (int i = 0; i < data.Count; ++i)
s += data[i].ToString() + " ";
s += "count = " + data.Count;
return s;
}
public bool IsConsistent()
{
// is the heap property true for all data?
if (data.Count == 0) return true;
int li = data.Count - 1; // last index
for (int pi = 0; pi < data.Count; ++pi) // each parent index
{
int lci = 2 * pi + 1; // left child index
int rci = 2 * pi + 2; // right child index
if (lci <= li && data[pi].CompareTo(data[lci]) > 0) return false; // if lc exists and it's greater than parent then bad.
if (rci <= li && data[pi].CompareTo(data[rci]) > 0) return false; // check the right child too.
}
return true; // passed all checks
} // IsConsistent
我创建了一个类似这样的adge类:
public class Node
{
long x;
long y;
public long parent;
public long rank;
public Node(long a, long b, long c)
{
x = a;
y = b;
parent = c;
rank = 0;
}
}
public class Edge : IComparable<Edge>
{
public long u;
public long v;
public double weight;
public Edge(long a, long b, double c)
{
u = a;
v = b;
weight = c;
}
public int CompareTo(Edge e1, Edge e2)
{
return e1.weight < e2.weight ? -1 : 1;
}
public int CompareTo(Edge other)
{
throw new NotImplementedException();
}
}
PriorityQueue<Edge> edges = new PriorityQueue<Edge>();
当我尝试创建一个带有边缘的实例表单优先级队列类时,出现如下错误:
public class Node
{
long x;
long y;
public long parent;
public long rank;
public Node(long a, long b, long c)
{
x = a;
y = b;
parent = c;
rank = 0;
}
}
public class Edge : IComparable<Edge>
{
public long u;
public long v;
public double weight;
public Edge(long a, long b, double c)
{
u = a;
v = b;
weight = c;
}
public int CompareTo(Edge e1, Edge e2)
{
return e1.weight < e2.weight ? -1 : 1;
}
public int CompareTo(Edge other)
{
throw new NotImplementedException();
}
}
PriorityQueue<Edge> edges = new PriorityQueue<Edge>();
类型“A4.Edge”不能用作泛型类型或方法“PriorityQueue”中的类型参数“T”。没有从“A4.Edge”到“System.IComparable”的隐式引用转换
如何解决此问题?在定义PriorityQueue类时,T必须实现IComparable。您的Edge类没有实现该接口,因此您会遇到编译器错误- 您的Edge类必须提供某种方式,以便将它的一个实例与另一个实例进行比较。这是通过实现IComparable接口实现的:
public class Edge : IComparable<Edge>
{
public long u;
public long v;
public double weight;
public Edge(long a, long b, double c)
{
u = a;
v = b;
weight = c;
}
public int CompareTo(Edge other)
{
// your comparison here
}
}
通过从边缘实现IComparable?无论如何,当你为你的问题提供一个比较器时,你必须使用它。除非被告知,否则CompareTo不会神奇地调用它。说到这里,我假设您根本不需要提供比较器,只需从Edge类实现接口即可。您好,谢谢您的回复,但是我如何解决这个问题呢?PriorityQueue Edge=new PriorityQueuenew Comparer@共和国类边:i可比较{public long u;public long v;public double weight;public Edgelong a,long b,double c{u=a;v=b;weight=c;}public int compareToredge e1,Edge e2{return e1.weight