-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample_tree_generator_test.go
149 lines (118 loc) · 2.81 KB
/
example_tree_generator_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
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
package avl_test
import (
"fmt"
"github.com/spikeekips/avl"
"golang.org/x/xerrors"
)
// ExampleMutableNode implements MutableNode. ExampleMutableNode has value field
// to store custom value.
type ExampleMutableNode struct {
key []byte
height int16
left avl.MutableNode
right avl.MutableNode
value int
}
func (em *ExampleMutableNode) Key() []byte {
return em.key
}
func (em *ExampleMutableNode) Height() int16 {
return em.height
}
func (em *ExampleMutableNode) SetHeight(height int16) error {
if height < 0 {
return xerrors.Errorf("height must be greater than zero; height=%d", height)
}
em.height = height
return nil
}
func (em *ExampleMutableNode) Left() avl.MutableNode {
return em.left
}
func (em *ExampleMutableNode) LeftKey() []byte {
if em.left == nil {
return nil
}
return em.left.Key()
}
func (em *ExampleMutableNode) SetLeft(node avl.MutableNode) error {
if node == nil {
em.left = nil
return nil
}
if avl.EqualKey(em.key, node.Key()) {
return xerrors.Errorf("left is same node; key=%v", em.key)
}
em.left = node
return nil
}
func (em *ExampleMutableNode) Right() avl.MutableNode {
return em.right
}
func (em *ExampleMutableNode) RightKey() []byte {
if em.right == nil {
return nil
}
return em.right.Key()
}
func (em *ExampleMutableNode) SetRight(node avl.MutableNode) error {
if node == nil {
em.right = nil
return nil
}
if avl.EqualKey(em.key, node.Key()) {
return xerrors.Errorf("right is same node; key=%v", em.key)
}
em.right = node
return nil
}
func (em *ExampleMutableNode) Merge(node avl.MutableNode) error {
e, ok := node.(*ExampleMutableNode)
if !ok {
return xerrors.Errorf("merge node is not *ExampleMutableNode; node=%T", node)
}
em.value = e.value
return nil
}
func ExampleTreeGenerator() {
// create new TreeGenerator
tg := avl.NewTreeGenerator()
fmt.Println("> trying to generate new tree")
// Generate 10 new MutableNodes and add to TreeGenerator.
for i := 0; i < 10; i++ {
node := &ExampleMutableNode{
key: []byte(fmt.Sprintf("%03d", i)),
}
if _, err := tg.Add(node); err != nil {
fmt.Printf("error: failed to add node: %v\n", err)
return
}
fmt.Printf("> node added: key=%s\n", string(node.Key()))
}
// Get Tree from TreeGenerator.
tree, err := tg.Tree()
if err != nil {
fmt.Printf("error: failed to get Tree from generator: %v\n", err)
return
}
// Check all the nodes is added in Tree.
var i int
_ = tree.Traverse(func(node avl.Node) (bool, error) {
fmt.Printf(
"%d: node loaded: key=%s height=%d left=%s right=%s\n",
i,
string(node.Key()),
node.Height(),
string(node.LeftKey()),
string(node.RightKey()),
)
i++
return true, nil
})
// check Tree is valid or not
if err := tree.IsValid(); err != nil {
fmt.Printf("tree is invalid: %v\n", err)
return
}
fmt.Println("< tree is valid")
}