@@ -8,18 +8,18 @@ namespace Advanced.Algorithms.DataStructures
8
8
/// A sorted Dictionary implementation using balanced binary search tree. IEnumerable will enumerate in sorted order.
9
9
/// 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).
10
10
/// </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
14
14
{
15
15
//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 ;
17
17
18
18
public int Count => binarySearchTree . Count ;
19
19
20
20
public SortedDictionary ( )
21
21
{
22
- binarySearchTree = new RedBlackTree < SortedDictionaryNode < TK , TV > > ( ) ;
22
+ binarySearchTree = new RedBlackTree < SortedDictionaryNode < K , V > > ( ) ;
23
23
}
24
24
25
25
/// <summary>
@@ -28,29 +28,29 @@ public SortedDictionary()
28
28
/// </summary>
29
29
/// <param name="key">The key to check.</param>
30
30
/// <returns>True if this dictionary contains the given key.</returns>
31
- public bool ContainsKey ( TK key )
31
+ public bool ContainsKey ( K key )
32
32
{
33
- return binarySearchTree . HasItem ( new SortedDictionaryNode < TK , TV > ( key , default ( TV ) ) ) ;
33
+ return binarySearchTree . HasItem ( new SortedDictionaryNode < K , V > ( key , default ( V ) ) ) ;
34
34
}
35
35
36
36
/// <summary>
37
37
/// Add a new value for given key.
38
38
/// Time complexity: O(log(n)).
39
39
/// </summary>
40
- public void Add ( TK key , TV value )
40
+ public void Add ( K key , V value )
41
41
{
42
- binarySearchTree . Insert ( new SortedDictionaryNode < TK , TV > ( key , value ) ) ;
42
+ binarySearchTree . Insert ( new SortedDictionaryNode < K , V > ( key , value ) ) ;
43
43
}
44
44
45
45
/// <summary>
46
46
/// Get/set value for given key.
47
47
/// Time complexity: O(log(n)).
48
48
/// </summary>
49
- public TV this [ TK key ]
49
+ public V this [ K key ]
50
50
{
51
51
get
52
52
{
53
- var node = binarySearchTree . FindNode ( new SortedDictionaryNode < TK , TV > ( key , default ( TV ) ) ) ;
53
+ var node = binarySearchTree . FindNode ( new SortedDictionaryNode < K , V > ( key , default ( V ) ) ) ;
54
54
if ( node == null )
55
55
{
56
56
throw new Exception ( "Key not found." ) ;
@@ -73,43 +73,43 @@ public TV this[TK key]
73
73
/// Remove the given key.
74
74
/// Time complexity: O(log(n)).
75
75
/// </summary>
76
- public void Remove ( TK key )
76
+ public void Remove ( K key )
77
77
{
78
- binarySearchTree . Delete ( new SortedDictionaryNode < TK , TV > ( key , default ( TV ) ) ) ;
78
+ binarySearchTree . Delete ( new SortedDictionaryNode < K , V > ( key , default ( V ) ) ) ;
79
79
}
80
80
81
81
/// <summary>
82
82
/// Return the next higher key-value pair after given key in this dictionary.
83
83
/// Time complexity: O(log(n)).
84
84
/// </summary>
85
85
/// <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 )
87
87
{
88
- var next = binarySearchTree . NextHigher ( new SortedDictionaryNode < TK , TV > ( key , default ( TV ) ) ) ;
88
+ var next = binarySearchTree . NextHigher ( new SortedDictionaryNode < K , V > ( key , default ( V ) ) ) ;
89
89
90
90
if ( next == null )
91
91
{
92
- return default ( KeyValuePair < TK , TV > ) ;
92
+ return default ( KeyValuePair < K , V > ) ;
93
93
}
94
94
95
- return new KeyValuePair < TK , TV > ( next . Key , next . Value ) ;
95
+ return new KeyValuePair < K , V > ( next . Key , next . Value ) ;
96
96
}
97
97
98
98
/// <summary>
99
99
/// Return the next lower key-value pair before given key in this dictionary.
100
100
/// Time complexity: O(log(n)).
101
101
/// </summary>
102
102
/// <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 )
104
104
{
105
- var prev = binarySearchTree . NextLower ( new SortedDictionaryNode < TK , TV > ( key , default ( TV ) ) ) ;
105
+ var prev = binarySearchTree . NextLower ( new SortedDictionaryNode < K , V > ( key , default ( V ) ) ) ;
106
106
107
107
if ( prev == null )
108
108
{
109
- return default ( KeyValuePair < TK , TV > ) ;
109
+ return default ( KeyValuePair < K , V > ) ;
110
110
}
111
111
112
- return new KeyValuePair < TK , TV > ( prev . Key , prev . Value ) ;
112
+ return new KeyValuePair < K , V > ( prev . Key , prev . Value ) ;
113
113
}
114
114
115
115
/// <summary>
@@ -126,27 +126,27 @@ IEnumerator IEnumerable.GetEnumerator()
126
126
return GetEnumerator ( ) ;
127
127
}
128
128
129
- public IEnumerator < KeyValuePair < TK , TV > > GetEnumerator ( )
129
+ public IEnumerator < KeyValuePair < K , V > > GetEnumerator ( )
130
130
{
131
- return new SortedDictionaryEnumerator < TK , TV > ( binarySearchTree ) ;
131
+ return new SortedDictionaryEnumerator < K , V > ( binarySearchTree ) ;
132
132
}
133
133
}
134
134
135
- internal class SortedDictionaryNode < TK , TV > : IComparable
136
- where TK : IComparable
135
+ internal class SortedDictionaryNode < K , V > : IComparable
136
+ where K : IComparable
137
137
{
138
- internal TK Key { get ; }
139
- internal TV Value { get ; set ; }
138
+ internal K Key { get ; }
139
+ internal V Value { get ; set ; }
140
140
141
- internal SortedDictionaryNode ( TK key , TV value )
141
+ internal SortedDictionaryNode ( K key , V value )
142
142
{
143
143
this . Key = key ;
144
144
this . Value = value ;
145
145
}
146
146
147
147
public int CompareTo ( object obj )
148
148
{
149
- if ( obj is SortedDictionaryNode < TK , TV > itemToComare )
149
+ if ( obj is SortedDictionaryNode < K , V > itemToComare )
150
150
{
151
151
return Key . CompareTo ( itemToComare . Key ) ;
152
152
}
@@ -155,12 +155,12 @@ public int CompareTo(object obj)
155
155
}
156
156
}
157
157
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
159
159
{
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 ;
162
162
163
- internal SortedDictionaryEnumerator ( RedBlackTree < SortedDictionaryNode < TK , TV > > bst )
163
+ internal SortedDictionaryEnumerator ( RedBlackTree < SortedDictionaryNode < K , V > > bst )
164
164
{
165
165
this . bst = bst ;
166
166
this . enumerator = bst . GetEnumerator ( ) ;
@@ -178,11 +178,11 @@ public void Reset()
178
178
179
179
object IEnumerator . Current => Current ;
180
180
181
- public KeyValuePair < TK , TV > Current
181
+ public KeyValuePair < K , V > Current
182
182
{
183
183
get
184
184
{
185
- return new KeyValuePair < TK , TV > ( enumerator . Current . Key , enumerator . Current . Value ) ;
185
+ return new KeyValuePair < K , V > ( enumerator . Current . Key , enumerator . Current . Value ) ;
186
186
}
187
187
}
188
188
0 commit comments