forked from vivekn/autocomplete
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrie.go
More file actions
100 lines (79 loc) · 1.52 KB
/
trie.go
File metadata and controls
100 lines (79 loc) · 1.52 KB
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
package trie
import (
"errors"
)
type Trie struct {
Value string
Children []Child
End bool
}
type Child struct {
Value rune
Link *Trie
}
func NewTrie() *Trie {
return &Trie{}
}
func NewChild(value rune) Child {
child := Child{}
child.Value = value
return child
}
func (t *Trie) Add(value rune) *Trie {
link := NewTrie()
link.Value = t.Value + string(value)
child := NewChild(value)
child.Link = link
t.Children = append(t.Children, child)
return link
}
func (t *Trie) Insert(value string) {
trie := t
for i, val := range value {
link, err := trie.Find(val)
if i < len(value) {
if err != nil {
link = trie.Add(val)
}
trie = link
}
}
trie.End = true
}
func (t *Trie) Find(value rune) (*Trie, error) {
if t != nil {
for _, child := range t.Children {
if child.Value == value {
return child.Link, nil
}
}
}
return nil, errors.New("Value not found")
}
func (t *Trie) AllPrefixes() ([]string, error) {
results := []string{}
if t != nil {
if t.End == true {
results = append(results, t.Value)
}
for _, val := range t.Children {
prefixes, err := val.Link.AllPrefixes()
if err != nil {
break
}
results = append(results, prefixes...)
}
} else {
return results, errors.New("No end found, so no completions available")
}
return results, nil
}
func (t *Trie) AutoComplete(prefix string) ([]string, error) {
trie := t
for _, val := range prefix {
link, _ := trie.Find(val)
trie = link
}
results, err := trie.AllPrefixes()
return results, err
}