-
Notifications
You must be signed in to change notification settings - Fork 1
/
node.go
312 lines (296 loc) · 5.45 KB
/
node.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
package btree
// node[T] is a generic type that represents a node in the AVL tree.
type node[T any] struct {
p *node[T] // parent
l *node[T] // left child
r *node[T] // right child
h uint // height of the node.
i T // The item the node is holding.
}
// balance calculates the relative balance of a node.
// Negative numbers indicate a subtree that is left-heavy,
// and positive numbers indicate a tree that is right-heavy.
func (n *node[T]) balance() (res int) {
if n.l != nil {
res -= int(n.l.h)
}
if n.r != nil {
res += int(n.r.h)
}
return
}
// setHeight calculates the height of this node.
func (n *node[T]) setHeight() {
n.h = 0
if n.l != nil {
n.h = n.l.h
}
if n.r != nil && n.r.h >= n.h {
n.h = n.r.h
}
n.h++
return
}
func (t *Tree[T]) newNode(v T) *node[T] {
res := t.nodePool.Get().(*node[T])
res.i = v
res.h = 1
t.count++
t.insertCount++
return res
}
func (t *Tree[T]) putNode(n *node[T]) {
n.l = nil
n.r = nil
n.p = nil
var ref T
n.i = ref
n.h = 0
t.count--
t.removeCount++
t.nodePool.Put(n)
}
func (t *Tree[T]) copyNodes(n *node[T], into *Tree[T]) *node[T] {
if n == nil {
return nil
}
res := into.newNode(n.i)
res.h = n.h
if res.l = t.copyNodes(n.l, into); res.l != nil {
res.l.p = res
}
if res.r = t.copyNodes(n.r, into); res.r != nil {
res.r.p = res
}
return res
}
func (t *Tree[T]) releaseNodes(n *node[T]) {
var s *node[T]
for n != nil {
if n.l != nil {
n = n.l
continue
}
if n.r != nil {
n = n.r
continue
}
if n.p != nil {
n.p.swapChild(n, s)
}
s = n.p
t.putNode(n)
n = s
s = nil
}
}
func (n *node[T]) swapChild(was, is *node[T]) {
if n.r == was {
n.r = is
} else {
n.l = is
}
if is != nil {
is.p = n
}
}
// rotateLeft transforms
//
// |
// a
// / \
// x b
// / \
// y z
//
// to
// |
// b
// / \
// a z
// / \
// x y
func (a *node[T]) rotateLeft() (b *node[T]) {
b = a.r
if a.p != nil {
a.p.swapChild(a, b)
} else {
b.p = nil
}
a.p = b
if a.r = b.l; a.r != nil {
a.r.p = a
}
b.l = a
return
}
// rotateRight is the inverse of rotateLeft. it transforms
//
// |
// a(h)
// / \
// b z
// / \
// x y
//
// to
//
// |
// b
// / \
// x a
// / \
// y z
func (a *node[T]) rotateRight() (b *node[T]) {
b = a.l
if a.p != nil {
a.p.swapChild(a, b)
} else {
b.p = nil
}
a.p = b
if a.l = b.r; a.l != nil {
a.l.p = a
}
b.r = a
return
}
func (t *Tree[T]) getExact(n *node[T], v T) (res *node[T], dir int) {
for n != nil {
if t.less(v, n.i) {
if n.l == nil {
return n, Less
}
n = n.l
} else if t.less(n.i, v) {
if n.r == nil {
return n, Greater
}
n = n.r
} else {
break
}
}
return n, Equal
}
// min finds the minimal child of h
func min[T any](n *node[T]) *node[T] {
for n.l != nil {
n = n.l
}
return n
}
// max finds the maximal child of h
func max[T any](n *node[T]) *node[T] {
for n.r != nil {
n = n.r
}
return n
}
// rebalanceAt walks up the tree starting at node n, rebalancing nodes
// that no longer meet the AVL balance criteria. rebalanceAt will continue until
// it either walks all the way up the tree, or the node has the
// same height it started with.
func (t *Tree[T]) rebalanceAt(n *node[T], forInsert bool) {
for {
oh := n.h
switch n.balance() {
case Less, Equal, Greater:
case 2:
// Tree is excessively right-heavy, rotate it to the left.
if n.r != nil && n.r.balance() < 0 {
// Right tree is left-heavy, which would cause the next rotation to result in overall left-heaviness.
// Rotate the right tree to the right to counteract this.
n.r = n.r.rotateRight()
n.r.r.setHeight()
}
n = n.rotateLeft()
n.l.setHeight()
if forInsert {
t.insertRebalanceCount++
} else {
t.removeRebalanceCount++
}
case -2:
// Tree is excessively left-heavy, rotate it to the right
if n.l != nil && n.l.balance() > 0 {
// The left tree is right-heavy, which would cause the next rotation to result in overall right-heaviness.
// Rotate the left tree to the left to compensate.
n.l = n.l.rotateLeft()
n.l.l.setHeight()
}
n = n.rotateRight()
n.r.setHeight()
if forInsert {
t.insertRebalanceCount++
} else {
t.removeRebalanceCount++
}
default:
panic("Tree too far out of shape!")
}
n.setHeight()
if n.p == nil {
t.root = n
return
}
if oh == n.h {
return
}
n = n.p
}
}
// insert or replace a new value. If a new value is inserted, any needed rebalancing
// is performed.
func (t *Tree[T]) insert(v T) {
n, direction := t.getExact(t.root, v)
var needRebalance bool
switch direction {
case Equal:
n.i = v
return
case Less:
n.l = t.newNode(v)
n.l.p = n
needRebalance = n.r == nil
case Greater:
n.r = t.newNode(v)
n.r.p = n
needRebalance = n.l == nil
}
if needRebalance {
n.h++
if n.p != nil {
t.rebalanceAt(n.p, true)
}
}
}
// remove the passed-in value from the tree, if it exists. The tree will be rebalanced if needed.
func (t *Tree[T]) remove(v T) (deleted T, found bool) {
at, direction := t.getExact(t.root, v)
if found = direction == Equal; !found {
return
}
deleted = at.i
var alt *node[T]
for {
if at.h == 1 {
if alt = at.p; alt != nil {
alt.swapChild(at, at.r)
t.rebalanceAt(alt, false)
} else {
t.root = nil
}
t.putNode(at)
return
} else if at.r != nil {
alt = min(at.r)
} else if at.l != nil {
alt = max(at.l)
} else {
panic("Impossible")
}
at.i, alt.i = alt.i, at.i
at = alt
}
}