-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.go
363 lines (320 loc) · 9.02 KB
/
tree.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
package wbtree
// weight-balanced binary search tree
// see: https://yoichihirai.com/bst.pdf
const (
// these params tune rebalance behavior, values found valid and performant in Y. Hirai (2011).
treeDelta = 3
treeGamma = 2
)
// Cmpable are types that can do C style comparisons with a value of the parameter type.
// This is almost always the same type.
// For example, *big.Rat and *big.Int implement this interface.
type Cmpable[T any] interface {
// Cmp compares this value with other and returns:
// -1 if this value < other
// 0 if this value == other
// +1 if this value > other
Cmp(other T) int
}
// Tree is a map implemented with a weight-balanced tree.
type Tree[K Cmpable[K], V any] struct {
left *Tree[K, V] // lesser
right *Tree[K, V] // greater
childSize uint64 // count of children, 0 for leaf nodes, equals size-1 and weight-2
key K
value V
}
// RootKey returns the key of the root node of this tree.
func (t *Tree[K, V]) RootKey() (key K) {
if t != nil {
key = t.key
}
return key
}
// RootValue returns the stored value in the root node of this tree.
func (t *Tree[K, V]) RootValue() (value V) {
if t != nil {
value = t.value
}
return value
}
// Size returns the total count of nodes in this tree, including the root node.
func (t *Tree[K, V]) Size() uint64 {
if t == nil {
return 0
}
return t.childSize + 1
}
// Keys returns a slice of all keys in dfs order (ascending keys)
func (t *Tree[K, V]) Keys() []K {
return t.LeastKeys(int(t.Size()))
}
// Values returns a slice of all values in dfs order (ascending keys)
func (t *Tree[K, V]) Values() []V {
return t.LeastValues(int(t.Size()))
}
// Least returns a slice with length <= n holding the nodes this tree, in dfs order (ascending keys)
func (t *Tree[K, V]) Least(n int) []*Tree[K, V] {
return top[K, V, *Tree[K, V]](t, n, false, identity[*Tree[K, V]])
}
// LeastKeys returns a slice with length <= n holding the keys this tree, in dfs order (ascending keys)
func (t *Tree[K, V]) LeastKeys(n int) []K {
return top[K, V, K](t, n, false, getKeyFunc[K, V])
}
// LeastValues returns a slice with length <= n holding the values this tree, in dfs order (ascending keys)
func (t *Tree[K, V]) LeastValues(n int) []V {
return top[K, V, V](t, n, false, getValFunc[K, V])
}
// Greatest returns a slice with length <= n holding the nodes this tree, in reverse dfs order (descending keys)
func (t *Tree[K, V]) Greatest(n int) []*Tree[K, V] {
return top[K, V, *Tree[K, V]](t, n, true, identity[*Tree[K, V]])
}
// GreatestKeys returns a slice with length <= n holding the keys this tree, in reverse dfs order (descending keys)
func (t *Tree[K, V]) GreatestKeys(n int) []K {
return top[K, V, K](t, n, true, getKeyFunc[K, V])
}
// GreatestValues returns a slice with length <= n holding the values this tree, in reverse dfs order (descending keys)
func (t *Tree[K, V]) GreatestValues(n int) []V {
return top[K, V, V](t, n, true, getValFunc[K, V])
}
// GreatestNode returns the rightmost Tree node
func (t *Tree[K, V]) GreatestNode() *Tree[K, V] {
if top := t.Greatest(1); len(top) > 0 {
return top[0]
}
return nil
}
// LeastNode returns the leftmost Tree node
func (t *Tree[K, V]) LeastNode() *Tree[K, V] {
if bottom := t.Least(1); len(bottom) > 0 {
return bottom[0]
}
return nil
}
func top[K Cmpable[K], V any, R any](t *Tree[K, V], n int, reversed bool, f func(*Tree[K, V]) R) []R {
if t == nil {
return nil
}
max := n
if ts := t.Size(); ts < uint64(n) {
max = int(ts)
}
result := make([]R, 0, max)
t.forEachNode(func(node *Tree[K, V]) bool {
result = append(result, f(node))
return len(result) < n
}, reversed)
return result
}
// Get returns the value in this tree associated with key, or the zero value of V if key is not present
func (t *Tree[K, V]) Get(key K) V {
return t.GetNode(key).RootValue()
}
// GetNode returns the Tree node at key, or nil if key is not present
func (t *Tree[K, V]) GetNode(key K) *Tree[K, V] {
if t == nil {
return nil
}
compared := t.key.Cmp(key)
if compared == 0 {
return t
}
var next *Tree[K, V]
if compared < 0 {
next = t.right
} else {
next = t.left
}
if next == nil {
return nil
}
return next.GetNode(key)
}
// Insert adds a new value to this Tree, or updates an existing value.
// Returns the new root (the same node if not rebalanced).
// The returned bool is true if the Insert resulted in a new entry in the tree, false if an existing value was replaced.
func (t *Tree[K, V]) Insert(key K, val V) (*Tree[K, V], bool) {
// if tree is empty, return new root
if t == nil {
return &Tree[K, V]{
key: key,
value: val,
}, true
}
// if key is equal to root, replace root value
compared := t.key.Cmp(key)
if compared == 0 {
t.value = val
return t, false
}
// add to right child if root key less than inserted key
addRight := compared < 0
var next *Tree[K, V]
if addRight {
next = t.right
} else {
next = t.left
}
// if no child, add new leaf node
if next == nil {
next = &Tree[K, V]{
key: key,
value: val,
}
} else {
// attempt to add to child tree
var added bool
next, added = next.Insert(key, val)
if !added { // replaced in child tree
return t, false
}
}
// update child pointers and size
if addRight {
t.right = next
} else {
t.left = next
}
t.childSize++
// fix balance and return
return t.balance(addRight), true
}
// Remove removes a node from a tree with the given key, if any exists.
// Returns the new root node, and a bool that is true if a node was removed.
func (t *Tree[K, V]) Remove(key K) (*Tree[K, V], bool) {
if t == nil {
// cannot remove from the empty tree
return t, false
}
compared := t.key.Cmp(key)
if compared == 0 {
// remove this node
if t.left == nil && t.right == nil {
// leaf node, return the empty tree
return nil, true
}
if t.left != nil && t.right != nil {
// two children, swap values with least on right
rightMin := t.right.LeastNode()
t.key = rightMin.key
t.value = rightMin.value
t.right, _ = t.right.Remove(rightMin.key)
t.childSize--
result := t.balance(false)
return result, true
}
if t.left == nil {
return t.right, true
}
return t.left, true
}
// not equal to t, determine next node to remove from
removeRight := compared < 0
var next *Tree[K, V]
if removeRight {
next = t.right
} else {
next = t.left
}
if newNext, removed := next.Remove(key); removed {
if removeRight {
t.right = newNext
} else {
t.left = newNext
}
t.childSize--
result := t.balance(!removeRight)
return result, true
}
// not present
return t, false
}
// ForEach will call f for each node in this tree, in dfs order. If f returns false, interation stops.
func (t *Tree[K, V]) ForEach(f func(K, V) bool) {
if t == nil {
return
}
t.forEach(f, false)
}
// ReverseForEach will call f for each node in this tree, in reverse dfs order. If f returns false, interation stops.
func (t *Tree[K, V]) ReverseForEach(f func(K, V) bool) {
if t == nil {
return
}
t.forEach(f, true)
}
func (t *Tree[K, V]) forEach(f func(K, V) bool, reversed bool) {
t.forEachNode(func(node *Tree[K, V]) bool {
return f(node.RootKey(), node.RootValue())
}, reversed)
}
func (t *Tree[K, V]) forEachNode(f func(*Tree[K, V]) bool, reversed bool) bool {
prev := t.left
next := t.right
if reversed {
prev, next = next, prev
}
if prev != nil && !prev.forEachNode(f, reversed) {
return false
}
if !f(t) {
return false
}
return next == nil || next.forEachNode(f, reversed)
}
// balance checks if, after Insert or Remove, the tree has become unbalanced.
// If it has, balance rotates (or double rotates) to fix it, updating childSize as needed.
// rightHeavy should be true if a new node was inserted into right (or removed from left), else false.
// Returns new root (same root if no rebalance is needed).
func (t *Tree[K, V]) balance(rightHeavy bool) *Tree[K, V] {
x, c := t.left, t.right
if !rightHeavy {
x, c = c, x
}
var b, z *Tree[K, V]
if c != nil {
b, z = c.left, c.right
if !rightHeavy {
b, z = z, b
}
}
xSize, cSize := x.Size(), c.Size()
if (xSize+1)*treeDelta >= cSize+1 {
// balanced, no rotation
return t
}
// balance broken, check for single or double rotation
bSize, zSize := b.Size(), z.Size()
// single rotation
if bSize+1 < (zSize+1)*treeGamma {
t.left, t.right = x, b
c.left, c.right = t, z
if !rightHeavy {
t.left, t.right = t.right, t.left
c.left, c.right = c.right, c.left
}
t.childSize = xSize + bSize
c.childSize = t.Size() + zSize
return c
}
// double rotation
s, y := b.left, b.right
if !rightHeavy {
s, y = y, s
}
t.left, t.right = x, s
b.left, b.right = t, c
c.left, c.right = y, z
if !rightHeavy {
t.left, t.right = t.right, t.left
b.left, b.right = b.right, b.left
c.left, c.right = c.right, c.left
}
t.childSize = xSize + s.Size()
c.childSize = y.Size() + zSize
b.childSize = t.Size() + c.Size()
return b
}
func identity[T any](x T) T { return x }
func getKeyFunc[K Cmpable[K], V any](t *Tree[K, V]) K { return t.RootKey() }
func getValFunc[K Cmpable[K], V any](t *Tree[K, V]) V { return t.RootValue() }