forked from aalhour/C-Sharp-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Trie.cs
197 lines (159 loc) · 5.27 KB
/
Trie.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
/***
* Trie.
*
* This is the standard/vanilla implementation of a Trie. For an associative version of Trie, checkout the TrieMap<TRecord> class.
*
* This class implements the IEnumerable interface.
*/
using System;
using System.Linq;
using System.Collections.Generic;
namespace DataStructures.Trees
{
/// <summary>
/// The vanila Trie implementation.
/// </summary>
public class Trie : IEnumerable<String>
{
private int _count { get; set; }
private TrieNode _root { get; set; }
/// <summary>
/// CONSTRUCTOR
/// </summary>
public Trie()
{
_count = 0;
_root = new TrieNode(' ', false);
}
/// <summary>
/// Return count of words.
/// </summary>
public int Count
{
get { return _count; }
}
/// <summary>
/// Checks if element is empty.
/// </summary>
public bool IsEmpty
{
get { return _count == 0; }
}
/// <summary>
/// Add word to trie
/// </summary>
public void Add(string word)
{
if (string.IsNullOrEmpty(word))
throw new ArgumentException("Word is empty or null.");
var current = _root;
for (int i = 0; i < word.Length; ++i)
{
if (!current.Children.ContainsKey(word[i]))
{
var newTrieNode = new TrieNode(word[i]);
newTrieNode.Parent = current;
current.Children.Add(word[i], newTrieNode);
}
current = current.Children[word[i]];
}
if (current.IsTerminal)
throw new InvalidOperationException("Word already exists in Trie.");
++_count;
current.IsTerminal = true;
}
/// <summary>
/// Removes a word from the trie.
/// </summary>
public void Remove(string word)
{
if (string.IsNullOrEmpty(word))
throw new ArgumentException("Word is empty or null.");
var current = _root;
for (int i = 0; i < word.Length; ++i)
{
if (!current.Children.ContainsKey(word[i]))
throw new KeyNotFoundException("Word doesn't belong to trie.");
current = current.Children[word[i]];
}
if (!current.IsTerminal)
throw new KeyNotFoundException("Word doesn't belong to trie.");
--_count;
current.Remove();
}
/// <summary>
/// Checks whether the trie has a specific word.
/// </summary>
public bool ContainsWord(string word)
{
if (string.IsNullOrEmpty(word))
throw new InvalidOperationException("Word is either null or empty.");
var current = _root;
for (int i = 0; i < word.Length; ++i)
{
if (!current.Children.ContainsKey(word[i]))
return false;
current = current.Children[word[i]];
}
return current.IsTerminal;
}
/// <summary>
/// Checks whether the trie has a specific prefix.
/// </summary>
public bool ContainsPrefix(string prefix)
{
if (string.IsNullOrEmpty(prefix))
throw new InvalidOperationException("Prefix is either null or empty.");
var current = _root;
for (int i = 0; i < prefix.Length; ++i)
{
if (!current.Children.ContainsKey(prefix[i]))
return false;
current = current.Children[prefix[i]];
}
return true;
}
/// <summary>
/// Searches the entire trie for words that has a specific prefix.
/// </summary>
public IEnumerable<String> SearchByPrefix(string prefix)
{
if (string.IsNullOrEmpty(prefix))
throw new InvalidOperationException("Prefix is either null or empty.");
var current = _root;
for (int i = 0; i < prefix.Length; ++i)
{
if (!current.Children.ContainsKey(prefix[i]))
return null;
current = current.Children[prefix[i]];
}
return current.GetByPrefix();
}
/// <summary>
/// Clears this insance.
/// </summary>
public void Clear()
{
_count = 0;
_root.Clear();
_root = new TrieNode(' ', false);
}
#region IEnumerable<String> Implementation
/// <summary>
/// IEnumerable\<String\>.IEnumerator implementation.
/// </summary>
public IEnumerator<string> GetEnumerator()
{
return _root.GetTerminalChildren().Select(node => node.Word).GetEnumerator();
}
/// <summary>
/// IEnumerable\<String\>.IEnumerator implementation.
/// </summary>
/// <returns></returns>
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
#endregion IEnumerable<String> Implementation
}
}