-
Notifications
You must be signed in to change notification settings - Fork 2
/
avtltree.go
478 lines (413 loc) · 10.9 KB
/
avtltree.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
// Copyright 2017 Google Inc. All Rights Reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package avltree provides an universal AVL tree
package avltree
import (
"fmt"
"sync"
"github.com/golang/glog"
)
// Comparable is an interface used to pass compare functions to this avltree
type Comparable func(c1 interface{}, c2 interface{}) bool
// EachFunc is an interface used to pass a function to the each() method
type EachFunc func(node *TreeNode, vals ...interface{})
// Tree represents a tree
type Tree struct {
root *TreeNode
lock sync.RWMutex
Count int
}
// TreeNode represents a node in a tree
type TreeNode struct {
left *TreeNode
right *TreeNode
key interface{}
Values []interface{}
height int64
issmaller Comparable
}
// max gives returns the maximum of a and b
func max(a, b int64) int64 {
if a > b {
return a
}
return b
}
// getHeight return the height of tree with root `root`
func (root *TreeNode) getHeight() int64 {
if root != nil {
return root.height
}
return -1
}
// TreeMinValueNode returns the node with the minimal key in the tree
func (root *TreeNode) minValueNode() *TreeNode {
for root.left != nil {
return root.left.minValueNode()
}
return nil
}
// search searches element with key `key` in tree with root `root`
// in case the searched element doesn't exist nil is returned
func (root *TreeNode) search(key interface{}) *TreeNode {
if root.key == key {
return root
}
if root.issmaller(key, root.key) {
if root.left == nil {
return nil
}
return root.left.search(key)
}
if root.right == nil {
return nil
}
return root.right.search(key)
}
// getBalance return difference of height of left and right
// subtrees of tree with root `root`
func (root *TreeNode) getBalance() int64 {
if root == nil {
return 0
}
return root.left.getHeight() - root.right.getHeight()
}
// leftRotate rotates tree with root `root` to the left and
// returns it's new root
func (root *TreeNode) leftRotate() *TreeNode {
node := root.right
root.right = node.left
node.left = root
root.height = max(root.left.getHeight(), root.right.getHeight()) + 1
node.height = max(node.right.getHeight(), node.left.getHeight()) + 1
return node
}
// leftRightRotate performs a left-right rotation of tree with root
// `root` and returns it's new root
func (root *TreeNode) leftRightRotate() *TreeNode {
root.left = root.left.leftRotate()
root = root.rightRotate()
return root
}
// rightRotate performs a right rotation of tree with root `root`
// and returns it's new root
func (root *TreeNode) rightRotate() *TreeNode {
node := root.left
root.left = node.right
node.right = root
root.height = max(root.left.getHeight(), root.right.getHeight()) + 1
node.height = max(node.left.getHeight(), node.right.getHeight()) + 1
return node
}
// rightLeftRotate preforms a right-left rotation of tree with root
// `root` and returns it's new root
func (root *TreeNode) rightLeftRotate() *TreeNode {
root.right = root.right.rightRotate()
root = root.leftRotate()
return root
}
// delete deletes node with key `key` from tree. If necessary rebalancing
// is done and new root is returned
func (root *TreeNode) delete(key interface{}) *TreeNode {
if root == nil {
return nil
}
if root.issmaller(key, root.key) {
root.left = root.left.delete(key)
} else if key == root.key {
if root.left == nil && root.right == nil {
return nil
} else if root.left == nil {
return root.right
} else if root.right == nil {
return root.left
}
tmp := root.minValueNode()
root.key = tmp.key
root.Values = tmp.Values
root.right = root.right.delete(tmp.key)
root.height = max(root.left.getHeight(), root.right.getHeight()) + 1
balance := root.getBalance()
if balance > 1 {
if root.left.getBalance() >= 0 {
return root.rightRotate()
}
return root.leftRightRotate()
} else if balance < -1 {
if root.right.getBalance() <= 0 {
return root.leftRotate()
}
return root.rightLeftRotate()
}
} else {
root.right = root.right.delete(key)
}
return root
}
// isEqual is a generic function that compares a and b of any comparable type
// return true if a and b are equal, otherwise false
func isEqual(a interface{}, b interface{}) bool {
return a == b
}
// New simply returns a new (empty) tree
func New() *Tree {
return &Tree{}
}
// Insert inserts an element to tree with root `t`
func (t *Tree) Insert(key interface{}, value interface{}, issmaller Comparable) (new *TreeNode, err error) {
if t == nil {
return nil, fmt.Errorf("unable to insert into nil tree")
}
t.lock.Lock()
defer t.lock.Unlock()
t.root, new = t.root.insert(key, value, issmaller)
t.Count++
return new, nil
}
// insert inserts an element into tree with root `root`
func (root *TreeNode) insert(key interface{}, value interface{}, issmaller Comparable) (*TreeNode, *TreeNode) {
if root == nil {
root = &TreeNode{
left: nil,
right: nil,
key: key,
Values: []interface{}{
value,
},
height: 0,
issmaller: issmaller,
}
return root, root
}
if isEqual(key, root.key) {
root.Values = append(root.Values, value)
return root, root
}
var new *TreeNode
if root.issmaller(key, root.key) {
root.left, new = root.left.insert(key, value, issmaller)
if root.left.getHeight()-root.right.getHeight() == 2 {
if root.issmaller(key, root.left.key) {
root = root.rightRotate()
} else {
root = root.leftRightRotate()
}
}
} else {
root.right, new = root.right.insert(key, value, issmaller)
if root.right.getHeight()-root.left.getHeight() == 2 {
if (!root.issmaller(key, root.right.key)) && !isEqual(key, root.right.key) {
root = root.leftRotate()
} else {
root = root.rightLeftRotate()
}
}
}
root.height = max(root.left.getHeight(), root.right.getHeight()) + 1
return root, new
}
// Exists checks if a node with key `key` exists in tree `t`
func (t *Tree) Exists(key interface{}) bool {
if t == nil {
return false
}
return t.root.exists(key)
}
// exists recursively searches through tree with root `root` for element with
// key `key`
func (root *TreeNode) exists(key interface{}) bool {
if root == nil {
return false
}
if isEqual(key, root.key) {
return true
}
if root.issmaller(key, root.key) {
if root.left == nil {
return false
}
return root.left.exists(key)
}
if root.right == nil {
return false
}
return root.right.exists(key)
}
// Intersection finds common elements in trees `t` and `x` and returns them in a new tree
func (t *Tree) Intersection(x *Tree) (res *Tree) {
if t == nil || x == nil {
return nil
}
res = New()
t.lock.RLock()
x.lock.RLock()
defer t.lock.RUnlock()
defer x.lock.RUnlock()
n := 0
newRoot := t.root.intersection(x.root, res.root, &n)
return &Tree{
root: newRoot,
Count: n,
}
}
// Intersection builds a tree of common elements of all trees in `candidates`
func Intersection(candidates []*Tree) (res *Tree) {
n := len(candidates)
if n == 0 {
return nil
}
if n == 1 {
return candidates[0]
}
chA := make([]chan *Tree, n/2)
chB := make([]chan *Tree, n/2)
chRet := make([]chan *Tree, n/2)
// Start a go routine that builds intersection of each pair of candidates
for i := 0; i < n/2; i++ {
chA[i] = make(chan *Tree)
chB[i] = make(chan *Tree)
chRet[i] = make(chan *Tree)
go func(chA chan *Tree, chB chan *Tree, chRes chan *Tree) {
a := <-chA
b := <-chB
if a == nil || b == nil {
chRes <- nil
return
}
glog.Infof("finding common elements in %d and %d elements", a.Count, b.Count)
chRes <- a.Intersection(b)
}(chA[i], chB[i], chRet[i])
chA[i] <- candidates[i*2]
chB[i] <- candidates[i*2+1]
}
results := make([]*Tree, 0)
// If amount of candidate trees is uneven we have to add last tree to results
if n%2 == 1 {
results = append(results, candidates[n-1])
}
// Fetch results
for i := 0; i < n/2; i++ {
results = append(results, <-chRet[i])
}
// If we only have one tree left over, we're done
if len(results) != 1 {
return Intersection(results)
}
return results[0]
}
// intersection recursively finds common elements in tree with roots `root` and `b`
// and returns the result in a new tree
func (root *TreeNode) intersection(b *TreeNode, res *TreeNode, n *int) *TreeNode {
if root == nil || b == nil {
return res
}
if root.left != nil {
res = root.left.intersection(b, res, n)
}
if root.right != nil {
res = root.right.intersection(b, res, n)
}
if b.exists(root.key) {
res, _ = res.insert(root.key, root.key, root.issmaller)
*n++
}
return res
}
// Each can be used to traverse tree `t` and call function f with params vals...
// for each node in the tree
func (t *Tree) Each(f EachFunc, vals ...interface{}) {
if t == nil {
return
}
t.lock.RLock()
defer t.lock.RUnlock()
t.root.Each(f, vals...)
}
// Each recursively traverses tree `tree` and calls functions f with params vals...
// for each node in the tree
func (root *TreeNode) Each(f EachFunc, vals ...interface{}) {
if root == nil {
return
}
f(root, vals...)
if root.left != nil {
root.left.Each(f, vals...)
}
if root.right != nil {
root.right.Each(f, vals...)
}
}
// Dump dumps tree `t` into a slice and returns it
func (t *Tree) Dump() (res []interface{}) {
if t == nil {
return
}
t.lock.RLock()
defer t.lock.RUnlock()
return t.root.dump()
}
// dump recursively dumps all nodes of tree with root `t` into a slice and returns it
func (root *TreeNode) dump() (res []interface{}) {
if root == nil {
return res
}
if root.left != nil {
tmp := root.left.dump()
res = append(res, tmp...)
}
res = append(res, root.Values...)
if root.right != nil {
tmp := root.right.dump()
res = append(res, tmp...)
}
return res
}
// TopN finds the the `n` biggest elements in tree `t` and returns them in a slice
func (t *Tree) TopN(n int) (res []interface{}) {
if t == nil {
return
}
t.lock.RLock()
defer t.lock.RUnlock()
return t.root.topN(n)
}
// topN recursively traverses tree with root `t` to find biggest `n` elements.
// Top elements are returned as a slice
func (root *TreeNode) topN(n int) (res []interface{}) {
if root == nil {
return res
}
if root.right != nil {
tmp := root.right.topN(n)
for _, k := range tmp {
if len(res) == n {
return res
}
res = append(res, k)
}
}
if len(res) < n {
res = append(res, root.Values...)
}
if len(res) == n {
return res
}
if root.left != nil {
tmp := root.left.topN(n - len(res))
for _, k := range tmp {
if len(res) == n {
return res
}
res = append(res, k)
}
}
return res
}