-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstandard_trie.go
96 lines (77 loc) · 1.64 KB
/
standard_trie.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package trie
import (
"golang.org/x/text/unicode/norm"
)
type node struct {
value rune
children map[rune]*node
isWord bool
}
type trie struct {
root *node
}
func newStandard() *trie {
return &trie{root: &node{children: make(map[rune]*node)}}
}
func (t *trie) insert(text string) {
if text == "" {
return
}
normalizedText := norm.NFC.String(text)
runes := []rune(normalizedText)
t.root.insert(runes)
}
func (n *node) insert(runes []rune) {
first := runes[0]
matching_child, ok := n.children[first]
if !ok {
matching_child = &node{value: first, children: make(map[rune]*node)}
n.children[first] = matching_child
}
if len(runes[1:]) == 0 {
matching_child.isWord = true
} else {
matching_child.insert(runes[1:])
}
}
func (t *trie) search(text string) []string {
if text == "" {
return []string{}
}
normalizedText := norm.NFC.String(text)
runes := []rune(normalizedText)
result := []string{}
t.root.search("", runes, &result)
return result
}
func (n *node) search(text string, runes []rune, result *[]string) {
if len(runes) == 0 {
n.collect(text, result)
return
}
if len(n.children) == 0 {
return
}
if child, ok := n.children[runes[0]]; ok {
child.search(text+string(child.value), runes[1:], result)
}
}
func (t *trie) collect() []string {
result := []string{}
t.root.collect("", &result)
return result
}
func (n *node) collect(text string, result *[]string) {
if len(n.children) == 0 {
if text != "" {
*result = append(*result, text)
}
return
}
for _, child := range n.children {
if n.isWord {
*result = append(*result, text)
}
child.collect(text+string(child.value), result)
}
}