-
Notifications
You must be signed in to change notification settings - Fork 0
/
trie.go
163 lines (133 loc) · 3.15 KB
/
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package trie
import (
"encoding/json"
"github.com/juju/errors"
)
type Trie struct {
words []string
root *Node
}
type SearchResult struct {
Word string
Position int
}
// Used for marshalling and unmarshalling of Trie structure
type jsonTrie struct {
Words []string
Root *Node
}
// Find searches for all occurences in Trie datastructure.
func (trie *Trie) Find(text string, f func(item SearchResult)) {
length := len(text)
for startPosition := 0; startPosition < length; {
var lastVisited *Node = trie.root
for indx := startPosition; indx < length; indx += 1 {
if node := lastVisited.FindChild(text[indx]); node != nil {
// We found word!
if node.Word != -1 {
f(SearchResult{
Word: trie.words[node.Word],
Position: startPosition,
})
}
lastVisited = node
} else {
break
}
}
startPosition += lastVisited.Skip
}
}
// IsContained searches for first occurence of any word in Trie datastructure.
func (trie *Trie) IsContained(text string) bool {
length := len(text)
for startPosition := 0; startPosition < length; {
var lastVisited *Node = trie.root
for indx := startPosition; indx < length; indx += 1 {
if node := lastVisited.FindChild(text[indx]); node != nil {
if node.Word != -1 {
return true
}
lastVisited = node
} else {
break
}
}
startPosition += lastVisited.Skip
}
return false
}
func (trie *Trie) HasPrefix(prefix string) bool {
length := len(prefix)
var lastVisited *Node = trie.root
for indx := 0; indx < length; indx += 1 {
if node := lastVisited.FindChild(prefix[indx]); node != nil {
lastVisited = node
} else {
return false
}
}
return true
}
func (trie *Trie) WordsWithPrefix(prefix string) []string {
length := len(prefix)
if length == 0 {
return nil
}
var lastVisited *Node = trie.root
for indx := 0; indx < length; indx += 1 {
if node := lastVisited.FindChild(prefix[indx]); node != nil {
lastVisited = node
} else {
return nil
}
}
if lastVisited != nil {
return trie.collectWords(lastVisited)
}
return nil
}
func (trie *Trie) collectWords(n *Node) []string {
var results []string
if n == nil {
return results
}
var nodesToSearch []*Node
nodesToSearch = append(nodesToSearch, n)
nodesCount := 1
for nodesCount > 0 {
currentNode := nodesToSearch[nodesCount-1]
if currentNode.HasWord() {
results = append(results, trie.words[currentNode.Word])
}
nodesToSearch = nodesToSearch[:nodesCount-1]
for i := len(currentNode.Children) - 1; i >= 0; i-- {
nodesToSearch = append(nodesToSearch, currentNode.Children[i])
}
nodesCount = len(nodesToSearch)
}
return results
}
func (trie *Trie) Words() []string {
return trie.words
}
func (trie *Trie) SortedWords() []string {
return trie.collectWords(trie.root)
}
func (trie *Trie) ToJson() ([]byte, error) {
return json.Marshal(jsonTrie{
Words: trie.words,
Root: trie.root,
})
}
func FromJson(data []byte) (*Trie, error) {
var trie jsonTrie
err := json.Unmarshal(data, &trie)
if err != nil {
return nil, errors.Annotate(err, "Error while parsing Trie datastructure from json")
}
return &Trie{
words: trie.Words,
root: trie.Root,
}, nil
}