-
Notifications
You must be signed in to change notification settings - Fork 1
/
tree_sort.go
58 lines (53 loc) · 1.06 KB
/
tree_sort.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
package prometheus_backfill
import (
io_prometheus_client "github.com/prometheus/client_model/go"
)
type node struct {
value []*io_prometheus_client.Metric
left *node
right *node
}
type bst struct {
root *node
length int64
}
func (t *bst) insert(v []*io_prometheus_client.Metric) {
if len(v) == 0 {
ErrLog("Tried insertion of an empty slice")
return // No insert for empty slices
}
if t.root == nil {
t.root = &node{v, nil, nil}
t.length = 0
return
}
current := t.root
t.length++
for {
if *v[0].TimestampMs < *current.value[0].TimestampMs {
if current.left == nil {
current.left = &node{v, nil, nil}
return
}
current = current.left
} else {
if current.right == nil {
current.right = &node{v, nil, nil}
return
}
current = current.right
}
}
}
func (t *bst) inorder(visit func(metric []*io_prometheus_client.Metric)) {
var traverse func(*node)
traverse = func(current *node) {
if current == nil {
return
}
traverse(current.left)
visit(current.value)
traverse(current.right)
}
traverse(t.root)
}