-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbtree.go
575 lines (505 loc) · 11.2 KB
/
btree.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
package tree
import (
"fmt"
)
// Btree represents an AVL tree
type Btree struct {
root *Node
values []Val
len int
}
// Val interface to define the compare method used to insert and find values
type Val interface {
Comp(val Val) int8
}
// Node represents a node in the tree with a value, left and right children, and a height/balance of the node.
type Node struct {
Value Val
left, right *Node
height int8
}
// New returns a new btree
func New() *Btree { return new(Btree).Init() }
// Init initializes all values/clears the tree and returns the tree pointer
func (t *Btree) Init() *Btree {
t.root = nil
t.values = nil
t.len = 0
return t
}
// String returns a string representation of the tree values
func (t *Btree) String() string {
return fmt.Sprint(t.Values())
}
// Empty returns true if the tree is empty
func (t *Btree) Empty() bool {
return t.root == nil
}
// NotEmpty returns true if the tree is not empty
func (t *Btree) NotEmpty() bool {
return t.root != nil
}
func (t *Btree) balance() int8 {
if t.root != nil {
return balance(t.root)
}
return 0
}
// Insert inserts a new value into the tree and returns the tree pointer
func (t *Btree) Insert(value Val) *Btree {
added := false
t.root = insert(t.root, value, &added)
if added {
t.len++
}
t.values = nil
return t
}
func insert(n *Node, value Val, added *bool) *Node {
if n == nil {
*added = true
return (&Node{Value: value}).Init()
}
c := value.Comp(n.Value)
if c > 0 {
n.right = insert(n.right, value, added)
} else if c < 0 {
n.left = insert(n.left, value, added)
} else {
n.Value = value
*added = false
return n
}
n.height = n.maxHeight() + 1
c = balance(n)
if c > 1 {
c = value.Comp(n.left.Value)
if c < 0 {
return n.rotateRight()
} else if c > 0 {
n.left = n.left.rotateLeft()
return n.rotateRight()
}
} else if c < -1 {
c = value.Comp(n.right.Value)
if c > 0 {
return n.rotateLeft()
} else if c < 0 {
n.right = n.right.rotateRight()
return n.rotateLeft()
}
}
return n
}
// InsertAll inserts all the values into the tree and returns the tree pointer
func (t *Btree) InsertAll(values []Val) *Btree {
for _, v := range values {
t.Insert(v)
}
return t
}
// Contains returns true if the tree contains the specified value
func (t *Btree) Contains(value Val) bool {
return t.Get(value) != nil
}
// ContainsAny returns true if the tree contains any of the values
func (t *Btree) ContainsAny(values []Val) bool {
for _, v := range values {
if t.Contains(v) {
return true
}
}
return false
}
// ContainsAll returns true if the tree contains all of the values
func (t *Btree) ContainsAll(values []Val) bool {
for _, v := range values {
if !t.Contains(v) {
return false
}
}
return true
}
// Get returns the node value associated with the search value
func (t *Btree) Get(value Val) Val {
var node *Node
if t.root != nil {
node = t.root.get(value)
}
if node != nil {
return node.Value
}
return nil
}
// Len return the number of nodes in the tree
func (t *Btree) Len() int {
return t.len
}
// Head returns the first value in the tree
func (t *Btree) Head() Val {
if t.root == nil {
return nil
}
var beginning = t.root
for beginning.left != nil {
beginning = beginning.left
}
if beginning == nil {
for beginning.right != nil {
beginning = beginning.right
}
}
if beginning != nil {
return beginning.Value
}
return nil
}
// Tail returns the last value in the tree
func (t *Btree) Tail() Val {
if t.root == nil {
return nil
}
var beginning = t.root
for beginning.right != nil {
beginning = beginning.right
}
if beginning == nil {
for beginning.left != nil {
beginning = beginning.left
}
}
if beginning != nil {
return beginning.Value
}
return nil
}
// Values returns a slice of all the values in tree in order
func (t *Btree) Values() []Val {
if t.values == nil {
t.values = make([]Val, t.len)
t.Ascend(func(n *Node, i int) bool {
t.values[i] = n.Value
return true
})
}
return t.values
}
// Delete deletes the node from the tree associated with the search value
func (t *Btree) Delete(value Val) *Btree {
deleted := false
t.root = deleteNode(t.root, value, &deleted)
if deleted {
t.len--
}
t.values = nil
return t
}
// DeleteAll deletes the nodes from the tree associated with the search values
func (t *Btree) DeleteAll(values []Val) *Btree {
for _, v := range values {
t.Delete(v)
}
return t
}
func deleteNode(n *Node, value Val, deleted *bool) *Node {
if n == nil {
return n
}
c := value.Comp(n.Value)
if c < 0 {
n.left = deleteNode(n.left, value, deleted)
} else if c > 0 {
n.right = deleteNode(n.right, value, deleted)
} else {
if n.left == nil {
t := n.right
n.Init()
return t
} else if n.right == nil {
t := n.left
n.Init()
return t
}
t := n.right.min()
n.Value = t.Value
n.right = deleteNode(n.right, t.Value, deleted)
*deleted = true
}
//re-balance
if n == nil {
return n
}
n.height = n.maxHeight() + 1
bal := balance(n)
if bal > 1 {
if balance(n.left) >= 0 {
return n.rotateRight()
}
n.left = n.left.rotateLeft()
return n.rotateRight()
} else if bal < -1 {
if balance(n.right) <= 0 {
return n.rotateLeft()
}
n.right = n.right.rotateRight()
return n.rotateLeft()
}
return n
}
// Pop deletes the last node from the tree and returns its value
func (t *Btree) Pop() Val {
value := t.Tail()
if value != nil {
t.Delete(value)
}
return value
}
// Pull deletes the first node from the tree and returns its value
func (t *Btree) Pull() Val {
value := t.Head()
if value != nil {
t.Delete(value)
}
return value
}
// NodeIterator expresses the iterator function used for traversals
type NodeIterator func(n *Node, i int) bool
// Ascend performs an ascending order traversal of the tree calling the iterator function on each node
// the iterator will continue as long as the NodeIterator returns true
func (t *Btree) Ascend(iterator NodeIterator) {
var i int
if t.root != nil {
t.root.iterate(iterator, &i, true)
}
}
// Descend performs a descending order traversal of the tree using the iterator
// the iterator will continue as long as the NodeIterator returns true
func (t *Btree) Descend(iterator NodeIterator) {
var i int
if t.root != nil {
t.root.rIterate(iterator, &i, true)
}
}
// Debug prints out useful debug information about the tree for debugging purposes
func (t *Btree) Debug() {
fmt.Println("----------------------------------------------------------------------------------------------")
if t.Empty() {
fmt.Println("tree is empty")
} else {
fmt.Println(t.Len(), "elements")
}
t.Ascend(func(n *Node, i int) bool {
if t.root.Value == n.Value {
fmt.Print("ROOT ** ")
}
n.Debug()
return true
})
fmt.Println("----------------------------------------------------------------------------------------------")
}
// Init initializes the values of the node or clears the node and returns the node pointer
func (n *Node) Init() *Node {
n.height = 1
n.left = nil
n.right = nil
return n
}
// String returns a string representing the node
func (n *Node) String() string {
return fmt.Sprint(n.Value)
}
// Debug prints out useful debug information about the tree node for debugging purposes
func (n *Node) Debug() {
var children string
if n.left == nil && n.right == nil {
children = "no children |"
} else if n.left != nil && n.right != nil {
children = fmt.Sprint("left child:", n.left.String(), " right child:", n.right.String())
} else if n.right != nil {
children = fmt.Sprint("right child:", n.right.String())
} else {
children = fmt.Sprint("left child:", n.left.String())
}
fmt.Println(n.String(), "|", "height", n.height, "|", "balance", balance(n), "|", children)
}
func height(n *Node) int8 {
if n != nil {
return n.height
}
return 0
}
func balance(n *Node) int8 {
if n == nil {
return 0
}
return height(n.left) - height(n.right)
}
func (n *Node) get(val Val) *Node {
var node *Node
c := val.Comp(n.Value)
if c < 0 {
if n.left != nil {
node = n.left.get(val)
}
} else if c > 0 {
if n.right != nil {
node = n.right.get(val)
}
} else {
node = n
}
return node
}
func (n *Node) rotateRight() *Node {
l := n.left
// Rotation
l.right, n.left = n, l.right
// update heights
n.height = n.maxHeight() + 1
l.height = l.maxHeight() + 1
return l
}
func (n *Node) rotateLeft() *Node {
r := n.right
// Rotation
r.left, n.right = n, r.left
// update heights
n.height = n.maxHeight() + 1
r.height = r.maxHeight() + 1
return r
}
func (n *Node) iterate(iterator NodeIterator, i *int, cont bool) {
if n != nil && cont {
n.left.iterate(iterator, i, cont)
cont = iterator(n, *i)
*i++
n.right.iterate(iterator, i, cont)
}
}
func (n *Node) rIterate(iterator NodeIterator, i *int, cont bool) {
if n != nil && cont {
n.right.iterate(iterator, i, cont)
cont = iterator(n, *i)
*i++
n.left.iterate(iterator, i, cont)
}
}
func (n *Node) min() *Node {
current := n
for current.left != nil {
current = current.left
}
return current
}
func (n *Node) maxHeight() int8 {
rh := height(n.right)
lh := height(n.left)
if rh > lh {
return rh
}
return lh
}
// IntVal represents an integer tree val
type IntVal int
// Comp returns 1 if i > val, -1 if i < val and 0 if i equal to val
func (i IntVal) Comp(val Val) int8 {
v := val.(IntVal)
if i > v {
return 1
} else if i < v {
return -1
} else {
return 0
}
}
// StringVal represents an string tree val
type StringVal string
// Comp returns 1 if i > val, -1 if i < val and 0 if i equal to val
func (i StringVal) Comp(val Val) int8 {
v := val.(StringVal)
if i > v {
return 1
} else if i < v {
return -1
} else {
return 0
}
}
// UintVal represents an uint tree val
type UintVal uint
// Comp returns 1 if i > val, -1 if i < val and 0 if i equal to val
func (i UintVal) Comp(val Val) int8 {
v := val.(UintVal)
if i > v {
return 1
} else if i < v {
return -1
} else {
return 0
}
}
// Float32Val represents an float32 tree val
type Float32Val float32
// Comp returns 1 if i > val, -1 if i < val and 0 if i equal to val
func (i Float32Val) Comp(val Val) int8 {
v := val.(Float32Val)
if i > v {
return 1
} else if i < v {
return -1
} else {
return 0
}
}
// Float64Val represents an float64 tree val
type Float64Val float64
// Comp returns 1 if i > val, -1 if i < val and 0 if i equal to val
func (i Float64Val) Comp(val Val) int8 {
v := val.(Float64Val)
if i > v {
return 1
} else if i < v {
return -1
} else {
return 0
}
}
// UintptrVal represents a uintptr tree val
type UintptrVal uintptr
// Comp returns 1 if i > val, -1 if i < val and 0 if i equal to val
func (i UintptrVal) Comp(val Val) int8 {
v := val.(UintptrVal)
if i > v {
return 1
} else if i < v {
return -1
} else {
return 0
}
}
// RuneVal represents a rune tree val
type RuneVal rune
// Comp returns 1 if i > val, -1 if i < val and 0 if i equal to val
func (i RuneVal) Comp(val Val) int8 {
v := val.(RuneVal)
if i > v {
return 1
} else if i < v {
return -1
} else {
return 0
}
}
// ByteVal represents a byte tree val
type ByteVal byte
// Comp returns 1 if i > val, -1 if i < val and 0 if i equal to val
func (i ByteVal) Comp(val Val) int8 {
v := val.(ByteVal)
if i > v {
return 1
} else if i < v {
return -1
} else {
return 0
}
}