-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrie.go
183 lines (153 loc) · 3.66 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Package trie provides primitives for processing radix tries.
package trie
import (
// "fmt"
"strings"
"unicode/utf8"
)
// Trie is a radix trie implementation.
type Trie struct {
child []*Node
count int
}
// NewTrie is used to create a new radix trie.
func NewTrie() Trie {
return Trie{}
}
// Count returns the number of nodes in the trie.
func (t *Trie) Count() int {
return t.count
}
func (t *Trie) isEmpty() bool {
return t.count == 0
}
// Insert is used to add a new term to the trie.
// If successful, this will create one or more child
// nodes. Validate for length: in the case of runes,
// it makes no sense to add a rune sequence unless it
// consists of more than one rune.
func (t *Trie) Insert(s string) bool {
// remove leading & trailing whitespace
trimmed := strings.TrimSpace(s)
// Sanity check (should catch empty strings too)
if len(trimmed) < 2 {
return false
}
// Sanity check (same again but for runes)
if utf8.RuneCount([]byte(s)) < 2 {
return false
}
if t.count == 0 {
t.makeRuneNode(s)
return true
}
// Check for duplicate nodes
if t.findNode(trimmed) != nil {
return false
}
for _, c := range t.child {
//fmt.Println()
if c.value == s[:1] {
//fmt.Printf("findNode child matched: %[01]v %[01]T\n", c)
beheaded := s[1:]
return t.insertRuneNode(c, c, beheaded)
}
}
t.makeRuneNode(s)
return true
}
func (t *Trie) insertRuneNode(parent *Node, n *Node, s string) bool {
for _, c := range n.children {
index := t.findRuneMatch(c.value, s)
//fmt.Printf("insertRuneMatch: '%s' '%s' %d len(s) = %d\n", c.value, s, index, len(s))
lenC := len(c.value)
if index > 0 {
if index == lenC {
return t.insertRuneNode(c, c, s[index:])
}
if index < lenC {
child := c.makeChildNode(c.value[index:], true)
child.children = c.children
child.childCount = 0
c.setChildNode(child)
//fmt.Printf("c.value: %s\n", c.value)
c.value = c.value[:index]
//fmt.Printf("c.value: %s\n", c.value)
c.entry = false
}
//fmt.Printf("making child node: %s\n", s[index:])
c.makeChildNode(s[index:], true)
t.count++
return true
}
}
// No match in the children so attach to the parent node
//fmt.Printf("parented.value2: %s\n", s)
parent.makeChildNode(s, true)
t.count++
return true
}
func (t *Trie) makeRuneNode(s string) {
rootRune := makeNode(s[:1], false)
rootChild := makeNode(s[1:], true)
rootRune.children = []*Node{&rootChild}
rootRune.childCount = 1
t.child = []*Node{&rootRune}
t.count++
}
// Find is used to search for a specific term in the trie.
func (t *Trie) Find(s string) (bool, *Node) {
// Remove leading & trailing whitespace
trimmed := strings.TrimSpace(s)
// Sanity check (should catch empty strings too)
if len(trimmed) < 2 {
return false, nil
}
n := t.findNode(trimmed)
return n != nil, n
}
func (t *Trie) findNode(s string) *Node {
for _, c := range t.child {
if c.value == s[:1] {
//fmt.Println()
beheaded := s[1:]
return t.findRuneNode(c, beheaded)
}
}
// Runaway check
return nil
}
func (t *Trie) findRuneNode(n *Node, s string) *Node {
for _, c := range n.children {
index := t.findRuneMatch(c.value, s)
//fmt.Printf("findRuneMatch: '%s' '%s' %d len(s) = %d\n", c.value, s, index, len(s))
lenC := len(c.value)
if index > 0 {
if index == lenC && index == len(s) {
return c
}
if c.value == s {
return c
}
if c.childCount > 0 {
return t.findRuneNode(c, s[index:])
}
}
}
return nil
}
func (t *Trie) findRuneMatch(v string, s string) int {
res := 0
sLen := len(s)
runes := []rune(s)
for i, c := range v {
if i > sLen {
return res
}
if c != runes[i] {
return res
}
res++
}
return res
}