.NET中的堆类

Tom*_*ski 19 .net c# heap

可能重复:
c#中的Fibonacci,Binary或Binomial堆?

在.NET中是否有像堆这样的类?我需要某种收集,我可以从中检索最小值.元件.我只想要3个方法:

  • 加()
  • RemoveMinElement()
  • GetMinElement()

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

cod*_*zen 10

您可以使用自定义键使用SortedListSortedDictionary(请参阅下面的讨论).如果您使用具有引用相等性的类型,但可以根据您关注的值进行比较,那么这可能会起作用.

像这样的东西:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

下面是使用具有二进制堆性能特征的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;
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

结果:

元素数:100; 比较getMinElement的计数:0

元素数:100; 比较deleteMinElement的计数:8

元素数:1000; 比较getMinElement的计数:0

元素数:1000; 比较deleteMinElement的计数:10

元素数:10000; 比较getMinElement的计数:0

元素数:10000; 比较deleteMinElement的计数:13

元素数:100000; 比较getMinElement的计数:0

元素数:100000; 比较deleteMinElement的计数:14

元素数:1000000; 比较getMinElement的计数:0

元素数:1000000; 比较deleteMinElement的计数:21

  • 没有堆是一个实现!=二进制(搜索)树.请参见http://en.wikipedia.org/wiki/Priority_queue#Simple_implementations (8认同)
  • SortedList过度杀死,并且性能特征会更差. (3认同)
  • @codek,SortedList将具有O(n)或O(nlog(n))性能,PQ将在O(log(n))中添加/删除 (3认同)
  • 我并不是说二进制堆适合所有应用程序。我的意思是,当您想要堆性能特征时(正如OP所做的那样),SortedList是错误的。我相信 SortedDictionary 这里也是错误的。它/不/使用二进制堆实现,也没有声称是。相反,它使用二叉搜索树。本质区别之一是 SortedDictionary 不能在常数时间内完成 getMinElement。 (2认同)

Ed *_*wer 6

优先级队列看起来很适合您的问题: .Net 中的优先级队列

Google 搜索“C# 优先级队列”以获取更多实现。