forked from gogearbox/gearbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tst.go
84 lines (72 loc) · 1.6 KB
/
tst.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
package gearbox
// Basic Implementation for Ternary Search Tree (TST)
// tst returns Ternary Search Tree
type tst interface {
Set(word string, value interface{})
Get(word string) interface{}
}
// Ternary Search Tree node that holds a single character and value if there is
type tstNode struct {
lower *tstNode
higher *tstNode
equal *tstNode
key byte
value interface{}
}
// newTST returns Ternary Search Tree
func newTST() tst {
return &tstNode{}
}
// Set adds a value to provided string
func (t *tstNode) Set(word string, value interface{}) {
if len(word) < 1 {
return
}
t = t.insert(t, word, 0, value)
}
// Get gets the value of provided key if it's existing, otherwise returns nil
func (t *tstNode) Get(word string) interface{} {
length := len(word)
if length < 1 || t == nil {
return nil
}
lastElm := length - 1
n := t
idx := 0
key := word[idx]
for n != nil {
if key < n.key {
n = n.lower
} else if key > n.key {
n = n.higher
} else {
if idx == lastElm {
return n.value
}
idx++
n = n.equal
key = word[idx]
}
}
return nil
}
// insert is an internal method for inserting a string with value in TST
func (t *tstNode) insert(n *tstNode, word string, index int, value interface{}) *tstNode {
key := word[index]
lastElm := len(word) - 1
if n == nil {
n = &tstNode{key: key}
}
if key < n.key {
n.lower = t.insert(n.lower, word, index, value)
} else if key > n.key {
n.higher = t.insert(n.higher, word, index, value)
} else {
if index == lastElm {
n.value = value
} else {
n.equal = t.insert(n.equal, word, index+1, value)
}
}
return n
}