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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
|
package tst
import (
"fmt"
)
type (
Node struct {
key byte
value interface{}
left, middle, right *Node
}
NodeIterator struct {
step int
node *Node
prev *NodeIterator
}
TernarySearchTree struct {
length int
root *Node
}
)
// Create a new ternary search tree
func New() *TernarySearchTree {
tree := &TernarySearchTree{}
tree.Init()
return tree
}
// Iterate over the collection
func (this *TernarySearchTree) Do(callback func(string, interface{})bool) {
if this.Len() == 0 {
return
}
bs := []byte{}
i := &NodeIterator{0,this.root,nil}
for i != nil {
switch i.step {
// Left
case 0:
i.step++
if i.node.left != nil {
i = &NodeIterator{0,i.node.left,i}
continue
}
// Value
case 1:
i.step++
if i.node.key > 0 {
bs = append(bs, i.node.key)
}
if i.node.value != nil {
if !callback(string(bs), i.node.value) {
return
}
continue
}
// Middle
case 2:
i.step++
if i.node.middle != nil {
i = &NodeIterator{0,i.node.middle,i}
continue
}
// Right
case 3:
if len(bs) > 0 {
bs = bs[:len(bs)-1]
}
i.step++
if i.node.right != nil {
i = &NodeIterator{0,i.node.right,i}
continue
}
// Backtrack
case 4:
i = i.prev
}
}
}
// Get the value at the specified key. Returns nil if not found.
func (this *TernarySearchTree) Get(key string) interface{} {
if this.length == 0 {
return nil
}
node := this.root
bs := []byte(key)
for i := 0; i < len(bs); {
b := bs[i]
if b > node.key {
if node.right == nil {
return nil
}
node = node.right
} else if (b < node.key) {
if node.left == nil {
return nil
}
node = node.left
} else {
i++
if i < len(bs) {
if node.middle == nil {
return nil
}
node = node.middle
} else {
break
}
}
}
return node.value
}
func (this *TernarySearchTree) GetLongestPrefix(key string) interface{} {
if this.length == 0 {
return nil
}
n := this.root
v := n.value
bs := []byte(key)
for i := 0; i < len(bs); {
b := bs[i]
if n.value != nil {
v = n.value
}
if b > n.key {
if n.right == nil {
break
}
n = n.right
} else if b < n.key {
if n.left == nil {
break
}
n = n.left
} else {
i++
if i < len(bs) {
if n.middle == nil {
break
}
n = n.middle
} else {
break
}
}
}
if n.value != nil {
v = n.value
}
return v
}
// Test to see whether or not the given key is contained in the tree.
func (this *TernarySearchTree) Has(key string) bool {
return this.Get(key) != nil
}
// Initialize the tree (reset it so that it's empty). New will do this for you.
func (this *TernarySearchTree) Init() {
this.length = 0
this.root = nil
}
// Insert a new key value pair into the collection
func (this *TernarySearchTree) Insert(key string, value interface{}) {
// If the value is nil then remove this key from the collection
if value == nil {
this.Remove(key)
return
}
if this.length == 0 {
this.root = &Node{0,nil,nil,nil,nil}
}
t := this.root
bs := []byte(key)
for i := 0; i < len(bs); {
b := bs[i]
if b > t.key {
if t.right == nil {
t.right = &Node{b,nil,nil,nil,nil}
}
t = t.right
} else if b < t.key {
if t.left == nil {
t.left = &Node{b,nil,nil,nil,nil}
}
t = t.left
} else {
i++
if i < len(bs) {
if t.middle == nil {
t.middle = &Node{bs[i],nil,nil,nil,nil}
}
t = t.middle
}
}
}
if t.value == nil {
this.length++
}
t.value = value
}
// Get the number of items stored in the tree
func (this *TernarySearchTree) Len() int {
return this.length
}
// Remove a key from the collection
func (this *TernarySearchTree) Remove(key string) interface{} {
if this.length == 0 {
return nil
}
var remove *Node
var direction int
t := this.root
bs := []byte(key)
for i := 0; i < len(bs); {
b := bs[i]
if b > t.key {
// Not in the collection
if t.right == nil {
return nil
}
// This is a branch so we have to keep it
remove = t
direction = 1
// Move to the next node
t = t.right
} else if b < t.key {
// Not in the collection
if t.left == nil {
return nil
}
// This is a branch so we have to keep it
remove = t
direction = -1
// Move to the next node
t = t.left
} else {
i++
if i < len(bs) {
// Not in the collection
if t.middle == nil {
return nil
}
// Has a value so we need to keep at least this much
if t.value != nil {
remove = t
direction = 0
}
// Move to the next node
t = t.middle
}
}
}
// If this was the only item in the tree, set the root pointer to nil
if this.length == 1 {
this.root = nil
} else {
if direction == -1 {
remove.left = nil
} else if direction == 0 {
remove.middle = nil
} else {
remove.right = nil
}
}
this.length--
return t.value
}
func (this *TernarySearchTree) String() string {
if this.length == 0 {
return "{}"
}
return this.root.String()
}
// Dump the tree to a string for easier debugging
func (this *Node) String() string {
str := "{" + string(this.key)
if this.value != nil {
str += ":" + fmt.Sprint(this.value)
}
if this.left != nil {
str += this.left.String()
} else {
str += " "
}
if this.middle != nil {
str += this.middle.String()
} else {
str += " "
}
if this.right != nil {
str += this.right.String()
} else {
str += " "
}
str += "}"
return str
}
|