【发布时间】:2014-11-14 14:33:23
【问题描述】:
我没有在 trie 中得到概念这个词的结尾。我认为我对 trie 本身的理解是不完整的。有人可以解释一下代码HERE
// If this is end of a word, then update prevMatch
if( crawl.isEnd() )
prevMatch = level + 1;
【问题讨论】:
我没有在 trie 中得到概念这个词的结尾。我认为我对 trie 本身的理解是不完整的。有人可以解释一下代码HERE
// If this is end of a word, then update prevMatch
if( crawl.isEnd() )
prevMatch = level + 1;
【问题讨论】:
当单词结束但不是 trie 或搜索结束时设置 IsEnd 终止开关。单词按字典顺序存储到树或哈希图中。可能发生搜索词不是第一个或第二个 killswitch。
【讨论】:
这发生在代码爬上 trie 搜索 gifen 单词的最长前缀时。
假设你有一个已经包含单词的 trie:
“的” “一” “一次”
并且您正在尝试搜索“onerous”的最长前缀,您希望它出现在 3 处(即“one”是已经存在的最长前缀。
正在执行的代码如下:
// See if there is a Trie edge for the current character
if (child.containsKey(ch)) {
result += ch; //Update result
crawl = child.get(ch); //Update crawl to move down in Trie
// If this is end of a word, then update prevMatch
if (crawl.isEnd()) {
prevMatch = level + 1;
}
} else {
break;
}
所以想象你正在看“繁重”的“n”。您发现“on”作为片段在 trie 中,但不是 end(因为您没有在 trie 中放置“on”一词,但在其中放置了“one”is )。此代码将使用下一个字符“e”继续爬树,但不会更改 prevMatch。但是,当它查看“e”时,树中有一个匹配项并且它是 end,因为单词“one”在树中,所以此代码将 3 存储在 @ 987654326@.
【讨论】: