-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtree.go
57 lines (46 loc) · 956 Bytes
/
tree.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
package zipkin
import (
zkcore "github.com/mattkanwisher/distributedtrace/gen/zipkincore"
)
type tree struct {
id int64
parent *tree
name string
absTime int64
relTime int64
children []*tree
span *zkcore.Span
outputMap OutputMap
}
func (t *tree) visitByBreadth(visitor func(*tree) bool) bool {
var next *tree
queue := []*tree{t}
for len(queue) > 0 {
next, queue = queue[0], queue[1:]
queue = append(queue, next.children...)
if !visitor(next) {
return false
}
}
return true
}
func (t *tree) visitByDepth(visitor func(*tree) bool) bool {
for _, child := range t.children {
if !child.visitByDepth(visitor) {
return false
} else if !visitor(child) {
return false
}
}
return visitor(t)
}
func (t *tree) childWithId(id int64) *tree {
for _, node := range t.children {
if node.id == id {
return node
} else if child := node.childWithId(id); child != nil {
return child
}
}
return nil
}