-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimpleBinaryTree.go
296 lines (253 loc) · 5.55 KB
/
simpleBinaryTree.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package bosc
import (
"errors"
)
func init() {
register("simple_binary_tree", func() BinarySearchTree { return newSimpleBinaryTree() })
}
type simpleBinaryTree struct {
root *simpleBinaryTreeNode
}
func newSimpleBinaryTree() *simpleBinaryTree {
return &simpleBinaryTree{}
}
func (t *simpleBinaryTree) Add(val Comparable) error {
if t.root == nil {
t.root = newSimpleBinaryTreeNode(val)
return nil
}
return t.root.Add(val)
}
func (t *simpleBinaryTree) Count() uint64 {
if t.root==nil {
return 0
}
return t.root.Count()
}
func (t *simpleBinaryTree) Find(val Comparable) (Comparable, error) {
if t.root == nil {
return nil, errors.New("Not Found. Tree is empty")
}
node, _, err := t.root.Find(val, nil)
if node!=nil {
return node.val, err
}else {
return nil, err
}
}
func (t *simpleBinaryTree) Remove(val Comparable) (found bool) {
if t.root == nil {
return false
}
// We are removing the root value.
if t.root.val.Compare(val)==0 {
// There is only one node with no childs
if t.root.left == nil && t.root.right == nil {
t.root = nil
return true
}
if t.root.left != nil && t.root.right == nil { //Childs only at left side
t.root = t.root.left
return true
}
if t.root.left == nil && t.root.right != nil { // Childs only ar right side
t.root = t.root.right
return true
}
// Rest of cases can be handled by node.remove()
}
return t.root.Remove(val)
}
func (t *simpleBinaryTree) Range(valFrom Comparable, valTo Comparable, fn func(val Comparable)) {
if t.root != nil {
t.root.Range(valFrom, valTo, fn)
}
}
func (t *simpleBinaryTree) RangeAll(fn func(val Comparable)) {
if t.root != nil {
t.root.RangeAll(fn)
}
}
func (t *simpleBinaryTree) RangeFrom(val Comparable, fn func(node Comparable)) {
if t.root != nil {
t.root.RangeFrom(val, fn)
}
}
func (t *simpleBinaryTree) RangeTo(val Comparable, fn func(node Comparable)) {
if t.root != nil {
t.root.RangeTo(val, fn)
}
}
func (t *simpleBinaryTree) Min() Comparable {
if t.root == nil {
return nil
} else {
return t.root.Min()
}
}
func (t *simpleBinaryTree) Max() Comparable {
if t.root == nil {
return nil
} else {
return t.root.Max()
}
}
type simpleBinaryTreeNode struct {
val Comparable
left *simpleBinaryTreeNode
right *simpleBinaryTreeNode
}
func newSimpleBinaryTreeNode(val Comparable) *simpleBinaryTreeNode {
return &simpleBinaryTreeNode{val: val}
}
func (n *simpleBinaryTreeNode) Val() Comparable {
return n.val
}
func (n *simpleBinaryTreeNode) Add(val Comparable) error {
compare := val.Compare(n.val)
switch {
case compare == 0:
return errors.New("Duplicated value.")
case compare < 0:
if n.left != nil {
return n.left.Add(val)
} else {
n.left = newSimpleBinaryTreeNode(val)
}
case compare > 0:
if n.right != nil {
return n.right.Add(val)
} else {
n.right = newSimpleBinaryTreeNode(val)
}
}
return nil
}
func (n *simpleBinaryTreeNode) Count() (count uint64) {
count=1
if n.left!=nil {
count += n.left.Count()
}
if n.right!=nil {
count += n.right.Count()
}
return count
}
func (n *simpleBinaryTreeNode) Find(val Comparable, father *simpleBinaryTreeNode) (this *simpleBinaryTreeNode, parent *simpleBinaryTreeNode, err error) {
switch {
case val.Compare(n.val) == 0:
return n, father, nil
case val.Compare(n.val) < 0:
if n.left != nil {
return n.left.Find(val, n)
}
case val.Compare(n.val) > 0:
if n.right != nil {
return n.right.Find(val, n)
}
}
return nil, father, errors.New("Not Found.")
}
func (n *simpleBinaryTreeNode) Remove(val Comparable) (found bool) {
node, parent, err := n.Find(val, nil)
if err != nil {
return false
}
// Node has no childs
if node.left == nil && node.right == nil {
if parent.left == node {
parent.left = nil
return true
}
if parent.right == node {
parent.right = nil
return true
}
}
// Node has right childs and no left childs
if node.left == nil && node.right != nil {
if parent.left == node {
parent.left = node.right
} else {
parent.right = node.right
}
return true
}
// Node has left childs and no right childs
if node.left != nil && node.right == nil {
if parent.left == node {
parent.left = node.left
} else {
parent.right = node.left
}
return true
}
// Node has left and right childs
if node.left != nil && node.right != nil {
min := node.right.Min()
node.Remove(min)
node.val = min
return true
}
return false // Never happens
}
func (n *simpleBinaryTreeNode) Range(valFrom Comparable, valTo Comparable, fn func(val Comparable)) {
if n.left != nil {
n.left.Range(valFrom, valTo, fn)
}
if n.val.Compare(valFrom) >= 0 && n.val.Compare(valTo) <= 0 {
fn(n.val)
}
if n.right != nil {
n.right.Range(valFrom, valTo, fn)
}
}
func (n *simpleBinaryTreeNode) RangeAll(fn func(val Comparable)) {
if n.left != nil {
n.left.RangeAll(fn)
}
fn(n.val)
if n.right != nil {
n.right.RangeAll(fn)
}
}
func (n *simpleBinaryTreeNode) RangeFrom(from Comparable, fn func(node Comparable)) {
if n.left != nil {
n.left.RangeFrom(from, fn)
}
if n.val.Compare(from) >= 0 {
fn(n.val)
}
if n.right != nil {
n.right.RangeFrom(from, fn)
}
}
func (n *simpleBinaryTreeNode) RangeTo(to Comparable, fn func(node Comparable)) {
if n.left != nil {
n.left.RangeTo(to, fn)
}
if n.val.Compare(to) <= 0 {
fn(n.val)
}
if n.right != nil {
n.right.RangeTo(to, fn)
}
}
func (n *simpleBinaryTreeNode) Min() Comparable {
p := n
for {
if p.left==nil {
return p.val
}
p = p.left
}
}
func (n *simpleBinaryTreeNode) Max() Comparable {
p := n
for {
if p.right==nil {
return p.val
}
p = p.right
}
}