这个问题已经在这里有了答案:




11年前关闭。






.NET中是否有类似堆的类?
我需要某种可以从中检索分钟的集合。元素。我只想要3种方法:

  • Add()
  • RemoveMinElement()
  • GetMinElement()

  • 我不能使用排序列表,因为键必须是唯一的,并且我可能有几个相同的元素。

    最佳答案

    您可以使用带有自定义键的 SortedList SortedDictionary (请参见下面的讨论)。如果您使用的是具有引用相等性的类型,但是可以根据您关心的值进行比较,那么这可以工作。
    像这样:

    class HeapKey : IComparable<HeapKey>
    {
        public HeapKey(Guid id, Int32 value)
        {
            Id = id;
            Value = value;
        }
    
        public Guid Id { get; private set; }
        public Int32 Value { get; private set; }
    
        public int CompareTo(HeapKey other)
        {
            if (_enableCompareCount)
            {
                ++_compareCount;
            }
    
            if (other == null)
            {
                throw new ArgumentNullException("other");
            }
    
            var result = Value.CompareTo(other.Value);
    
            return result == 0 ? Id.CompareTo(other.Id) : result;
        }
    }
    
    这是使用具有二进制堆性能特征的SortedDictionary的工作示例:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    namespace SortedDictionaryAsBinaryHeap
    {
        class Program
        {
            private static Boolean _enableCompareCount = false;
            private static Int32 _compareCount = 0;
    
            static void Main(string[] args)
            {
                var rnd = new Random();
    
                for (int elementCount = 2; elementCount <= 6; elementCount++)
                {
                    var keyValues = Enumerable.Range(0, (Int32)Math.Pow(10, elementCount))
                        .Select(i => new HeapKey(Guid.NewGuid(), rnd.Next(0, 10)))
                        .ToDictionary(k => k);
                    var heap = new SortedDictionary<HeapKey, HeapKey>(keyValues);
    
                    _compareCount = 0;
                    _enableCompareCount = true;
                    var min = heap.First().Key;
                    _enableCompareCount = false;
                    Console.WriteLine("Element count: {0}; Compare count for getMinElement: {1}",
                                      (Int32)Math.Pow(10, elementCount),
                                      _compareCount);
    
                    _compareCount = 0;
                    _enableCompareCount = true;
                    heap.Remove(min);
                    _enableCompareCount = false;
                    Console.WriteLine("Element count: {0}; Compare count for deleteMinElement: {1}",
                                      (Int32)Math.Pow(10, elementCount),
                                      _compareCount);
                }
    
                Console.ReadKey();
            }
    
            private class HeapKey : IComparable<HeapKey>
            {
                public HeapKey(Guid id, Int32 value)
                {
                    Id = id;
                    Value = value;
                }
    
                public Guid Id { get; private set; }
                public Int32 Value { get; private set; }
    
                public int CompareTo(HeapKey other)
                {
                    if (_enableCompareCount)
                    {
                        ++_compareCount;
                    }
    
                    if (other == null)
                    {
                        throw new ArgumentNullException("other");
                    }
    
                    var result = Value.CompareTo(other.Value);
    
                    return result == 0 ? Id.CompareTo(other.Id) : result;
                }
            }
        }
    }
    
    结果:

    关于c# - .NET中的堆类,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2231796/

    10-11 02:15