I’m a C# beginner and at the moment I’m trying to challenge myself with different problems.
At the moment I’m trying to build a web application where you can input letters and wildcards to search after possible words.
Trough previous questions about this I’ve decided to build a Trie containing letters generated from 400k+ words. Later I’ll search the Trie for possible word matches depending on letter and wildcard input.
I’ve built two classes, one representing one node in the Trie and one representing the whole Trie.
I’m currently at a standstill, my problem is that I want to add multiple children to the Trie at multiple levels and every child has to be uniqe.
Doing this manually would look something like this:
//Level 1
Root.Children.Add(new TrieNode(Letter, false, new List<TrieNode>()));
//Level 2
Root.Children[0].Children.Add(new TrieNode(Letter, false, new List<TrieNode>()));
//Level 3
Root.Children[0].Children[0].Children.Add(new TrieNode(Letter, false, new List<TrieNode>()));
Problem is that I want to add children with one or more loops and doing it this way seems a little “wrong”:
LetterArray = Word.ToCharArray();
int level = 0;
foreach (char Letter in LetterArray)
{
//Level 1
if (level == 0)
Root.Children.Add(new TrieNode(Letter, false, new List<TrieNode>()));
//Level 2
if (level == 1)
Root.Children[0].Children.Add(new TrieNode(Letter, false, new List<TrieNode>()));
//Level 3
if (level == 2)
Root.Children[0].Children[0].Children.Add(new TrieNode(Letter, false, new List<TrieNode>()));
level++;
}
What I need is one or more loops with “clean” code, think you can help me?
For the Trie to be searchable later the letters needs to come in order i think.
Here are my other questions related to this: Question 1, Question 2.
Here’s my TrieNode Class:
public class TrieNode
{
private char _Letter;
private bool _IsEndOfWord;
private List<TrieNode> _Children;
public char Letter {
get { return _Letter; }
set { _Letter = value; }
}
public bool IsEndOfWord {
get { return _IsEndOfWord; }
set { _IsEndOfWord = value; }
}
public List<TrieNode> Children {
get { return _Children; }
set { _Children = value; }
}
public TrieNode(char letter, bool isEndOfWord, List<TrieNode> children) {
Letter = letter;
IsEndOfWord = isEndOfWord;
Children = children;
}
}
… and here is my Trie Class:
public class Trie
{
private TrieNode _Root;
public TrieNode Root
{
get { return _Root; }
set { _Root = value; }
}
public Trie(List<string> Words)
{
Root = new TrieNode('^', false, new List<TrieNode>());
char[] LetterArray;
foreach (String Word in Words)
{
LetterArray = Word.ToCharArray();
foreach (char Letter in LetterArray)
{
// Here is where I want to add nodes to my Trie
}
}
}
}
I’d accomplish this through recursion as well, however my recursive method would be inside the TrieNode class:
Finally you just have to change your initial loop in your Trie constructor:
(note: I haven’t tested this code so there may be typos).