@@ -8,18 +8,18 @@ namespace Advanced.Algorithms.DataStructures
88 /// A sorted Dictionary implementation using balanced binary search tree. IEnumerable will enumerate in sorted order.
99 /// This may be better than regular Dictionary implementation which can give o(K) in worst case (but O(1) amortized when collisions K is avoided).
1010 /// </summary>
11- /// <typeparam name="TK ">The key datatype.</typeparam>
12- /// <typeparam name="TV ">The value datatype.</typeparam>
13- public class SortedDictionary < TK , TV > : IEnumerable < KeyValuePair < TK , TV > > where TK : IComparable
11+ /// <typeparam name="K ">The key datatype.</typeparam>
12+ /// <typeparam name="V ">The value datatype.</typeparam>
13+ public class SortedDictionary < K , V > : IEnumerable < KeyValuePair < K , V > > where K : IComparable
1414 {
1515 //use red-black tree as our balanced BST since it gives good performance for both deletion/insertion
16- private readonly RedBlackTree < SortedDictionaryNode < TK , TV > > binarySearchTree ;
16+ private readonly RedBlackTree < SortedDictionaryNode < K , V > > binarySearchTree ;
1717
1818 public int Count => binarySearchTree . Count ;
1919
2020 public SortedDictionary ( )
2121 {
22- binarySearchTree = new RedBlackTree < SortedDictionaryNode < TK , TV > > ( ) ;
22+ binarySearchTree = new RedBlackTree < SortedDictionaryNode < K , V > > ( ) ;
2323 }
2424
2525 /// <summary>
@@ -28,29 +28,29 @@ public SortedDictionary()
2828 /// </summary>
2929 /// <param name="key">The key to check.</param>
3030 /// <returns>True if this dictionary contains the given key.</returns>
31- public bool ContainsKey ( TK key )
31+ public bool ContainsKey ( K key )
3232 {
33- return binarySearchTree . HasItem ( new SortedDictionaryNode < TK , TV > ( key , default ( TV ) ) ) ;
33+ return binarySearchTree . HasItem ( new SortedDictionaryNode < K , V > ( key , default ( V ) ) ) ;
3434 }
3535
3636 /// <summary>
3737 /// Add a new value for given key.
3838 /// Time complexity: O(log(n)).
3939 /// </summary>
40- public void Add ( TK key , TV value )
40+ public void Add ( K key , V value )
4141 {
42- binarySearchTree . Insert ( new SortedDictionaryNode < TK , TV > ( key , value ) ) ;
42+ binarySearchTree . Insert ( new SortedDictionaryNode < K , V > ( key , value ) ) ;
4343 }
4444
4545 /// <summary>
4646 /// Get/set value for given key.
4747 /// Time complexity: O(log(n)).
4848 /// </summary>
49- public TV this [ TK key ]
49+ public V this [ K key ]
5050 {
5151 get
5252 {
53- var node = binarySearchTree . FindNode ( new SortedDictionaryNode < TK , TV > ( key , default ( TV ) ) ) ;
53+ var node = binarySearchTree . FindNode ( new SortedDictionaryNode < K , V > ( key , default ( V ) ) ) ;
5454 if ( node == null )
5555 {
5656 throw new Exception ( "Key not found." ) ;
@@ -73,43 +73,43 @@ public TV this[TK key]
7373 /// Remove the given key.
7474 /// Time complexity: O(log(n)).
7575 /// </summary>
76- public void Remove ( TK key )
76+ public void Remove ( K key )
7777 {
78- binarySearchTree . Delete ( new SortedDictionaryNode < TK , TV > ( key , default ( TV ) ) ) ;
78+ binarySearchTree . Delete ( new SortedDictionaryNode < K , V > ( key , default ( V ) ) ) ;
7979 }
8080
8181 /// <summary>
8282 /// Return the next higher key-value pair after given key in this dictionary.
8383 /// Time complexity: O(log(n)).
8484 /// </summary>
8585 /// <returns>Null if the given key does'nt exist or next key does'nt exist.</returns>
86- public KeyValuePair < TK , TV > NextHigher ( TK key )
86+ public KeyValuePair < K , V > NextHigher ( K key )
8787 {
88- var next = binarySearchTree . NextHigher ( new SortedDictionaryNode < TK , TV > ( key , default ( TV ) ) ) ;
88+ var next = binarySearchTree . NextHigher ( new SortedDictionaryNode < K , V > ( key , default ( V ) ) ) ;
8989
9090 if ( next == null )
9191 {
92- return default ( KeyValuePair < TK , TV > ) ;
92+ return default ( KeyValuePair < K , V > ) ;
9393 }
9494
95- return new KeyValuePair < TK , TV > ( next . Key , next . Value ) ;
95+ return new KeyValuePair < K , V > ( next . Key , next . Value ) ;
9696 }
9797
9898 /// <summary>
9999 /// Return the next lower key-value pair before given key in this dictionary.
100100 /// Time complexity: O(log(n)).
101101 /// </summary>
102102 /// <returns>Null if the given key does'nt exist or previous key does'nt exist.</returns>
103- public KeyValuePair < TK , TV > NextLower ( TK key )
103+ public KeyValuePair < K , V > NextLower ( K key )
104104 {
105- var prev = binarySearchTree . NextLower ( new SortedDictionaryNode < TK , TV > ( key , default ( TV ) ) ) ;
105+ var prev = binarySearchTree . NextLower ( new SortedDictionaryNode < K , V > ( key , default ( V ) ) ) ;
106106
107107 if ( prev == null )
108108 {
109- return default ( KeyValuePair < TK , TV > ) ;
109+ return default ( KeyValuePair < K , V > ) ;
110110 }
111111
112- return new KeyValuePair < TK , TV > ( prev . Key , prev . Value ) ;
112+ return new KeyValuePair < K , V > ( prev . Key , prev . Value ) ;
113113 }
114114
115115 /// <summary>
@@ -126,27 +126,27 @@ IEnumerator IEnumerable.GetEnumerator()
126126 return GetEnumerator ( ) ;
127127 }
128128
129- public IEnumerator < KeyValuePair < TK , TV > > GetEnumerator ( )
129+ public IEnumerator < KeyValuePair < K , V > > GetEnumerator ( )
130130 {
131- return new SortedDictionaryEnumerator < TK , TV > ( binarySearchTree ) ;
131+ return new SortedDictionaryEnumerator < K , V > ( binarySearchTree ) ;
132132 }
133133 }
134134
135- internal class SortedDictionaryNode < TK , TV > : IComparable
136- where TK : IComparable
135+ internal class SortedDictionaryNode < K , V > : IComparable
136+ where K : IComparable
137137 {
138- internal TK Key { get ; }
139- internal TV Value { get ; set ; }
138+ internal K Key { get ; }
139+ internal V Value { get ; set ; }
140140
141- internal SortedDictionaryNode ( TK key , TV value )
141+ internal SortedDictionaryNode ( K key , V value )
142142 {
143143 this . Key = key ;
144144 this . Value = value ;
145145 }
146146
147147 public int CompareTo ( object obj )
148148 {
149- if ( obj is SortedDictionaryNode < TK , TV > itemToComare )
149+ if ( obj is SortedDictionaryNode < K , V > itemToComare )
150150 {
151151 return Key . CompareTo ( itemToComare . Key ) ;
152152 }
@@ -155,12 +155,12 @@ public int CompareTo(object obj)
155155 }
156156 }
157157
158- internal class SortedDictionaryEnumerator < TK , TV > : IEnumerator < KeyValuePair < TK , TV > > where TK : IComparable
158+ internal class SortedDictionaryEnumerator < K , V > : IEnumerator < KeyValuePair < K , V > > where K : IComparable
159159 {
160- private RedBlackTree < SortedDictionaryNode < TK , TV > > bst ;
161- private IEnumerator < SortedDictionaryNode < TK , TV > > enumerator ;
160+ private RedBlackTree < SortedDictionaryNode < K , V > > bst ;
161+ private IEnumerator < SortedDictionaryNode < K , V > > enumerator ;
162162
163- internal SortedDictionaryEnumerator ( RedBlackTree < SortedDictionaryNode < TK , TV > > bst )
163+ internal SortedDictionaryEnumerator ( RedBlackTree < SortedDictionaryNode < K , V > > bst )
164164 {
165165 this . bst = bst ;
166166 this . enumerator = bst . GetEnumerator ( ) ;
@@ -178,11 +178,11 @@ public void Reset()
178178
179179 object IEnumerator . Current => Current ;
180180
181- public KeyValuePair < TK , TV > Current
181+ public KeyValuePair < K , V > Current
182182 {
183183 get
184184 {
185- return new KeyValuePair < TK , TV > ( enumerator . Current . Key , enumerator . Current . Value ) ;
185+ return new KeyValuePair < K , V > ( enumerator . Current . Key , enumerator . Current . Value ) ;
186186 }
187187 }
188188
0 commit comments