-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrie.cs
More file actions
55 lines (47 loc) · 1.26 KB
/
Copy pathTrie.cs
File metadata and controls
55 lines (47 loc) · 1.26 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CodingProblems
{
public class TrieNode
{
public char Value { get; set; }
public bool IsWord { get; set; }
public Dictionary<char, TrieNode> Children { get; set; }
public TrieNode()
{
Children = new Dictionary<char, TrieNode>();
}
}
public class Trie
{
public Trie()
{
Root = new TrieNode();
}
public TrieNode Root { get; set; }
public void InsertString(string str)
{
TrieNode currentNode = Root;
for (int i = 0; i < str.Length; i++)
{
if (currentNode.Children.ContainsKey(str[i]))
{
currentNode = currentNode.Children[str[i]];
}
else
{
var newNode = new TrieNode { Value = str[i] };
currentNode.Children.Add(str[i], newNode);
currentNode = newNode;
}
if (i == str.Length -1)
{
currentNode.IsWord = true;
}
}
}
}
}