This repository has been archived by the owner on Dec 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtrie.go
179 lines (143 loc) · 3.09 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
package main
import (
"fmt"
"sort"
"strings"
)
type edge struct {
node *node
label string
counter uint
}
type node struct {
edges []*edge
}
// Trie represents a a very limited radix tree implementation.
//
// For information about radix trees, see https://en.wikipedia.org/wiki/Radix_tree.
type Trie struct {
root *node
}
// WalkFn is used when walking the tree.
type WalkFn func(prefix string, count uint)
func (n *node) appendEdge(e *edge) {
n.edges = append(n.edges, e)
}
func (n *node) walk(fn WalkFn, prefixes []string) {
for _, edge := range n.edges {
newPrefixes := append(prefixes, edge.label)
fn(
strings.Join(newPrefixes, ""),
edge.counter,
)
if edge.node != nil {
edge.node.walk(fn, newPrefixes)
}
}
}
func commonPrefix(s1, s2 string) int {
max := len(s1)
if l := len(s2); l < max {
max = l
}
var i int
for i = 0; i < max; i++ {
if s1[i] != s2[i] {
break
}
}
return i
}
func (e *edge) split(length int) {
oldLabel := e.label
oldNode := e.node
e.label = oldLabel[:length]
e.node = &node{}
e.node.appendEdge(&edge{
label: oldLabel[length:],
counter: e.counter - 1,
node: oldNode,
})
}
// NewTrie returns an empty Trie.
func NewTrie() *Trie {
return &Trie{&node{[]*edge{}}}
}
// Insert adds an element to the tree.
func (t *Trie) Insert(s string) {
var nextEdge *edge
traverseNode := t.root
elementsFound := 0
for traverseNode != nil {
nextEdge = nil
for _, e := range traverseNode.edges {
prefixLength := commonPrefix(s[elementsFound:], e.label)
if prefixLength == 0 {
continue
}
elementsFound += prefixLength
e.counter++
nextEdge = e
if prefixLength < len(e.label) {
e.split(prefixLength)
}
break
}
if nextEdge == nil {
break
}
traverseNode = nextEdge.node
}
if elementsFound < len(s) {
if traverseNode == nil {
traverseNode = &node{}
nextEdge.node = traverseNode
}
traverseNode.appendEdge(&edge{
label: s[elementsFound:],
counter: 1,
})
}
}
// Walk is used to walk the tree edges.
func (t *Trie) Walk(fn WalkFn) {
t.root.walk(fn, []string{})
}
type edgeData struct {
prefix string
count uint
}
type edgeDataSorter []*edgeData
func (s edgeDataSorter) Len() int {
return len(s)
}
func (s edgeDataSorter) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s edgeDataSorter) Less(i, j int) bool {
if s[i].count == s[j].count {
return s[i].prefix > s[j].prefix
}
return s[i].count < s[j].count
}
// Sprint returns human readable representation of the tree data.
// This method is computationally expensive because it walks the tree and sorts edge data.
func (t *Trie) Sprint(count int) string {
edges := []*edgeData{}
var total uint
t.Walk(func(prefix string, count uint) {
edges = append(edges, &edgeData{prefix, count})
total += count
})
sort.Sort(sort.Reverse(edgeDataSorter(edges)))
lines := []string{}
for i, e := range edges {
percent := float32(e.count) * 100 / float32(total)
l := fmt.Sprintf("%s: %.2f%% (%d)", e.prefix, percent, e.count)
lines = append(lines, l)
if i+1 == count {
break
}
}
return strings.Join(lines, "\n")
}