-
Notifications
You must be signed in to change notification settings - Fork 1
/
examples_test.go
107 lines (82 loc) · 2.19 KB
/
examples_test.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package segtree
import "fmt"
func ExampleTree_QueryIndex() {
tree := new(Tree)
tree.Push(1, 10, "hello, world")
tree.BuildTree()
results, err := tree.QueryIndex(4)
if err != nil {
panic(fmt.Sprintf("Failed to query tree: %s", err.Error()))
}
result := <-results
fmt.Println("Found:", result)
// Output: Found: hello, world
}
func ExampleTree_QueryIndex_multiple_elements() {
tree := new(Tree)
tree.Push(1, 10, "hello, world")
tree.Push(5, 15, 3.14)
tree.BuildTree()
results, err := tree.QueryIndex(6)
if err != nil {
panic(fmt.Sprintf("Failed to query tree: %s", err.Error()))
}
for result := range results {
fmt.Println("Found:", result)
}
// Output:
// Found: 3.14
// Found: hello, world
}
func ExampleTree_QueryIndex_2_dimensional() {
inner := new(Tree)
inner.Push(1, 10, "hello, world")
inner.BuildTree()
outer := new(Tree)
outer.Push(0, 99, inner)
outer.BuildTree()
resultsOuter, err := outer.QueryIndex(10)
if err != nil {
panic(fmt.Sprintf("Failed to query outer tree: %s", err.Error()))
}
result := <-resultsOuter
resultsInner, err := result.(*Tree).QueryIndex(4)
if err != nil {
panic(fmt.Sprintf("Failed to query inner tree: %s", err.Error()))
}
result = <-resultsInner
fmt.Println("Found:", result.(string))
// Output: Found: hello, world
}
func ExampleTree_Clear() {
tree := new(Tree)
tree.Push(1, 10, "hello, world")
tree.BuildTree()
tree.Clear()
_, err := tree.QueryIndex(4)
if err != nil {
// The tree is empty. Trying to Query it will fail
fmt.Println(fmt.Sprintf("Failed to query tree: %s", err.Error()))
}
tree.Push(1, 10, "I destroyed the world")
tree.BuildTree()
results, err := tree.QueryIndex(4)
if err != nil {
panic(fmt.Sprintf("Failed to query tree: %s", err.Error()))
}
result := <-results
// "hello world" will not be found
fmt.Println("Found:", result)
// Output:
// Failed to query tree: Tree is empty. Build the tree first
// Found: I destroyed the world
}
func ExampleTree_Print() {
tree := new(Tree)
tree.Push(1, 10, "hello, world")
tree.Push(5, 6, "how are you today?")
tree.Push(9, 45, "test")
tree.BuildTree()
tree.Print()
// Output is a pretty tree (note that the leafs are not always placed properly)
}