How to get the first key that exceeds the specified key in SortedDictionary in O (log n)?

Let's say I have the following codes:

SortedDictionary<int,string> test = new SortedDictionary<int, string> ( ); test.Add ( 1, "one" ); test.Add ( 3, "three" ); test.Add ( 7, "seven" ); test.Add ( 8, "eight" ); int key = GetFirstKeyGreaterThan ( test, 3 ); // expects to get 7 int key2 = GetFirstKeyGreaterThan ( test, 6 ); // expects to get 7 

Is there an easy way to implement the GetFirstKeyGreaterThan method? I know that we can use the GetEnumerator method and call MoveNext after we reach the key of 3, but this will be O (n) operation.

I do not want to use SortedList because I need O (log n) to insert and delete keys.

+4
source share

Source: https://habr.com/ru/post/1442876/


All Articles