如何在创建优先级队列时修复IComparable <t>

时间:2019-04-03 10:24:12

标签: c#

这是我在GitHub上找到的优先级队列

 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>();

在通用类型或方法“ PriorityQueue”中,类型“ A4.Edge”不能用作类型参数“ T”。从'A4.Edge'到'System.IComparable没有隐式引用转换 我该如何解决?

1 个答案:

答案 0 :(得分:1)

从定义PriorityQueueT开始,必须实现IComparable<T>。您的Edge类未实现该接口,因此会出现编译器错误-

您的Edge类必须提供某种方式,以便将其一个实例与另一个实例进行比较。这是通过实现IComparable<T>-interface

来完成的:
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
    }
 }

现在,由于您的班级已经提供了某种机制使其具有可比性,因此您完全不需要为您的Comparer级提供一个PriorityQueue。实际上,您甚至没有在代码中使用它,因此请从构造函数中省略该参数:

PriorityQueue<Edge> edges = new PriorityQueue<Edge>();