I know that I can use a Dictionary and retrieve an arbitrary element in O(1) time.
I know that I can get the next highest (or lowest) element in a SortedDictionary in O(1) time. But what if I wanted to remove the first value (based on TKey‘s IComparable) in the SortedDictionary?
Can I use the .First() method to retrieve the smallest key? And what is its complexity? Will it run in O(1), O(log n) or O(n) time?
Is the SortedDictionary the correct data structure for this?
Note: The use case is sort of a poor man’s priority queue or ordered queue. No open-source is allowed for this (must be code-from-scratch, or already in the .NET 3.5 framework).
SortedList and SortedDictionary are implemented internally as binary search trees and could ideally give you O(log n) performance for a Min (requires walking the tree, but not enumerating the whole list). However, using LINQ to perform that Min will probably enumerate the entire list.
I would consider a Skip List as a better alternative data structure.