Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 596639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:11:35+00:00 2026-05-13T16:11:35+00:00

Possible Duplicate: Fibonacci, Binary, or Binomial heap in c#? Is there any class like

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T16:11:36+00:00Added an answer on May 13, 2026 at 4:11 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: “BigInt” in C? Hey there! I'm calculating the Fibonacci numbers in C
Possible Duplicate: .NET - What’s the best way to implement a catch all exceptions
Possible Duplicate: Python program to find fibonacci series. More Pythonic way. Hey, i was
Possible Duplicate: Overloading += in c++ Do I need to overload the += operator
Possible Duplicate: string1 >= string2 not implemented in Linq to SQL, any workarround? To
Possible Duplicate: Why does “abcd”.StartsWith(“”) return true? Whilst debugging through some code I found
Possible Duplicate: Why not use tables for layout in HTML? Under what conditions should
Possible Duplicate: NAnt or MSBuild, which one to choose and when? What is the
Possible Duplicate: How do I calculate someone's age in C#? Maybe this could be
Possible Duplicate: What Ruby IDE do you prefer? I've generally been doing stuff on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.