-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy pathTrieMap.cs
493 lines (422 loc) · 19 KB
/
TrieMap.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
using DSA.DataStructures.Lists;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace DSA.DataStructures.Trees
{
/// <summary>
/// Represents a Trie Map(Prefix Tree Map).
/// </summary>
public class TrieMap<TValue> : IEnumerable<KeyValuePair<string, TValue>>
{
/// <summary>
/// Gets the tree root of the <see cref="TrieMap{TValue}"/>.
/// </summary>
public TrieMapNode<TValue> Root { get; internal set; }
/// <summary>
/// Gets the number of words in the <see cref="TrieMap{TValue}"/>.
/// </summary>
public int Count { get; internal set; }
/// <summary>
/// Gets or sets the value associated with the specified word in the <see cref="TrieMap{TValue}"/>.
/// </summary>
/// <param name="word">The word of the value to get or set.</param>
/// <returns>The value associated with the specified word. If the specified word is not found,
/// the get operation throws a <see cref="KeyNotFoundException"/>, and
/// the set operation creates a new element with the specified word.</returns>
public virtual TValue this[string word]
{
get
{
TValue value;
if (TryGetValue(word, out value))
return value;
else
throw new KeyNotFoundException("The word \"" + word + "\" was not found in the TrieMap.");
}
set
{
var curNode = Root;
bool wordWasAlreadyAdded = false;
for (int i = 0; i < word.Length; i++)
{
if (curNode.children.ContainsKey(word[i]))
{
if (i == word.Length - 1)
{
var child = curNode.children[word[i]];
wordWasAlreadyAdded = child.IsTerminal;
child.IsTerminal = true;
child.Value = value;
}
else
curNode = curNode.children[word[i]];
}
else
{
if (i == word.Length - 1)
{
var newNode = new TrieMapNode<TValue>(word[i], value, true);
curNode.children.Add(word[i], newNode);
}
else
{
var newNode = new TrieMapNode<TValue>(word[i], default(TValue), false);
curNode.children.Add(word[i], newNode);
curNode = curNode.children[word[i]];
}
}
}
if (!wordWasAlreadyAdded) Count++;
}
}
/// <summary>
/// Creates a new instance of the <see cref="TrieMap{TValue}"/> class.
/// </summary>
public TrieMap()
{
Root = new TrieMapNode<TValue>(default(char), default(TValue), false);
}
/// <summary>
/// Adds a word and a value to it to the <see cref="TrieMap{TValue}"/>.
/// </summary>
/// <param name="word">The word to add.</param>
/// <param name="value">The value to add.</param>
public void Add(string word, TValue value)
{
var curNode = Root;
bool wordWasAlreadyAdded = false;
for (int i = 0; i < word.Length; i++)
{
if (curNode.children.ContainsKey(word[i]))
{
if (i == word.Length - 1)
{
var child = curNode.children[word[i]];
wordWasAlreadyAdded = child.IsTerminal;
child.IsTerminal = true;
child.Value = value;
}
else
curNode = curNode.children[word[i]];
}
else
{
if (i == word.Length - 1)
{
var newNode = new TrieMapNode<TValue>(word[i], value, true);
curNode.children.Add(word[i], newNode);
}
else
{
var newNode = new TrieMapNode<TValue>(word[i], default(TValue), false);
curNode.children.Add(word[i], newNode);
curNode = curNode.children[word[i]];
}
}
}
if (!wordWasAlreadyAdded) Count++;
}
/// <summary>
/// Removes a word from the <see cref="TrieMap{TValue}"/>.
/// </summary>
/// <param name="word">The word to remove.</param>
/// <returns>true if the word is successfully removed; otherwise false. Also returns false if word is not found.</returns>
public bool Remove(string word)
{
var curNode = Root;
bool removedSuccessfully = true;
if (!ContainsWord(word)) return false;
var nodesList = new SinglyLinkedList<TrieMapNode<TValue>>();
nodesList.AddFirst(curNode);
for (int i = 0; i < word.Length; i++)
{
curNode = curNode.children[word[i]];
nodesList.AddFirst(curNode);
}
if (nodesList.First.Value.children.Count > 0)
{
nodesList.First.Value.IsTerminal = false;
Count--;
return true;
}
var curListNode = nodesList.First;
while (curListNode.Next != null)
{
if (curListNode.Value.children.Count == 0)
{
if (!curListNode.Next.Value.children.Remove(curListNode.Value.Key))
removedSuccessfully = false;
curListNode = curListNode.Next;
}
else break;
}
if (removedSuccessfully) Count--;
return removedSuccessfully;
}
/// <summary>
/// Determines whether a word is in the <see cref="TrieMap{TValue}"/>.
/// </summary>
/// <param name="word">The word to check.</param>
/// <returns>true if word is found; otherwise false.</returns>
public bool ContainsWord(string word)
{
var curNode = Root;
for (int i = 0; i < word.Length; i++)
{
if (curNode.children.ContainsKey(word[i]))
{
if (i == word.Length - 1)
{
return curNode.children[word[i]].IsTerminal;
}
else
curNode = curNode.children[word[i]];
}
else return false;
}
return false;
}
/// <summary>
/// Determines whether a value is in the <see cref="TrieMap{TValue}"/>.
/// </summary>
/// <param name="value">The value to check.</param>
/// <returns>true if value is found; otherwise false.</returns>
public bool ContainsValue(TValue value)
{
// Stack containing the nodes for traversal
var nodesStack = new Stack<TrieMapNode<TValue>>();
// Stack containing the level of the current node
var levelStack = new Stack<int>();
var curNode = Root;
// Adding nodes in the stack
foreach (var kvp in curNode.children)
{
nodesStack.Push(kvp.Value);
levelStack.Push(1);
}
var curWord = new StringBuilder();
while (nodesStack.Count != 0)
{
curNode = nodesStack.Pop();
var curLevel = levelStack.Pop();
// if the current word lenght is higher that the node level
// we have to trim the lenght to the previous level
// Note: this happens when we traverse all the nodes in a level
// and drop to a lower level again
if (curWord.Length >= curLevel)
curWord.Length = curLevel - 1;
// Add the current node character to the word
curWord.Append(curNode.Key);
// If we have a complete word we check for its value
if (curNode.IsTerminal)
{
if (object.Equals(curNode.Value, value)) return true;
}
// Adding nodes in the stack
foreach (var kvp in curNode.children)
{
nodesStack.Push(kvp.Value);
levelStack.Push(curLevel + 1);
}
}
return false;
}
/// <summary>
/// Gets the value associated with the specified word in the <see cref="TrieMap{TValue}"/>.
/// </summary>
/// <param name="word">The word of the value to get.</param>
/// <param name="value">When element with the specified word is found, the value of the element; otherwise, the default value of the value type.</param>
/// <returns>true if the <see cref="TrieMap{TValue}"/> contains an element with the specified word; otherwise, false.</returns>
public virtual bool TryGetValue(string word, out TValue value)
{
value = default(TValue);
var curNode = Root;
for (int i = 0; i < word.Length; i++)
{
if (curNode.children.ContainsKey(word[i]))
{
if (i == word.Length - 1)
{
if (curNode.children[word[i]].IsTerminal)
{
value = curNode.children[word[i]].Value;
return true;
}
else return false;
}
else
curNode = curNode.children[word[i]];
}
else return false;
}
return false;
}
/// <summary>
/// Determines whether a prefix is in the <see cref="TrieMap{TValue}"/>.
/// </summary>
/// <param name="prefix">The prefix to check.</param>
/// <returns>true if prefix is found; otherwise false.</returns>
public bool ContainsPrefix(string prefix)
{
var curNode = Root;
for (int i = 0; i < prefix.Length; i++)
{
if (curNode.children.ContainsKey(prefix[i]))
{
if (i == prefix.Length - 1)
{
return true;
}
else
curNode = curNode.children[prefix[i]];
}
else return false;
}
return false;
}
/// <summary>
/// Removes all words from the <see cref="TrieMap{TValue}"/>.
/// </summary>
public void Clear()
{
Root = new TrieMapNode<TValue>(default(char), default(TValue), false);
Count = 0;
}
/// <summary>
/// Returns an <see cref="IEnumerable{T}"/>(T being <see cref="KeyValuePair{TKey, TValue}"/> with key of type <see cref="string"/> and value of type TValue) of the words in the <see cref="TrieMap{TValue}"/> beginning with the given prefix sorted lexicographically.
/// </summary>
/// <param name="prefix">The prefix of the requested words.</param>
/// <returns>Returns the words sorted in lexicographical order.</returns>
public IEnumerable<KeyValuePair<string, TValue>> GetWordsByPrefix(string prefix)
{
return GetWordsByPrefix(prefix, Comparer<char>.Default);
}
/// <summary>
/// Returns an <see cref="IEnumerable{T}"/>(T being <see cref="KeyValuePair{TKey, TValue}"/> with key of type <see cref="string"/> and value of type TValue) of the words in the <see cref="TrieMap{TValue}"/> beginning with the given prefix sorted by the given comparer.
/// </summary>
/// <param name="prefix">The prefix of the requested words.</param>
/// <param name="comparer">The comparer which is used for sorting.</param>
/// <returns>Returns the words sorted with the given comparer.</returns>
public IEnumerable<KeyValuePair<string, TValue>> GetWordsByPrefix(string prefix, Comparer<char> comparer)
{
if (ContainsPrefix(prefix))
{
// Stack containing the nodes for traversal
var nodesStack = new Stack<TrieMapNode<TValue>>();
// Stack containing the level of the current node
var levelStack = new Stack<int>();
var curNode = Root;
var curWord = new StringBuilder();
// Finding last node of the prefix and adding the characters
// to the current word
for (int i = 0; i < prefix.Length; i++)
{
curNode = curNode.children[prefix[i]];
curWord.Append(prefix[i]);
}
int prefixLength = prefix.Length;
// Code below is the same as the GetEnumerator() method except we are using
// the given comparer instead of the default compare method. Normal DFS
// begging from the last node of the prefix.
var sortedChildren = new SortedDictionary<char, TrieMapNode<TValue>>(curNode.children,
Comparer<char>.Create((x, y) => comparer.Compare(y, x)));
foreach (var kvp in sortedChildren)
{
nodesStack.Push(kvp.Value);
// We set the level of the nodes after the last node of the prefix
levelStack.Push(prefixLength + 1);
}
while (nodesStack.Count != 0)
{
curNode = nodesStack.Pop();
var curLevel = levelStack.Pop();
if (curWord.Length >= curLevel)
curWord.Length = curLevel - 1;
curWord.Append(curNode.Key);
if (curNode.IsTerminal)
yield return new KeyValuePair<string, TValue>(curWord.ToString(), curNode.Value);
sortedChildren = new SortedDictionary<char, TrieMapNode<TValue>>(curNode.children,
Comparer<char>.Create((x, y) => comparer.Compare(y, x)));
foreach (var kvp in sortedChildren)
{
nodesStack.Push(kvp.Value);
levelStack.Push(curLevel + 1);
}
}
}
}
/// <summary>
/// Returns an <see cref="IEnumerable{T}"/>(T being <see cref="KeyValuePair{TKey, TValue}"/> with key of type <see cref="string"/> and value of type TValue) of all words in the <see cref="TrieMap{TValue}"/> sorted in lexicographical order.
/// </summary>
/// <returns>Returns the words sorted in lexicographical order.</returns>
public IEnumerable<KeyValuePair<string, TValue>> GetAllWords()
{
return GetAllWords(Comparer<char>.Default);
}
/// <summary>
/// Returns an <see cref="IEnumerable{T}"/>(T being <see cref="KeyValuePair{TKey, TValue}"/> with key of type <see cref="string"/> and value of type TValue) of all words in the <see cref="TrieMap{TValue}"/> sorted by the given comparer.
/// </summary>
/// <param name="comparer">The comparer which is used for sorting.</param>
/// <returns>Returns the words sorted with the given comparer.</returns>
public IEnumerable<KeyValuePair<string, TValue>> GetAllWords(Comparer<char> comparer)
{
// Stack containing the nodes for traversal
var nodesStack = new Stack<TrieMapNode<TValue>>();
// Stack containing the level of the current node
var levelStack = new Stack<int>();
var curNode = Root;
// Creating reversed sorted dictionary for adding into the stack of nodes
var sortedChildren = new SortedDictionary<char, TrieMapNode<TValue>>(curNode.children,
Comparer<char>.Create((x, y) => comparer.Compare(y, x)));
// Adding nodes in the stack
foreach (var kvp in sortedChildren)
{
nodesStack.Push(kvp.Value);
levelStack.Push(1);
}
var curWord = new StringBuilder();
while (nodesStack.Count != 0)
{
curNode = nodesStack.Pop();
var curLevel = levelStack.Pop();
// if the current word lenght is higher that the node level
// we have to trim the lenght to the previous level
// Note: this happens when we traverse all the nodes in a level
// and drop to a lower level again
if (curWord.Length >= curLevel)
curWord.Length = curLevel - 1;
// Add the current node character to the word
curWord.Append(curNode.Key);
// If we have a complete word we output it
if (curNode.IsTerminal)
yield return new KeyValuePair<string, TValue>(curWord.ToString(), curNode.Value);
// Creating reversed sorted dictionary for adding into the stack of nodes
sortedChildren = new SortedDictionary<char, TrieMapNode<TValue>>(curNode.children,
Comparer<char>.Create((x, y) => comparer.Compare(y, x)));
// Adding nodes in the stack
foreach (var kvp in sortedChildren)
{
nodesStack.Push(kvp.Value);
levelStack.Push(curLevel + 1);
}
}
// Note: We add the nodes in reversed order because the stack
// is LIFO(last in first out) data structure so everything is outputted
// in reversed order. We do this because we need this feature(LIFO)
// of the stack to create a DFS(depth first search)
}
/// <summary>
/// Returns an enumerator that iterates lexicographically through the <see cref="TrieMap{TValue}"/>.
/// </summary>
/// <returns>Returns the words sorted in lexicographical order.</returns>
public IEnumerator<KeyValuePair<string, TValue>> GetEnumerator()
{
return GetAllWords(Comparer<char>.Default).GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}