/
avltree.go
475 lines (424 loc) · 9.59 KB
/
avltree.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
// Copyright (c) 2017, Benjamin Scher Purcell. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package avltree implements an AVL balanced binary tree.
//
// Structure is not thread safe.
//
// References: https://en.wikipedia.org/wiki/AVL_tree
package avltree
import (
"fmt"
"github.com/emirpasic/gods/trees"
"github.com/emirpasic/gods/utils"
)
func assertTreeImplementation() {
var _ trees.Tree = new(Tree)
}
// Tree holds elements of the AVL tree.
type Tree struct {
Root *Node // Root node
Comparator utils.Comparator // Key comparator
size int // Total number of keys in the tree
}
// Node is a single element within the tree
type Node struct {
Key interface{}
Value interface{}
Parent *Node // Parent node
Children [2]*Node // Children nodes
b int8
}
// NewWith instantiates an AVL tree with the custom comparator.
func NewWith(comparator utils.Comparator) *Tree {
return &Tree{Comparator: comparator}
}
// NewWithIntComparator instantiates an AVL tree with the IntComparator, i.e. keys are of type int.
func NewWithIntComparator() *Tree {
return &Tree{Comparator: utils.IntComparator}
}
// NewWithStringComparator instantiates an AVL tree with the StringComparator, i.e. keys are of type string.
func NewWithStringComparator() *Tree {
return &Tree{Comparator: utils.StringComparator}
}
// Put inserts node into the tree.
// Key should adhere to the comparator's type assertion, otherwise method panics.
func (t *Tree) Put(key interface{}, value interface{}) {
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 *Tree) Get(key interface{}) (value interface{}, found bool) {
n := t.GetNode(key)
if n != nil {
return n.Value, true
}
return nil, false
}
// GetNode searches the node in the tree by key and returns its node or nil if key is not found in tree.
// Key should adhere to the comparator's type assertion, otherwise method panics.
func (t *Tree) GetNode(key interface{}) *Node {
n := t.Root
for n != nil {
cmp := t.Comparator(key, n.Key)
switch {
case cmp == 0:
return n
case cmp < 0:
n = n.Children[0]
case cmp > 0:
n = n.Children[1]
}
}
return n
}
// Remove remove the node from the tree by key.
// Key should adhere to the comparator's type assertion, otherwise method panics.
func (t *Tree) Remove(key interface{}) {
t.remove(key, &t.Root)
}
// Empty returns true if tree does not contain any nodes.
func (t *Tree) Empty() bool {
return t.size == 0
}
// Size returns the number of elements stored in the tree.
func (t *Tree) Size() int {
return t.size
}
// Size returns the number of elements stored in the subtree.
// Computed dynamically on each call, i.e. the subtree is traversed to count the number of the nodes.
func (n *Node) Size() int {
if n == nil {
return 0
}
size := 1
if n.Children[0] != nil {
size += n.Children[0].Size()
}
if n.Children[1] != nil {
size += n.Children[1].Size()
}
return size
}
// Keys returns all keys in-order
func (t *Tree) Keys() []interface{} {
keys := make([]interface{}, t.size)
it := t.Iterator()
for i := 0; it.Next(); i++ {
keys[i] = it.Key()
}
return keys
}
// Values returns all values in-order based on the key.
func (t *Tree) Values() []interface{} {
values := make([]interface{}, t.size)
it := t.Iterator()
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 *Tree) Left() *Node {
return t.bottom(0)
}
// Right returns the maximum element of the AVL tree
// or nil if the tree is empty.
func (t *Tree) Right() *Node {
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 *Tree) Floor(key interface{}) (floor *Node, found bool) {
found = false
n := t.Root
for n != nil {
c := t.Comparator(key, n.Key)
switch {
case c == 0:
return n, true
case c < 0:
n = n.Children[0]
case c > 0:
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 *Tree) Ceiling(key interface{}) (floor *Node, found bool) {
found = false
n := t.Root
for n != nil {
c := t.Comparator(key, n.Key)
switch {
case c == 0:
return n, true
case c < 0:
floor, found = n, true
n = n.Children[0]
case c > 0:
n = n.Children[1]
}
}
if found {
return
}
return nil, false
}
// Clear removes all nodes from the tree.
func (t *Tree) Clear() {
t.Root = nil
t.size = 0
}
// String returns a string representation of container
func (t *Tree) String() string {
str := "AVLTree\n"
if !t.Empty() {
output(t.Root, "", true, &str)
}
return str
}
func (n *Node) String() string {
return fmt.Sprintf("%v", n.Key)
}
func (t *Tree) put(key interface{}, value interface{}, p *Node, qp **Node) bool {
q := *qp
if q == nil {
t.size++
*qp = &Node{Key: key, Value: value, Parent: p}
return true
}
c := t.Comparator(key, q.Key)
if c == 0 {
q.Key = key
q.Value = value
return false
}
if c < 0 {
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 *Tree) remove(key interface{}, qp **Node) bool {
q := *qp
if q == nil {
return false
}
c := t.Comparator(key, q.Key)
if c == 0 {
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
}
if c < 0 {
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 **Node, minKey *interface{}, minVal *interface{}) 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 **Node) 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 **Node) 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 *Node) *Node {
s.b = 0
s = rotate(c, s)
s.b = 0
return s
}
func doublerot(c int8, s *Node) *Node {
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 *Node) *Node {
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 *Tree) bottom(d int) *Node {
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 *Node) Prev() *Node {
return n.walk1(0)
}
// Next returns the next element in an inorder
// walk of the AVL tree.
func (n *Node) Next() *Node {
return n.walk1(1)
}
func (n *Node) walk1(a int) *Node {
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 *Node, 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)
}
}