Why SortedSetT.GetViewBetween isn't Olog N?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
The `SortedSet`````<T>`````` class in C# is a collection that maintains elements in a sorted order. One of its useful methods is `GetViewBetween(T lowerValue, T upperValue)`, which provides a subset of the `SortedSet`````<T>`````` that contains only the elements greater than or equal to `lowerValue` and less than or equal to `upperValue`.
Intuition might suggest that `GetViewBetween` could operate in time because it deals with sorted data potentially backed by a tree structure like a red-black tree, where many operations have logarithmic complexity. However, `GetViewBetween` is not an operation, and understanding why requires diving deeper into its implementation and characteristics.
Analyzing `GetViewBetween`
The Internal Structure of `SortedSet`````<T>``````
The `SortedSet`````<T>`````` in C# is typically implemented as a balanced binary search tree, specifically a red-black tree. Each operation—like `Add`, `Remove`, and `Contains`—can be performed in time due to the balanced nature of the tree. However, `GetViewBetween` is a more complex operation that goes beyond a simple tree traversal or update.
Complexity of `GetViewBetween`
Although the elements can be accessed in order (left-to-right or in-order traversal), the view operation involves potentially complex mechanisms:
- Iterative Traversal: While inserting and searching involve a single path from the root to a leaf, creating a view requires moving through a range of elements. Even in a balanced tree, ensuring that all and only the correct elements are included requires a traversal which involves multiple paths.
- Construction of View: The method does not stop at identifying the range but needs to construct a subset, sharing the structure or creating a copy of elements. This involves additional overhead beyond traversal, requiring operations that do not scale logarithmically with only .
- Subsequent Operations: Once a view is obtained, any number of operations can be performed on it until it’s destroyed, potentially affecting internal nodes and requiring structural adjustments in the tree.
Estimated Performance
The `GetViewBetween` operation, therefore, involves more than simple node access due to these steps, translating to complexity as each element in the specified range can be involved in the returned view, rather than a single insertion or lookup operation.
Example Scenario
Consider a `SortedSet``<int>``` with the following elements: `[1, 3, 5, 7, 9, 11, 13, 15]`. When calling `GetViewBetween(3, 11)`:
- An iteration over elements starting from `3` and ending at `11` is needed.
- Each element must be checked, added to the view, or skipped, which takes linear time relative to the size of the returned subset.
Key Points Summary
| Aspect | Details |
| Data Structure | SortedSet`````\<T>`````` is typically implemented as a red-black tree, supporting $O(\log N)$` operations for single element actions. |
| Operation Complexity | GetViewBetween has time complexity due to range processing. |
| View Construction | Involves iterating through elements and constructing a view, beyond simple node access. |
| Path Traversal | Involves multiple paths through the tree, potentially accessing multiple nodes for complete range retrieval. |
| Real-world Implications | Suitable for smaller subsets where traversal overhead is acceptable but not efficient for very large ranges. |
Considerations and Alternatives
- Memory Usage: While `GetViewBetween` itself may not duplicate elements, subsequent operations on the view might affect performance.
- Use Cases: Ideal for scenarios where small ranges need to be managed efficiently without reallocation of large structures.
- Alternatives: For large data ranges, approaches such as databases or specialized data structures that support range queries with secondary indexes might be preferable.
Conclusion
The `GetViewBetween` function in `SortedSet`````<T>`````` is a valuable method for accessing element subsets, but it is crucial to understand its time complexity and performance characteristics. It's inherently an operation when considering real-world data structures within .NET, due to full-range iteration and view construction overhead. Recognizing these intricacies ensures proper use and expectation management when employing this method in software solutions.
Related reading
- Why spark.ml don't implement any of spark.mllib algorithms?
- Why the tree resulting from Kruskal is different from Dijkstra?
- Why there is no stdcopy_if algorithm?
- Why use binary search if there's ternary search?
- Why split app server into Read and Write servers?
- Why swapping is not a good idea in zookeeper and kafka?
- Why static classes cant implement interfaces?
- Why there are 5 Versions of Timer Classes in .NET?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.