1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | const rotate = (tree, parent, newParent, n) => {
|
9 | if (parent === null) {
|
10 | tree.root = newParent
|
11 | newParent._parent = null
|
12 | } else if (parent.left === n) {
|
13 | parent.left = newParent
|
14 | } else if (parent.right === n) {
|
15 | parent.right = newParent
|
16 | } else {
|
17 | throw new Error('The elements are wrongly connected!')
|
18 | }
|
19 | }
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | class N {
|
25 | |
26 |
|
27 |
|
28 |
|
29 |
|
30 | constructor (val) {
|
31 | this.val = val
|
32 | this.color = true
|
33 | this._left = null
|
34 | this._right = null
|
35 | this._parent = null
|
36 | }
|
37 |
|
38 | isRed () { return this.color }
|
39 | isBlack () { return !this.color }
|
40 | redden () { this.color = true; return this }
|
41 | blacken () { this.color = false; return this }
|
42 | get grandparent () {
|
43 | return this.parent.parent
|
44 | }
|
45 |
|
46 | get parent () {
|
47 | return this._parent
|
48 | }
|
49 |
|
50 | get sibling () {
|
51 | return (this === this.parent.left)
|
52 | ? this.parent.right : this.parent.left
|
53 | }
|
54 |
|
55 | get left () {
|
56 | return this._left
|
57 | }
|
58 |
|
59 | get right () {
|
60 | return this._right
|
61 | }
|
62 |
|
63 | set left (n) {
|
64 | if (n !== null) {
|
65 | n._parent = this
|
66 | }
|
67 | this._left = n
|
68 | }
|
69 |
|
70 | set right (n) {
|
71 | if (n !== null) {
|
72 | n._parent = this
|
73 | }
|
74 | this._right = n
|
75 | }
|
76 |
|
77 | rotateLeft (tree) {
|
78 | const parent = this.parent
|
79 | const newParent = this.right
|
80 | const newRight = this.right.left
|
81 | newParent.left = this
|
82 | this.right = newRight
|
83 | rotate(tree, parent, newParent, this)
|
84 | }
|
85 |
|
86 | next () {
|
87 | if (this.right !== null) {
|
88 |
|
89 | var o = this.right
|
90 | while (o.left !== null) {
|
91 | o = o.left
|
92 | }
|
93 | return o
|
94 | } else {
|
95 | var p = this
|
96 | while (p.parent !== null && p !== p.parent.left) {
|
97 | p = p.parent
|
98 | }
|
99 | return p.parent
|
100 | }
|
101 | }
|
102 |
|
103 | prev () {
|
104 | if (this.left !== null) {
|
105 |
|
106 | var o = this.left
|
107 | while (o.right !== null) {
|
108 | o = o.right
|
109 | }
|
110 | return o
|
111 | } else {
|
112 | var p = this
|
113 | while (p.parent !== null && p !== p.parent.right) {
|
114 | p = p.parent
|
115 | }
|
116 | return p.parent
|
117 | }
|
118 | }
|
119 |
|
120 | rotateRight (tree) {
|
121 | const parent = this.parent
|
122 | const newParent = this.left
|
123 | const newLeft = this.left.right
|
124 | newParent.right = this
|
125 | this.left = newLeft
|
126 | rotate(tree, parent, newParent, this)
|
127 | }
|
128 |
|
129 | getUncle () {
|
130 |
|
131 | if (this.parent === this.parent.parent.left) {
|
132 | return this.parent.parent.right
|
133 | } else {
|
134 | return this.parent.parent.left
|
135 | }
|
136 | }
|
137 | }
|
138 |
|
139 | const isBlack = node =>
|
140 | node !== null ? node.isBlack() : true
|
141 |
|
142 | const isRed = (node) =>
|
143 | node !== null ? node.isRed() : false
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 | export class Tree {
|
151 | constructor () {
|
152 | this.root = null
|
153 | this.length = 0
|
154 | }
|
155 |
|
156 | |
157 |
|
158 |
|
159 | findNext (id) {
|
160 | var nextID = id.clone()
|
161 | nextID.clock += 1
|
162 | return this.findWithLowerBound(nextID)
|
163 | }
|
164 |
|
165 | |
166 |
|
167 |
|
168 | findPrev (id) {
|
169 | const prevID = id.clone()
|
170 | prevID.clock -= 1
|
171 | return this.findWithUpperBound(prevID)
|
172 | }
|
173 |
|
174 | |
175 |
|
176 |
|
177 | findNodeWithLowerBound (from) {
|
178 | var o = this.root
|
179 | if (o === null) {
|
180 | return null
|
181 | } else {
|
182 | while (true) {
|
183 | if (from === null || (from.lessThan(o.val._id) && o.left !== null)) {
|
184 |
|
185 |
|
186 | o = o.left
|
187 | } else if (from !== null && o.val._id.lessThan(from)) {
|
188 |
|
189 | if (o.right !== null) {
|
190 | o = o.right
|
191 | } else {
|
192 |
|
193 |
|
194 | return o.next()
|
195 | }
|
196 | } else {
|
197 | return o
|
198 | }
|
199 | }
|
200 | }
|
201 | }
|
202 |
|
203 | |
204 |
|
205 |
|
206 | findNodeWithUpperBound (to) {
|
207 | if (to === undefined) {
|
208 | throw new Error('You must define from!')
|
209 | }
|
210 | var o = this.root
|
211 | if (o === null) {
|
212 | return null
|
213 | } else {
|
214 | while (true) {
|
215 | if ((to === null || o.val._id.lessThan(to)) && o.right !== null) {
|
216 |
|
217 |
|
218 | o = o.right
|
219 | } else if (to !== null && to.lessThan(o.val._id)) {
|
220 |
|
221 | if (o.left !== null) {
|
222 | o = o.left
|
223 | } else {
|
224 |
|
225 |
|
226 | return o.prev()
|
227 | }
|
228 | } else {
|
229 | return o
|
230 | }
|
231 | }
|
232 | }
|
233 | }
|
234 |
|
235 | |
236 |
|
237 |
|
238 | findSmallestNode () {
|
239 | var o = this.root
|
240 | while (o != null && o.left != null) {
|
241 | o = o.left
|
242 | }
|
243 | return o
|
244 | }
|
245 |
|
246 | |
247 |
|
248 |
|
249 |
|
250 | findWithLowerBound (from) {
|
251 | var n = this.findNodeWithLowerBound(from)
|
252 | return n == null ? null : n.val
|
253 | }
|
254 |
|
255 | |
256 |
|
257 |
|
258 |
|
259 | findWithUpperBound (to) {
|
260 | var n = this.findNodeWithUpperBound(to)
|
261 | return n == null ? null : n.val
|
262 | }
|
263 |
|
264 | |
265 |
|
266 |
|
267 |
|
268 |
|
269 | iterate (from, to, f) {
|
270 | var o
|
271 | if (from === null) {
|
272 | o = this.findSmallestNode()
|
273 | } else {
|
274 | o = this.findNodeWithLowerBound(from)
|
275 | }
|
276 | while (
|
277 | o !== null &&
|
278 | (
|
279 | to === null ||
|
280 | o.val._id.lessThan(to) ||
|
281 | o.val._id.equals(to)
|
282 | )
|
283 | ) {
|
284 | f(o.val)
|
285 | o = o.next()
|
286 | }
|
287 | }
|
288 |
|
289 | |
290 |
|
291 |
|
292 |
|
293 | find (id) {
|
294 | const n = this.findNode(id)
|
295 | if (n !== null) {
|
296 | return n.val
|
297 | } else {
|
298 | return null
|
299 | }
|
300 | }
|
301 |
|
302 | |
303 |
|
304 |
|
305 |
|
306 | findNode (id) {
|
307 | var o = this.root
|
308 | if (o === null) {
|
309 | return null
|
310 | } else {
|
311 | while (true) {
|
312 | if (o === null) {
|
313 | return null
|
314 | }
|
315 | if (id.lessThan(o.val._id)) {
|
316 | o = o.left
|
317 | } else if (o.val._id.lessThan(id)) {
|
318 | o = o.right
|
319 | } else {
|
320 | return o
|
321 | }
|
322 | }
|
323 | }
|
324 | }
|
325 |
|
326 | |
327 |
|
328 |
|
329 | delete (id) {
|
330 | var d = this.findNode(id)
|
331 | if (d == null) {
|
332 |
|
333 | return
|
334 | }
|
335 | this.length--
|
336 | if (d.left !== null && d.right !== null) {
|
337 |
|
338 |
|
339 | var o = d.left
|
340 |
|
341 | while (o.right !== null) {
|
342 | o = o.right
|
343 | }
|
344 |
|
345 | d.val = o.val
|
346 | d = o
|
347 | }
|
348 |
|
349 |
|
350 | var isFakeChild
|
351 | var child = d.left || d.right
|
352 | if (child === null) {
|
353 | isFakeChild = true
|
354 | child = new N(null)
|
355 | child.blacken()
|
356 | d.right = child
|
357 | } else {
|
358 | isFakeChild = false
|
359 | }
|
360 |
|
361 | if (d.parent === null) {
|
362 | if (!isFakeChild) {
|
363 | this.root = child
|
364 | child.blacken()
|
365 | child._parent = null
|
366 | } else {
|
367 | this.root = null
|
368 | }
|
369 | return
|
370 | } else if (d.parent.left === d) {
|
371 | d.parent.left = child
|
372 | } else if (d.parent.right === d) {
|
373 | d.parent.right = child
|
374 | } else {
|
375 | throw new Error('Impossible!')
|
376 | }
|
377 | if (d.isBlack()) {
|
378 | if (child.isRed()) {
|
379 | child.blacken()
|
380 | } else {
|
381 | this._fixDelete(child)
|
382 | }
|
383 | }
|
384 | this.root.blacken()
|
385 | if (isFakeChild) {
|
386 | if (child.parent.left === child) {
|
387 | child.parent.left = null
|
388 | } else if (child.parent.right === child) {
|
389 | child.parent.right = null
|
390 | } else {
|
391 | throw new Error('Impossible #3')
|
392 | }
|
393 | }
|
394 | }
|
395 |
|
396 | _fixDelete (n) {
|
397 | if (n.parent === null) {
|
398 |
|
399 | return
|
400 | }
|
401 |
|
402 |
|
403 |
|
404 | var sibling = n.sibling
|
405 | if (isRed(sibling)) {
|
406 |
|
407 | n.parent.redden()
|
408 | sibling.blacken()
|
409 | if (n === n.parent.left) {
|
410 | n.parent.rotateLeft(this)
|
411 | } else if (n === n.parent.right) {
|
412 | n.parent.rotateRight(this)
|
413 | } else {
|
414 | throw new Error('Impossible #2')
|
415 | }
|
416 | sibling = n.sibling
|
417 | }
|
418 |
|
419 | if (n.parent.isBlack() &&
|
420 | sibling.isBlack() &&
|
421 | isBlack(sibling.left) &&
|
422 | isBlack(sibling.right)
|
423 | ) {
|
424 | sibling.redden()
|
425 | this._fixDelete(n.parent)
|
426 | } else if (n.parent.isRed() &&
|
427 | sibling.isBlack() &&
|
428 | isBlack(sibling.left) &&
|
429 | isBlack(sibling.right)
|
430 | ) {
|
431 | sibling.redden()
|
432 | n.parent.blacken()
|
433 | } else {
|
434 | if (n === n.parent.left &&
|
435 | sibling.isBlack() &&
|
436 | isRed(sibling.left) &&
|
437 | isBlack(sibling.right)
|
438 | ) {
|
439 | sibling.redden()
|
440 | sibling.left.blacken()
|
441 | sibling.rotateRight(this)
|
442 | sibling = n.sibling
|
443 | } else if (n === n.parent.right &&
|
444 | sibling.isBlack() &&
|
445 | isRed(sibling.right) &&
|
446 | isBlack(sibling.left)
|
447 | ) {
|
448 | sibling.redden()
|
449 | sibling.right.blacken()
|
450 | sibling.rotateLeft(this)
|
451 | sibling = n.sibling
|
452 | }
|
453 | sibling.color = n.parent.color
|
454 | n.parent.blacken()
|
455 | if (n === n.parent.left) {
|
456 | sibling.right.blacken()
|
457 | n.parent.rotateLeft(this)
|
458 | } else {
|
459 | sibling.left.blacken()
|
460 | n.parent.rotateRight(this)
|
461 | }
|
462 | }
|
463 | }
|
464 |
|
465 | put (v) {
|
466 | var node = new N(v)
|
467 | if (this.root !== null) {
|
468 | var p = this.root
|
469 | while (true) {
|
470 | if (node.val._id.lessThan(p.val._id)) {
|
471 | if (p.left === null) {
|
472 | p.left = node
|
473 | break
|
474 | } else {
|
475 | p = p.left
|
476 | }
|
477 | } else if (p.val._id.lessThan(node.val._id)) {
|
478 | if (p.right === null) {
|
479 | p.right = node
|
480 | break
|
481 | } else {
|
482 | p = p.right
|
483 | }
|
484 | } else {
|
485 | p.val = node.val
|
486 | return p
|
487 | }
|
488 | }
|
489 | this._fixInsert(node)
|
490 | } else {
|
491 | this.root = node
|
492 | }
|
493 | this.length++
|
494 | this.root.blacken()
|
495 | return node
|
496 | }
|
497 |
|
498 | _fixInsert (n) {
|
499 | if (n.parent === null) {
|
500 | n.blacken()
|
501 | return
|
502 | } else if (n.parent.isBlack()) {
|
503 | return
|
504 | }
|
505 | var uncle = n.getUncle()
|
506 | if (uncle !== null && uncle.isRed()) {
|
507 |
|
508 | n.parent.blacken()
|
509 | uncle.blacken()
|
510 | n.grandparent.redden()
|
511 | this._fixInsert(n.grandparent)
|
512 | } else {
|
513 |
|
514 |
|
515 |
|
516 |
|
517 |
|
518 |
|
519 |
|
520 | if (n === n.parent.right && n.parent === n.grandparent.left) {
|
521 | n.parent.rotateLeft(this)
|
522 |
|
523 |
|
524 |
|
525 | n = n.left
|
526 | } else if (n === n.parent.left && n.parent === n.grandparent.right) {
|
527 | n.parent.rotateRight(this)
|
528 |
|
529 | n = n.right
|
530 | }
|
531 |
|
532 |
|
533 |
|
534 | n.parent.blacken()
|
535 | n.grandparent.redden()
|
536 | if (n === n.parent.left) {
|
537 |
|
538 | n.grandparent.rotateRight(this)
|
539 | } else {
|
540 |
|
541 | n.grandparent.rotateLeft(this)
|
542 | }
|
543 | }
|
544 | }
|
545 | }
|