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

Ask A Question

Stats

  • Questions 379k
  • Answers 379k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Why don't you make some fake buttons that don't do… May 14, 2026 at 9:30 pm
  • Editorial Team
    Editorial Team added an answer I figured it out. While my method of storing and… May 14, 2026 at 9:30 pm
  • Editorial Team
    Editorial Team added an answer Verify that the servers have the same indexes. Also check… May 14, 2026 at 9:30 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.