| 1 |
package tst |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
) |
| 6 |
|
| 7 |
type ( |
| 8 |
Node struct { |
| 9 |
key byte |
| 10 |
value interface{} |
| 11 |
left, middle, right *Node |
| 12 |
} |
| 13 |
NodeIterator struct { |
| 14 |
step int |
| 15 |
node *Node |
| 16 |
prev *NodeIterator |
| 17 |
} |
| 18 |
TernarySearchTree struct { |
| 19 |
length int |
| 20 |
root *Node |
| 21 |
} |
| 22 |
) |
| 23 |
|
| 24 |
|
| 25 |
func New() *TernarySearchTree { |
| 26 |
tree := &TernarySearchTree{} |
| 27 |
tree.Init() |
| 28 |
return tree |
| 29 |
} |
| 30 |
|
| 31 |
func (this *TernarySearchTree) Do(callback func(string, interface{})bool) { |
| 32 |
if this.Len() == 0 { |
| 33 |
return |
| 34 |
} |
| 35 |
bs := []byte{} |
| 36 |
i := &NodeIterator{0,this.root,nil} |
| 37 |
for i != nil { |
| 38 |
switch i.step { |
| 39 |
|
| 40 |
case 0: |
| 41 |
i.step++ |
| 42 |
if i.node.left != nil { |
| 43 |
i = &NodeIterator{0,i.node.left,i} |
| 44 |
continue |
| 45 |
} |
| 46 |
|
| 47 |
case 1: |
| 48 |
i.step++ |
| 49 |
if i.node.key > 0 { |
| 50 |
bs = append(bs, i.node.key) |
| 51 |
} |
| 52 |
if i.node.value != nil { |
| 53 |
if !callback(string(bs), i.node.value) { |
| 54 |
return |
| 55 |
} |
| 56 |
continue |
| 57 |
} |
| 58 |
|
| 59 |
case 2: |
| 60 |
i.step++ |
| 61 |
if i.node.middle != nil { |
| 62 |
i = &NodeIterator{0,i.node.middle,i} |
| 63 |
continue |
| 64 |
} |
| 65 |
|
| 66 |
case 3: |
| 67 |
if len(bs) > 0 { |
| 68 |
bs = bs[:len(bs)-1] |
| 69 |
} |
| 70 |
i.step++ |
| 71 |
if i.node.right != nil { |
| 72 |
i = &NodeIterator{0,i.node.right,i} |
| 73 |
continue |
| 74 |
} |
| 75 |
|
| 76 |
case 4: |
| 77 |
i = i.prev |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
func (this *TernarySearchTree) Get(key string) interface{} { |
| 83 |
if this.length == 0 { |
| 84 |
return nil |
| 85 |
} |
| 86 |
|
| 87 |
node := this.root |
| 88 |
bs := []byte(key) |
| 89 |
for i := 0; i < len(bs); { |
| 90 |
b := bs[i] |
| 91 |
if b > node.key { |
| 92 |
if node.right == nil { |
| 93 |
return nil |
| 94 |
} |
| 95 |
node = node.right |
| 96 |
} else if (b < node.key) { |
| 97 |
if node.left == nil { |
| 98 |
return nil |
| 99 |
} |
| 100 |
node = node.left |
| 101 |
} else { |
| 102 |
i++ |
| 103 |
if i < len(bs) { |
| 104 |
if node.middle == nil { |
| 105 |
return nil |
| 106 |
} |
| 107 |
node = node.middle |
| 108 |
} else { |
| 109 |
break |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
return node.value |
| 114 |
} |
| 115 |
func (this *TernarySearchTree) GetLongestPrefix(key string) interface{} { |
| 116 |
if this.length == 0 { |
| 117 |
return nil |
| 118 |
} |
| 119 |
|
| 120 |
n := this.root |
| 121 |
v := n.value |
| 122 |
bs := []byte(key) |
| 123 |
for i := 0; i < len(bs); { |
| 124 |
b := bs[i] |
| 125 |
if n.value != nil { |
| 126 |
v = n.value |
| 127 |
} |
| 128 |
if b > n.key { |
| 129 |
if n.right == nil { |
| 130 |
break |
| 131 |
} |
| 132 |
n = n.right |
| 133 |
} else if b < n.key { |
| 134 |
if n.left == nil { |
| 135 |
break |
| 136 |
} |
| 137 |
n = n.left |
| 138 |
} else { |
| 139 |
i++ |
| 140 |
if i < len(bs) { |
| 141 |
if n.middle == nil { |
| 142 |
break |
| 143 |
} |
| 144 |
n = n.middle |
| 145 |
} else { |
| 146 |
break |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
if n.value != nil { |
| 151 |
v = n.value |
| 152 |
} |
| 153 |
return v |
| 154 |
} |
| 155 |
|
| 156 |
func (this *TernarySearchTree) Has(key string) bool { |
| 157 |
return this.Get(key) != nil |
| 158 |
} |
| 159 |
|
| 160 |
func (this *TernarySearchTree) Init() { |
| 161 |
this.length = 0 |
| 162 |
this.root = nil |
| 163 |
} |
| 164 |
|
| 165 |
func (this *TernarySearchTree) Insert(key string, value interface{}) { |
| 166 |
|
| 167 |
if value == nil { |
| 168 |
this.Remove(key) |
| 169 |
return |
| 170 |
} |
| 171 |
|
| 172 |
if this.length == 0 { |
| 173 |
this.root = &Node{0,nil,nil,nil,nil} |
| 174 |
} |
| 175 |
|
| 176 |
t := this.root |
| 177 |
bs := []byte(key) |
| 178 |
for i := 0; i < len(bs); { |
| 179 |
b := bs[i] |
| 180 |
if b > t.key { |
| 181 |
if t.right == nil { |
| 182 |
t.right = &Node{b,nil,nil,nil,nil} |
| 183 |
} |
| 184 |
t = t.right |
| 185 |
} else if b < t.key { |
| 186 |
if t.left == nil { |
| 187 |
t.left = &Node{b,nil,nil,nil,nil} |
| 188 |
} |
| 189 |
t = t.left |
| 190 |
} else { |
| 191 |
i++ |
| 192 |
if i < len(bs) { |
| 193 |
if t.middle == nil { |
| 194 |
t.middle = &Node{bs[i],nil,nil,nil,nil} |
| 195 |
} |
| 196 |
t = t.middle |
| 197 |
} |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
if t.value == nil { |
| 202 |
this.length++ |
| 203 |
} |
| 204 |
t.value = value |
| 205 |
} |
| 206 |
|
| 207 |
func (this *TernarySearchTree) Len() int { |
| 208 |
return this.length |
| 209 |
} |
| 210 |
|
| 211 |
func (this *TernarySearchTree) Remove(key string) interface{} { |
| 212 |
if this.length == 0 { |
| 213 |
return nil |
| 214 |
} |
| 215 |
|
| 216 |
var remove *Node |
| 217 |
var direction int |
| 218 |
|
| 219 |
t := this.root |
| 220 |
bs := []byte(key) |
| 221 |
for i := 0; i < len(bs); { |
| 222 |
b := bs[i] |
| 223 |
if b > t.key { |
| 224 |
|
| 225 |
if t.right == nil { |
| 226 |
return nil |
| 227 |
} |
| 228 |
|
| 229 |
remove = t |
| 230 |
direction = 1 |
| 231 |
|
| 232 |
t = t.right |
| 233 |
} else if b < t.key { |
| 234 |
|
| 235 |
if t.left == nil { |
| 236 |
return nil |
| 237 |
} |
| 238 |
|
| 239 |
remove = t |
| 240 |
direction = -1 |
| 241 |
|
| 242 |
t = t.left |
| 243 |
} else { |
| 244 |
i++ |
| 245 |
if i < len(bs) { |
| 246 |
|
| 247 |
if t.middle == nil { |
| 248 |
return nil |
| 249 |
} |
| 250 |
|
| 251 |
if t.value != nil { |
| 252 |
remove = t |
| 253 |
direction = 0 |
| 254 |
} |
| 255 |
|
| 256 |
t = t.middle |
| 257 |
} |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
if this.length == 1 { |
| 263 |
this.root = nil |
| 264 |
} else { |
| 265 |
if direction == -1 { |
| 266 |
remove.left = nil |
| 267 |
} else if direction == 0 { |
| 268 |
remove.middle = nil |
| 269 |
} else { |
| 270 |
remove.right = nil |
| 271 |
} |
| 272 |
} |
| 273 |
this.length-- |
| 274 |
return t.value |
| 275 |
} |
| 276 |
func (this *TernarySearchTree) String() string { |
| 277 |
if this.length == 0 { |
| 278 |
return "{}" |
| 279 |
} |
| 280 |
|
| 281 |
return this.root.String() |
| 282 |
} |
| 283 |
|
| 284 |
func (this *Node) String() string { |
| 285 |
str := "{" + string(this.key) |
| 286 |
if this.value != nil { |
| 287 |
str += ":" + fmt.Sprint(this.value) |
| 288 |
} |
| 289 |
if this.left != nil { |
| 290 |
str += this.left.String() |
| 291 |
} else { |
| 292 |
str += " " |
| 293 |
} |
| 294 |
if this.middle != nil { |
| 295 |
str += this.middle.String() |
| 296 |
} else { |
| 297 |
str += " " |
| 298 |
} |
| 299 |
if this.right != nil { |
| 300 |
str += this.right.String() |
| 301 |
} else { |
| 302 |
str += " " |
| 303 |
} |
| 304 |
str += "}" |
| 305 |
return str |
| 306 |
} |