forked from TheAlgorithms/C-Sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Trie.cs
124 lines (112 loc) · 4.33 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
using System;
using System.Collections.Generic;
namespace DataStructures.Tries
{
/// <summary>
/// A Trie is a data structure (particular case of m-ary tree) used to efficiently represent strings with common prefixes.
/// Originally posed by E. Fredkin in 1960.
/// Fredkin, Edward (Sept. 1960), "Trie Memory", Communications of the ACM 3 (9): 490-499.
/// Its name is due to retrieval because its main application is in the field of "Information Retrieval" (information retrieval).
/// </summary>
public class Trie
{
/// <summary>
/// This character marks the end of a string.
/// </summary>
private const char Mark = '$';
/// <summary>
/// This property represents the root node of the trie.
/// </summary>
private readonly TrieNode root;
/// <summary>
/// Initializes a new instance of the <see cref="Trie"/> class. This instances was created without text strings, generating the root node of the trie, without children.
/// </summary>
public Trie()
{
root = new TrieNode(Mark);
}
/// <summary>
/// Initializes a new instance of the <see cref="Trie"/> class. Given a set of text strings, each of those strings inserts them into the trie using the Insert (string) method.
/// </summary>
/// <param name="words">The array with text strings to insert in the trie.</param>
public Trie(IEnumerable<string> words)
: this()
{
foreach (string s in words)
{
Insert(s);
}
}
/// <summary>
/// Insert a string s to the trie. The $ mark is added to the end of the chain and then it is added, this in order to indicate the end of the chain in the trie.
/// </summary>
/// <param name="s">The string to insert into the trie.</param>
public void Insert(string s)
{
s += Mark;
int index = 0;
TrieNode match = PrefixQuery(s, ref index);
for (int i = index; i < s.Length; i++)
{
TrieNode t = new(s[i], match);
match[s[i]] = t;
match = t;
}
}
/// <summary>
/// Remove a text string from the trie.
/// </summary>
/// <param name="s">The text string to be removed from the trie.</param>
public void Remove(string s)
{
s += Mark;
int index = 0;
TrieNode match = PrefixQuery(s, ref index);
while(match.IsLeaf())
{
char c = match.Value;
if(match.Parent == null)
{
break;
}
match = match.Parent;
match.Children.Remove(c);
}
}
/// <summary>
/// Know if a text string is in the trie.
/// </summary>
/// <param name="s">The string s that you want to know if it is in the trie.</param>
/// <returns>If the string is found, it returns true, otherwise false.</returns>
public bool Find(string s)
{
int index = 0;
return PrefixQuery(s + Mark, ref index).IsLeaf();
}
/// <summary>
/// This method analyzes which is the longest common prefix of a string s in the trie. If the string is in the trie then it is equivalent to doing Find (s).
/// </summary>
/// <param name="s">The string for which you want to know the longest common prefix.</param>
/// <param name="index">The index to which the longest common prefix goes.</param>
/// <returns>
/// Returns the longest common prefix node found in the trie with the string s.
/// </returns>
private TrieNode PrefixQuery(string s, ref int index)
{
TrieNode current = root;
for (int i = 0; i < s.Length && current != null; i++)
{
if (current[s[i]] != null)
{
current = current[s[i]] ?? throw new NullReferenceException();
index = i + 1;
}
else
{
break;
}
}
return current ?? throw new NullReferenceException();
}
}
}