使用一个或多个循环和“clean”在多个级别添加多个独特的子级;代码
我是一名 C# 初学者,目前我正在尝试用不同的问题挑战自己。
目前我正在尝试构建一个网络应用程序,您可以在其中输入字母和通配符来搜索可能的单词。
通过之前的问题,我决定构建一个包含由 40 万多个单词生成的字母的 Trie。稍后我将根据字母和通配符输入在 Trie 中搜索可能的单词匹配。
我构建了两个类,一个代表 Trie 中的一个节点,另一个代表整个 Trie。
我目前处于停滞状态,我的问题是我想在多个级别向 Trie 添加多个子项,并且每个子项都必须是唯一的。
手动执行此操作看起来像这样:
//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>()));
问题是我想添加具有一个或多个循环的子级,这样做似乎有点“错误”:
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++;
}
我需要的是具有“干净”代码的一个或多个循环,我想你可以帮助我吗? 我认为,为了以后可以搜索 Trie,字母需要按顺序排列。 以下是我与此相关的其他问题:问题 1、问题2.。
这是我的 TrieNode 类:
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;
}
}
...这是我的 Trie 类:
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’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
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在寻找递归吗?
http://en.wikipedia.org/wiki/Recursion_%28computer_science%29
我希望这对您有所帮助,如果我为您做了太多工作,我很抱歉。
我同意,最好的学习方式就是实践。
Are you looking for recursion?
http://en.wikipedia.org/wiki/Recursion_%28computer_science%29
I hope this helps, and sorry if I did too much work for you.
The best way to learn is doing, I agree.
我也会通过递归来完成此操作,但是我的递归方法将在 TrieNode 类内部:
最后,您只需更改 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).