-
Notifications
You must be signed in to change notification settings - Fork 0
/
ds-avltree
520 lines (462 loc) · 10.8 KB
/
ds-avltree
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
# name: AVL Tree
# key: ds/avltree
# --
// Tree holds elements of the AVL tree.
type ${1:Tree} struct {
Root *$1Node // Root node
size int // Total number of keys in the tree
}
// $1Node is a single element within the tree
type $1Node struct {
Key ${2:int}
Value ${3:string}
Parent *$1Node // Parent node
Children [2]*$1Node // Children nodes
b int8
}
// Put inserts node into the tree.
// Key should adhere to the comparator's type assertion, otherwise method panics.
func (t *$1) Put(key $2, value $3) {
t.put(key, value, nil, &t.Root)
}
// Get searches the node in the tree by key and returns its value or nil if key is not found in tree.
// Second return parameter is true if key was found, otherwise false.
// Key should adhere to the comparator's type assertion, otherwise method panics.
func (t *$1) Get(key $2) (value $3, found bool) {
n := t.Root
for n != nil {
switch {
case key == n.Key:
return n.Value, true
case key < n.Key:
n = n.Children[0]
default:
n = n.Children[1]
}
}
return ${4:""}, false
}
// Remove remove the node from the tree by key.
// Key should adhere to the comparator's type assertion, otherwise method panics.
func (t *$1) Remove(key $2) {
t.remove(key, &t.Root)
}
// Empty returns true if tree does not contain any nodes.
func (t *$1) Empty() bool {
return t.size == 0
}
// Size returns the number of elements stored in the tree.
func (t *$1) Size() int {
return t.size
}
// Keys returns all keys in-order
func (t *$1) Keys() []$2 {
keys := make([]$2, t.size)
it := t.$1Iterator()
for i := 0; it.Next(); i++ {
keys[i] = it.Key()
}
return keys
}
// Values returns all values in-order based on the key.
func (t *$1) Values() []$3 {
values := make([]$3, t.size)
it := t.$1Iterator()
for i := 0; it.Next(); i++ {
values[i] = it.Value()
}
return values
}
// Left returns the minimum element of the AVL tree
// or nil if the tree is empty.
func (t *$1) Left() *$1Node {
return t.bottom(0)
}
// Right returns the maximum element of the AVL tree
// or nil if the tree is empty.
func (t *$1) Right() *$1Node {
return t.bottom(1)
}
// Floor Finds floor node of the input key, return the floor node or nil if no ceiling is found.
// Second return parameter is true if floor was found, otherwise false.
//
// Floor node is defined as the largest node that is smaller than or equal to the given node.
// A floor node may not be found, either because the tree is empty, or because
// all nodes in the tree is larger than the given node.
//
// Key should adhere to the comparator's type assertion, otherwise method panics.
func (t *$1) Floor(key $2) (floor *$1Node, found bool) {
found = false
n := t.Root
for n != nil {
switch {
case key == n.Key:
return n, true
case key < n.Key:
n = n.Children[0]
default:
floor, found = n, true
n = n.Children[1]
}
}
if found {
return
}
return nil, false
}
// Ceiling finds ceiling node of the input key, return the ceiling node or nil if no ceiling is found.
// Second return parameter is true if ceiling was found, otherwise false.
//
// Ceiling node is defined as the smallest node that is larger than or equal to the given node.
// A ceiling node may not be found, either because the tree is empty, or because
// all nodes in the tree is smaller than the given node.
//
// Key should adhere to the comparator's type assertion, otherwise method panics.
func (t *$1) Ceiling(key $2) (floor *$1Node, found bool) {
found = false
n := t.Root
for n != nil {
switch {
case key == n.Key:
return n, true
case key < n.Key:
floor, found = n, true
n = n.Children[0]
default:
n = n.Children[1]
}
}
if found {
return
}
return nil, false
}
// Clear removes all nodes from the tree.
func (t *$1) Clear() {
t.Root = nil
t.size = 0
}
// String returns a string representation of container
func (t *$1) String() string {
str := "AVLTree\n"
if !t.Empty() {
output(t.Root, "", true, &str)
}
return str
}
func (n *$1Node) String() string {
return fmt.Sprintf("%v", n.Key)
}
func (t *$1) put(key $2, value $3, p *$1Node, qp **$1Node) bool {
q := *qp
if q == nil {
t.size++
*qp = &TreeNode{Key: key, Value: value, Parent: p}
return true
}
if key == q.Key {
q.Key = key
q.Value = value
return false
}
var c int
if key < q.Key {
c = -1
} else {
c = 1
}
a := (c + 1) / 2
var fix bool
fix = t.put(key, value, q, &q.Children[a])
if fix {
return putFix(int8(c), qp)
}
return false
}
func (t *$1) remove(key $2, qp **$1Node) bool {
q := *qp
if q == nil {
return false
}
if key == q.Key {
t.size--
if q.Children[1] == nil {
if q.Children[0] != nil {
q.Children[0].Parent = q.Parent
}
*qp = q.Children[0]
return true
}
fix := removeMin(&q.Children[1], &q.Key, &q.Value)
if fix {
return removeFix(-1, qp)
}
return false
}
var c int
if key < q.Key {
c = -1
} else {
c = 1
}
a := (c + 1) / 2
fix := t.remove(key, &q.Children[a])
if fix {
return removeFix(int8(-c), qp)
}
return false
}
func removeMin(qp **$1Node, minKey *$2, minVal *$3) bool {
q := *qp
if q.Children[0] == nil {
*minKey = q.Key
*minVal = q.Value
if q.Children[1] != nil {
q.Children[1].Parent = q.Parent
}
*qp = q.Children[1]
return true
}
fix := removeMin(&q.Children[0], minKey, minVal)
if fix {
return removeFix(1, qp)
}
return false
}
func putFix(c int8, t **$1Node) bool {
s := *t
if s.b == 0 {
s.b = c
return true
}
if s.b == -c {
s.b = 0
return false
}
if s.Children[(c+1)/2].b == c {
s = singlerot(c, s)
} else {
s = doublerot(c, s)
}
*t = s
return false
}
func removeFix(c int8, t **$1Node) bool {
s := *t
if s.b == 0 {
s.b = c
return false
}
if s.b == -c {
s.b = 0
return true
}
a := (c + 1) / 2
if s.Children[a].b == 0 {
s = rotate(c, s)
s.b = -c
*t = s
return false
}
if s.Children[a].b == c {
s = singlerot(c, s)
} else {
s = doublerot(c, s)
}
*t = s
return true
}
func singlerot(c int8, s *$1Node) *$1Node {
s.b = 0
s = rotate(c, s)
s.b = 0
return s
}
func doublerot(c int8, s *$1Node) *$1Node {
a := (c + 1) / 2
r := s.Children[a]
s.Children[a] = rotate(-c, s.Children[a])
p := rotate(c, s)
switch {
default:
s.b = 0
r.b = 0
case p.b == c:
s.b = -c
r.b = 0
case p.b == -c:
s.b = 0
r.b = c
}
p.b = 0
return p
}
func rotate(c int8, s *$1Node) *$1Node {
a := (c + 1) / 2
r := s.Children[a]
s.Children[a] = r.Children[a^1]
if s.Children[a] != nil {
s.Children[a].Parent = s
}
r.Children[a^1] = s
r.Parent = s.Parent
s.Parent = r
return r
}
func (t *$1) bottom(d int) *$1Node {
n := t.Root
if n == nil {
return nil
}
for c := n.Children[d]; c != nil; c = n.Children[d] {
n = c
}
return n
}
// Prev returns the previous element in an inorder
// walk of the AVL tree.
func (n *$1Node) Prev() *$1Node {
return n.walk1(0)
}
// Next returns the next element in an inorder
// walk of the AVL tree.
func (n *$1Node) Next() *$1Node {
return n.walk1(1)
}
func (n *$1Node) walk1(a int) *$1Node {
if n == nil {
return nil
}
if n.Children[a] != nil {
n = n.Children[a]
for n.Children[a^1] != nil {
n = n.Children[a^1]
}
return n
}
p := n.Parent
for p != nil && p.Children[a] == n {
n = p
p = p.Parent
}
return p
}
func output(node *$1Node, prefix string, isTail bool, str *string) {
if node.Children[1] != nil {
newPrefix := prefix
if isTail {
newPrefix += "│ "
} else {
newPrefix += " "
}
output(node.Children[1], newPrefix, false, str)
}
*str += prefix
if isTail {
*str += "└── "
} else {
*str += "┌── "
}
*str += node.String() + "\n"
if node.Children[0] != nil {
newPrefix := prefix
if isTail {
newPrefix += " "
} else {
newPrefix += "│ "
}
output(node.Children[0], newPrefix, true, str)
}
}
// $1Iterator holding the iterator's state
type $1Iterator struct {
tree *$1
node *$1Node
position position
}
type position byte
const (
begin, between, end position = 0, 1, 2
)
// $1Iterator returns a stateful iterator whose elements are key/value pairs.
func (tree *$1) $1Iterator() *$1Iterator {
return &$1Iterator{tree: tree, node: nil, position: begin}
}
// Next moves the iterator to the next element and returns true if there was a next element in the container.
// If Next() returns true, then next element's key and value can be retrieved by Key() and Value().
// If Next() was called for the first time, then it will point the iterator to the first element if it exists.
// Modifies the state of the iterator.
func (iterator *$1Iterator) Next() bool {
switch iterator.position {
case begin:
iterator.position = between
iterator.node = iterator.tree.Left()
case between:
iterator.node = iterator.node.Next()
}
if iterator.node == nil {
iterator.position = end
return false
}
return true
}
// Prev moves the iterator to the next element and returns true if there was a previous element in the container.
// If Prev() returns true, then next element's key and value can be retrieved by Key() and Value().
// If Prev() was called for the first time, then it will point the iterator to the first element if it exists.
// Modifies the state of the iterator.
func (iterator *$1Iterator) Prev() bool {
switch iterator.position {
case end:
iterator.position = between
iterator.node = iterator.tree.Right()
case between:
iterator.node = iterator.node.Prev()
}
if iterator.node == nil {
iterator.position = begin
return false
}
return true
}
// Value returns the current element's value.
// Does not modify the state of the iterator.
func (iterator *$1Iterator) Value() $3 {
if iterator.node == nil {
return $4
}
return iterator.node.Value
}
// Key returns the current element's key.
// Does not modify the state of the iterator.
func (iterator *$1Iterator) Key() $2 {
if iterator.node == nil {
return $5
}
return iterator.node.Key
}
// Begin resets the iterator to its initial state (one-before-first)
// Call Next() to fetch the first element if any.
func (iterator *$1Iterator) Begin() {
iterator.node = nil
iterator.position = begin
}
// End moves the iterator past the last element (one-past-the-end).
// Call Prev() to fetch the last element if any.
func (iterator *$1Iterator) End() {
iterator.node = nil
iterator.position = end
}
// First moves the iterator to the first element and returns true if there was a first element in the container.
// If First() returns true, then first element's key and value can be retrieved by Key() and Value().
// Modifies the state of the iterator
func (iterator *$1Iterator) First() bool {
iterator.Begin()
return iterator.Next()
}
// Last moves the iterator to the last element and returns true if there was a last element in the container.
// If Last() returns true, then last element's key and value can be retrieved by Key() and Value().
// Modifies the state of the iterator.
func (iterator *$1Iterator) Last() bool {
iterator.End()
return iterator.Prev()
}