-
Notifications
You must be signed in to change notification settings - Fork 10
/
search.go
66 lines (54 loc) · 1.44 KB
/
search.go
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
56
57
58
59
60
61
62
63
64
65
66
package bits
/**
* Given a word, returns array of words, prefix of which is word
*/
func (f *FrozenTrie) GetSuggestedWords(word string, limit int) []string {
var result []string
node := f.GetRoot()
// find the node corresponding to the last char of input
for _, runeValue := range word {
var child FrozenTrieNode
var j uint = 0
for ; j < node.GetChildCount(); j++ {
child = node.GetChild(j)
if child.letter == string(runeValue) {
break
}
}
// not found, return.
if j == node.GetChildCount() {
return result
}
node = child
}
// The node corresponding to the last letter of word is found.
// Use this node as root. traversing the trie in level order.
return f.traverseSubTrie(node, word, limit)
}
func (f *FrozenTrie) traverseSubTrie(node FrozenTrieNode, prefix string, limit int) []string {
var result []string
var level []FrozenTrieNode
level = append(level, node)
var prefixLevel []string
prefixLevel = append(prefixLevel, prefix)
for len(level) > 0 {
nodeNow := level[0]
level = level[1:]
prefixNow := prefixLevel[0]
prefixLevel = prefixLevel[1:]
// if the prefix is a legal word.
if nodeNow.final {
result = append(result, prefixNow)
if len(result) > limit {
return result
}
}
var i uint = 0
for ; i < nodeNow.GetChildCount(); i++ {
child := nodeNow.GetChild(i)
level = append(level, child)
prefixLevel = append(prefixLevel, prefixNow+child.letter)
}
}
return result
}