-
Notifications
You must be signed in to change notification settings - Fork 1
/
Trie.cs
96 lines (87 loc) · 3.01 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
using System.Collections.Generic;
namespace WordamentSolver
{
/// <summary>
/// Simple Trie using char nodes.
/// </summary>
class Trie
{
private class Node
{
/// <summary>
/// Character for this node.
/// </summary>
public char Character { get; set; }
/// <summary>
/// Indicates if this node forms a complete word.
/// </summary>
public bool IsWord { get; set; }
/// <summary>
/// Child nodes.
/// </summary>
public Dictionary<char, Node> Children = new Dictionary<char, Node>();
/// <summary>
/// Create a node with the supplied character.
/// </summary>
/// <param name="character">Character for this node.</param>
public Node(char character)
{
Character = character;
}
}
private Node root = new Node(' '); // Dummy root node
/// <summary>
/// Create an empty Trie.
/// </summary>
public Trie() { }
/// <summary>
/// Create a Trie using the supplied words.
/// </summary>
/// <param name="words">Initial words to populate the Trie.</param>
public Trie(IEnumerable<string> words)
{
foreach (string word in words)
Add(word);
}
/// <summary>
/// Add a word to the Trie.
/// </summary>
/// <param name="word">Word to add.</param>
public void Add(string word)
{
Node node = root;
foreach (char character in word)
{
Node childNode;
if (!node.Children.TryGetValue(character, out childNode))
{
childNode = new Node(character);
node.Children.Add(character, childNode);
}
node = childNode;
}
// As node will point to the last Node, we indicate that this is a word
node.IsWord = true;
}
/// <summary>
/// Indicates if the Trie contains the specified word and if the word is a prefix to another word.
/// </summary>
/// <param name="word">Word to search for.</param>
/// <param name="isPrefix">True if the specified word is a prefix; otherwise false.</param>
/// <returns>True if the Trie contains the specified word; otherwise false.</returns>
public bool Contains(string word, out bool isPrefix)
{
Node node = root;
foreach (char character in word)
{
if (!node.Children.TryGetValue(character, out node))
{
isPrefix = false;
return false;
}
}
isPrefix = true;
return node.IsWord;
}
}
}