1
1
package trie
2
2
3
+ // TernaryTrie provides ternary trie-tree.
3
4
type TernaryTrie struct {
4
5
root TernaryNode
5
6
}
6
7
8
+ // NewTernaryTrie creates a ternary trie-tree.
7
9
func NewTernaryTrie () * TernaryTrie {
8
10
return & TernaryTrie {}
9
11
}
10
12
13
+ // Root returns the root node of the trie-tree.
11
14
func (t * TernaryTrie ) Root () Node {
12
15
return & t .root
13
16
}
14
17
18
+ // Get returns a node for key k.
15
19
func (t * TernaryTrie ) Get (k string ) Node {
16
20
return Get (t , k )
17
21
}
18
22
23
+ // Put puts a pair of key and value to trie-tree.
19
24
func (t * TernaryTrie ) Put (k string , v interface {}) Node {
20
25
return Put (t , k , v )
21
26
}
22
27
28
+ // Size counts nodes in the trie-tree.
23
29
func (t * TernaryTrie ) Size () int {
24
30
count := 0
25
31
EachDepth (t , func (Node ) bool {
@@ -29,6 +35,7 @@ func (t *TernaryTrie) Size() int {
29
35
return count
30
36
}
31
37
38
+ // Balance balances all nodes of trie-tree in each layers.
32
39
func (t * TernaryTrie ) Balance () {
33
40
EachDepth (t , func (n Node ) bool {
34
41
n .(* TernaryNode ).Balance ()
@@ -37,17 +44,20 @@ func (t *TernaryTrie) Balance() {
37
44
t .root .Balance ()
38
45
}
39
46
47
+ // TernaryNode provides node of ternary trie-tree.
40
48
type TernaryNode struct {
41
49
label rune
42
50
firstChild * TernaryNode
43
51
low , high * TernaryNode
44
52
value interface {}
45
53
}
46
54
55
+ // NewTernaryNode creates a node instance.
47
56
func NewTernaryNode (l rune ) * TernaryNode {
48
57
return & TernaryNode {label : l }
49
58
}
50
59
60
+ // Get returns a child node for k.
51
61
func (n * TernaryNode ) Get (k rune ) Node {
52
62
curr := n .firstChild
53
63
for curr != nil {
@@ -62,6 +72,7 @@ func (n *TernaryNode) Get(k rune) Node {
62
72
return nil
63
73
}
64
74
75
+ // Dig digs a node for k. it returns node and a flag for whether dig or not.
65
76
func (n * TernaryNode ) Dig (k rune ) (node Node , isnew bool ) {
66
77
curr := n .firstChild
67
78
if curr == nil {
@@ -87,14 +98,17 @@ func (n *TernaryNode) Dig(k rune) (node Node, isnew bool) {
87
98
}
88
99
}
89
100
101
+ // FirstChild returns first child node.
90
102
func (n * TernaryNode ) FirstChild () * TernaryNode {
91
103
return n .firstChild
92
104
}
93
105
106
+ // HasChildren returns the node hash any children or not.
94
107
func (n * TernaryNode ) HasChildren () bool {
95
108
return n .firstChild != nil
96
109
}
97
110
111
+ // Size counts descended nodes.
98
112
func (n * TernaryNode ) Size () int {
99
113
if n .firstChild == nil {
100
114
return 0
@@ -107,6 +121,7 @@ func (n *TernaryNode) Size() int {
107
121
return count
108
122
}
109
123
124
+ // Each enumerates descended nodes.
110
125
func (n * TernaryNode ) Each (proc func (Node ) bool ) {
111
126
var f func (* TernaryNode ) bool
112
127
f = func (n * TernaryNode ) bool {
@@ -120,18 +135,22 @@ func (n *TernaryNode) Each(proc func(Node) bool) {
120
135
f (n .firstChild )
121
136
}
122
137
138
+ // RemoveAll removes all descended nodes.
123
139
func (n * TernaryNode ) RemoveAll () {
124
140
n .firstChild = nil
125
141
}
126
142
143
+ // Label returns a label rune.
127
144
func (n * TernaryNode ) Label () rune {
128
145
return n .label
129
146
}
130
147
148
+ // Value returns a value for the node.
131
149
func (n * TernaryNode ) Value () interface {} {
132
150
return n .value
133
151
}
134
152
153
+ // SetValue set a value for the node.
135
154
func (n * TernaryNode ) SetValue (v interface {}) {
136
155
n .value = v
137
156
}
@@ -150,6 +169,7 @@ func (n *TernaryNode) children() []*TernaryNode {
150
169
return children
151
170
}
152
171
172
+ // Balance balances all descended nodes.
153
173
func (n * TernaryNode ) Balance () {
154
174
if n .firstChild == nil {
155
175
return
@@ -171,11 +191,10 @@ func balance(nodes []*TernaryNode, s, e int) *TernaryNode {
171
191
} else if count == 2 {
172
192
nodes [s ].high = nodes [s + 1 ]
173
193
return nodes [s ]
174
- } else {
175
- mid := (s + e ) / 2
176
- n := nodes [mid ]
177
- n .low = balance (nodes , s , mid )
178
- n .high = balance (nodes , mid + 1 , e )
179
- return n
180
194
}
195
+ mid := (s + e ) / 2
196
+ n := nodes [mid ]
197
+ n .low = balance (nodes , s , mid )
198
+ n .high = balance (nodes , mid + 1 , e )
199
+ return n
181
200
}
0 commit comments