-
Notifications
You must be signed in to change notification settings - Fork 17
/
node.go
409 lines (331 loc) · 8.22 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
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
package strtree8
// !!!DON'T EDIT!!! Generated by infobloxopen/go-trees/etc from <name>tree{{.bits}} with etc -s uint8 -d uintX.yaml -t ./<name>tree\{\{.bits\}\}
import "fmt"
const (
dirLeft = iota
dirRight
)
type node struct {
key string
value uint8
chld [2]*node
red bool
}
func (n *node) dot() string {
body := ""
// Iterate all nodes using breadth-first search algorithm.
i := 0
queue := []*node{n}
for len(queue) > 0 {
n := queue[0]
body += fmt.Sprintf("N%d %s\n", i, n.dotString())
if n != nil && (n.chld[0] != nil || n.chld[1] != nil) {
// Children for current node if any always go to the end of the queue
// so we can know their indices using current queue length.
body += fmt.Sprintf("N%d -> { N%d N%d }\n", i, i+len(queue), i+len(queue)+1)
queue = append(append(queue, n.chld[0]), n.chld[1])
}
queue = queue[1:]
i++
}
return body
}
func (n *node) dotString() string {
if n == nil {
return "[label=\"nil\" style=filled fontcolor=white fillcolor=black]"
}
k := fmt.Sprintf("%q", n.key)
k = fmt.Sprintf("\"k: \\\"%s\\\" v: \\\"%d\\\"\"", k[1:len(k)-1], n.value)
color := "fontcolor=white fillcolor=black"
if n.red {
color = "fillcolor=red"
}
return fmt.Sprintf("[label=%s style=filled %s]", k, color)
}
func (n *node) insert(key string, value uint8, compare Compare) *node {
if n == nil {
return &node{key: key, value: value}
}
// Using fake root to get rid of corner cases with rotation right under the root.
root := &node{chld: [2]*node{nil, n}}
dir := dirLeft
// Nodes down the path to current node. All these nodes are copies of nodes from tree.
var (
// Grandparent's parent.
gp *node
// Grandparent.
g *node
// Parent.
p *node
// Childern.
c [2]*node
)
// Start with fake root.
n = root
// As real root is right child of fake root - go to the right from start.
r := -1
// Continue until keys are equal.
for r != 0 {
parentDir := dir
dir = dirLeft
if r < 0 {
// Go to the right if current node is less then given key.
dir = dirRight
}
// Propagate set of nodes.
gp = g
g = p
p = n
n = n.chld[dir]
if n == nil {
// If no child in the direction we go insert new red node.
n = &node{
key: key,
red: true}
c = [2]*node{nil, nil}
} else {
// Make copy of current node or just use copy of child node if it has been made during color flip.
if n != c[dir] {
n = n.fullCopy()
}
// Color flip case to maintain invariant that the current node is black and has at least one black child.
if n.chld[dirLeft] != nil && n.chld[dirRight] != nil && n.chld[dirLeft].red && n.chld[dirRight].red {
n.red = true
c = [2]*node{
n.chld[dirLeft].colorCopy(false),
n.chld[dirRight].colorCopy(false)}
n.chld = c
} else {
c = [2]*node{nil, nil}
}
}
p.chld[dir] = n
// Fix red violation.
if n.red && p != nil && p.red {
// As root is black we can't be here earlier than fake root becomes parent of grandparent.
grandParentDir := dirLeft
if gp.chld[dirRight] == g {
grandParentDir = dirRight
}
if n == p.chld[parentDir] {
// With single rotation if current node goes in the same direction from
// parent as parent from grandparent.
gp.chld[grandParentDir] = g.single(parentDir)
// The rotation changes parent and grandparent so during next iteration
// grandparent's parent should remain the same. Here we fix grandparent
// to keep correct gradparent's parent.
g = gp
} else {
// With double rotation if current node goes in the opposite direction.
gp.chld[grandParentDir] = g.double(parentDir)
// The rotation puts grandparent and parent as children of current node.
// The nodes are copied on previous steps so we put them to to children
// array to prevent additional coping at the next step. Also in the next
// step grandparent's parent and grandparent iteslf make step back. So we
// fix parent to keep correct grandparent but there is no information on
// parent of grandparent's parent to keep corrent grandparent's parent.
// Luckily after the rotation current node (next parent) becomes black so
// we can't make red violation on next iteration.
c = n.chld
p = gp
}
}
r = compare(n.key, key)
}
n.value = value
n = root.chld[dirRight]
n.red = false
return n
}
func (n *node) inplaceInsert(key string, value uint8, compare Compare) *node {
if n == nil {
return &node{key: key, value: value}
}
root := &node{chld: [2]*node{nil, n}}
dir := dirLeft
var (
gp *node
g *node
p *node
)
n = root
r := -1
for r != 0 {
parentDir := dir
dir = dirLeft
if r < 0 {
dir = dirRight
}
gp = g
g = p
p = n
n = n.chld[dir]
if n == nil {
n = &node{
key: key,
red: true}
p.chld[dir] = n
} else {
if n.chld[dirLeft] != nil && n.chld[dirRight] != nil && n.chld[dirLeft].red && n.chld[dirRight].red {
n.red = true
n.chld[dirLeft].red = false
n.chld[dirRight].red = false
}
}
if n.red && p != nil && p.red {
grandParentDir := dirLeft
if gp.chld[dirRight] == g {
grandParentDir = dirRight
}
if n == p.chld[parentDir] {
gp.chld[grandParentDir] = g.single(parentDir)
g = gp
} else {
gp.chld[grandParentDir] = g.double(parentDir)
p = gp
}
}
r = compare(n.key, key)
}
n.value = value
n = root.chld[dirRight]
n.red = false
return n
}
func (n *node) fullCopy() *node {
return &node{
key: n.key,
value: n.value,
chld: n.chld,
red: n.red}
}
func (n *node) colorCopy(color bool) *node {
return &node{
key: n.key,
value: n.value,
chld: n.chld,
red: color}
}
func (n *node) single(dir int) *node {
nDir := 1 - dir
s := n.chld[dir]
n.chld[dir] = s.chld[nDir]
s.chld[nDir] = n
n.red = true
s.red = false
return s
}
func (n *node) double(dir int) *node {
n.chld[dir] = n.chld[dir].single(1 - dir)
return n.single(dir)
}
func (n *node) get(key string, compare Compare) (uint8, bool) {
for n != nil {
r := compare(n.key, key)
if r == 0 {
return n.value, true
}
dir := dirLeft
if r < 0 {
dir = dirRight
}
n = n.chld[dir]
}
return 0, false
}
func (n *node) enumerate(ch chan Pair) {
if n == nil {
return
}
n.chld[dirLeft].enumerate(ch)
ch <- Pair{Key: n.key, Value: n.value}
n.chld[dirRight].enumerate(ch)
}
func (n *node) del(key string, compare Compare) (*node, bool) {
// Fake root.
root := &node{chld: [2]*node{nil, n}}
// Nodes down the path to current node.
var (
// Grandparent.
g *node
// Parent.
p *node
// Target node.
t *node
)
n = root
// Direction from current node to next child we need to go.
dir := dirRight
for n.chld[dir] != nil {
// Direction from parent to current node.
pDir := dir
g = p
p = n
n.chld[dir] = n.chld[dir].fullCopy()
n = n.chld[dir]
dir = dirLeft
r := compare(n.key, key)
if r < 0 {
dir = dirRight
}
if r == 0 {
t = n
}
if !n.red && (n.chld[dir] == nil || !n.chld[dir].red) {
nDir := 1 - dir
if n.chld[nDir] != nil && n.chld[nDir].red {
n.chld[nDir] = n.chld[nDir].fullCopy()
p.chld[pDir] = n.single(nDir)
p = p.chld[pDir]
} else {
nPDir := 1 - pDir
s := p.chld[nPDir]
if s != nil {
s = s.fullCopy()
p.chld[nPDir] = s
if (s.chld[dirLeft] == nil || !s.chld[dirLeft].red) &&
(s.chld[dirRight] == nil || !s.chld[dirRight].red) {
p.red = false
n.red = true
s.red = true
} else {
// Direction from grandparent to parent.
gpDir := dirLeft
if g.chld[dirRight] == p {
gpDir = dirRight
}
if s.chld[pDir] != nil && s.chld[pDir].red {
s.chld[pDir] = s.chld[pDir].fullCopy()
g.chld[gpDir] = p.double(nPDir)
} else {
s.chld[nPDir] = s.chld[nPDir].fullCopy()
g.chld[gpDir] = p.single(nPDir)
}
n.red = true
g.chld[gpDir].red = true
g.chld[gpDir].chld[dirLeft].red = false
g.chld[gpDir].chld[dirRight].red = false
}
}
}
}
}
if t != nil {
t.key = n.key
t.value = n.value
dir = dirLeft
if p.chld[dirRight] == n {
dir = dirRight
}
chldDir := dirLeft
if n.chld[dirLeft] == nil {
chldDir = dirRight
}
p.chld[dir] = n.chld[chldDir]
}
n = root.chld[dirRight]
if n != nil {
n.red = false
}
return n, t != nil
}