.NET 中的堆类

发布于 2024-08-21 00:24:58 字数 397 浏览 6 评论 0原文

可能的重复:
C# 中的斐波那契、二元或二项式堆?

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

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

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

Possible Duplicate:
Fibonacci, Binary, or Binomial heap in c#?

Is there any class like heap in .NET?
I need some kind of collection from which I can retrieve min. element. I just want 3 methods:

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

I can't use sorted list because there keys has to be unique, and I might have several identical elements.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫烠⑼条掵仅有一顆心 2024-08-28 00:24:58

您可以使用 SortedList< /a> 或 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;
            }
        }
    }
}

结果:

元素数量: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

You could use SortedList or a SortedDictionary (see discussion below) with a custom key. If you used a type with referential equality, but could be compared based on the value you care about, then this could work.

Something like this:

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;
    }
}

Here is a working example of using a SortedDictionary which has binary-heap performance characteristics:

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;
            }
        }
    }
}

Results:

Element count: 100; Compare count for getMinElement: 0

Element count: 100; Compare count for deleteMinElement: 8

Element count: 1000; Compare count for getMinElement: 0

Element count: 1000; Compare count for deleteMinElement: 10

Element count: 10000; Compare count for getMinElement: 0

Element count: 10000; Compare count for deleteMinElement: 13

Element count: 100000; Compare count for getMinElement: 0

Element count: 100000; Compare count for deleteMinElement: 14

Element count: 1000000; Compare count for getMinElement: 0

Element count: 1000000; Compare count for deleteMinElement: 21

救赎№ 2024-08-28 00:24:58

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

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

Priority Queues look like a good fit to your problem:
Priority queue in .Net

Google for "C# priority queues" for more implementations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文