-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathtrie_st.go
174 lines (156 loc) · 2.89 KB
/
trie_st.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
package algs4
const radix = 256
type trieNode struct {
val interface{}
next []*trieNode
}
func newNode() *trieNode {
return &trieNode{next: make([]*trieNode, radix)}
}
// TrieST ...
type TrieST struct {
R int
n int // size
root *trieNode
}
// NewTrieST ...
func NewTrieST() *TrieST {
return &TrieST{radix, 0, nil}
}
// Get ...
func (t *TrieST) Get(key string) interface{} {
x := t.get(t.root, key, 0)
if x == nil {
return nil
}
return x.val
}
func (t *TrieST) get(x *trieNode, key string, d int) *trieNode {
if x == nil {
return nil
}
if d == len(key) {
return x
}
c := key[d]
return t.get(x.next[c], key, d+1)
}
// Put ...
func (t *TrieST) Put(key string, val interface{}) {
t.root = t.put(t.root, key, val, 0)
}
func (t *TrieST) put(x *trieNode, key string, val interface{}, d int) *trieNode {
if x == nil {
x = newNode()
}
if d == len(key) {
if x.val == nil {
t.n++
}
x.val = val
return x
}
c := key[d]
x.next[c] = t.put(x.next[c], key, val, d+1)
return x
}
// Keys ...
func (t *TrieST) Keys() []string {
return t.KeysWithPrefix("")
}
// KeysWithPrefix ...
func (t *TrieST) KeysWithPrefix(pre string) (keys []string) {
q := NewQueue()
t.collect(t.get(t.root, pre, 0), pre, q)
for _, item := range q.StringSlice() {
keys = append(keys, item)
}
return
}
func (t *TrieST) collect(x *trieNode, pre string, q *Queue) {
if x == nil {
return
}
if x.val != nil {
q.Enqueue(pre)
}
for c := 0; c < t.R; c++ {
t.collect(x.next[c], pre+string(c), q)
}
}
// KeysThatMatch ...
func (t *TrieST) KeysThatMatch(pat string) (keys []string) {
q := NewQueue()
t.collectMatch(t.root, "", pat, q)
for _, item := range q.StringSlice() {
keys = append(keys, item)
}
return
}
func (t *TrieST) collectMatch(x *trieNode, pre, pat string, q *Queue) {
if x == nil {
return
}
d := len(pre)
if d == len(pat) && x.val != nil {
q.Enqueue(pre)
}
if d == len(pat) {
return
}
next := pat[d]
for c := 0; c < t.R; c++ {
if string(next) == "." || int(next) == c {
t.collectMatch(x.next[c], pre+string(c), pat, q)
}
}
}
// LongPrefixOf ...
func (t *TrieST) LongPrefixOf(s string) string {
length := t.search(t.root, s, 0, 0)
return s[:length]
}
func (t *TrieST) search(x *trieNode, s string, d, length int) int {
if x == nil {
return length
}
if x.val != nil {
length = d
}
if d == len(s) {
return length
}
c := s[d]
return t.search(x.next[c], s, d+1, length)
}
// Delete ...
func (t *TrieST) Delete(key string) {
t.root = t.delete(t.root, key, 0)
}
func (t *TrieST) delete(x *trieNode, key string, d int) *trieNode {
if x == nil {
return nil
}
if d == len(key) {
if x.val != nil {
t.n--
}
x.val = nil
} else {
c := key[d]
x.next[c] = t.delete(x.next[c], key, d+1)
}
if x.val == nil {
return x
}
for c := 0; c < t.R; c++ {
if x.next[c] != nil {
return x
}
}
return nil
}
// Size ...
func (t *TrieST) Size() int {
return t.n
}