我有大约150,000个字加载到一个Trie数据结构,并希望搜索。每次我在searchBox中输入一个新字符时,都会作为NextMatch的参数传递。搜索三数据结构
例如:如果我输入'Appl',则将返回以'Appl'开头的所有单词。如果我输入字符'e',则同样显示以'Apple'开头的所有单词。
问题是,当我删除字符'e'时,'l'将作为NextMatch参数传递给Matcher方法,并创建类似'Appll'的东西,但我想要以'Appl'开头的那些单词的列表。
这里是我的代码狙击
private void searchBox_TextChanged(object sender, TextChangedEventArgs e)
{
listboxWords1.Items.Clear();
if (searchBox.Text.Length > 0)
{
trie.Matcher.NextMatch(searchBox.Text.Trim().Last());
foundWords = trie.Matcher.GetPrefixMatches(); //foundWords is List<string>
for (int i = foundWords.Count - 1; i > 0 ; i--)
{
listboxWords1.Items.Add(foundWords[i]);
}
foundWords = null;
isFoundExact = trie.Matcher.IsExactMatch();
if (isFoundExact)
listboxWords1.Items.Add(trie.Matcher.GetExactMatch());
}
else
{
foundWords = null;
trie.Matcher.ResetMatch();
}
}
特里数据结构的实现,可以发现here
主要问题是我无法确定是否输入了新字符或者字符是否已从最后一个字符串中删除。如果我可以确定一个字符被删除,我可以告诉它搜索剩余的字符一个接一个。 – Hadi 2014-11-25 08:29:00