I need to save a collection of nodes:
class Node { int Value;
I have three requirements:
- You must be able to efficiently retrieve the node with the lowest value in the collection
- You must be able to effectively insert node into the collection.
- Two nodes can have the same value.
I thought the best collection for this would be a sort of sorted list. Thus, requirement No. 1 is fulfilled efficiently by simply taking the first item from a sorted list. Requirement No. 2 is met efficiently by inserting a new node at the desired location in the list.
But .Net's SortedList collection is similar to SortedDictionary and requires the key to be sorted to be unique, which violates requirement # 3.
It seems that .Net does not have a collection that satisfies these requirements, mainly because existing self-distribution collections require keys to be sorted in order to be unique. What is the reason for this? I suppose this cannot be oversight. What I donβt understand here? I can find similar questions about this, but they usually include someone offering a SortList , after which awareness of this does not work, and then the conversation disappears without a standard solution. At least if someone said: "There is no collection in C # for this task, you need to hack something together," which would be the answer.
Is it possible to use a regular List<Node> and re-sort the list whenever a new node is added? It seems like this would not be as effective as inserting the node in the right place to start. Perhaps this is what I should do? Manually iterate over the list until I find a place to insert a new node myself?
source share