splaytree 3.1.0 → 3.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js DELETED
@@ -1,572 +0,0 @@
1
- import Node from './node';
2
- /* follows "An implementation of top-down splaying"
3
- * by D. Sleator <sleator@cs.cmu.edu> March 1992
4
- */
5
- function DEFAULT_COMPARE(a, b) {
6
- return a > b ? 1 : a < b ? -1 : 0;
7
- }
8
- /**
9
- * Simple top down splay, not requiring i to be in the tree t.
10
- */
11
- function splay(i, t, comparator) {
12
- if (t === null)
13
- return t;
14
- const N = new Node(null, null);
15
- let l = N;
16
- let r = N;
17
- while (true) {
18
- const cmp = comparator(i, t.key);
19
- //if (i < t.key) {
20
- if (cmp < 0) {
21
- if (t.left === null)
22
- break;
23
- //if (i < t.left.key) {
24
- if (comparator(i, t.left.key) < 0) {
25
- const y = t.left; /* rotate right */
26
- t.left = y.right;
27
- y.right = t;
28
- t = y;
29
- if (t.left === null)
30
- break;
31
- }
32
- r.left = t; /* link right */
33
- r = t;
34
- t = t.left;
35
- //} else if (i > t.key) {
36
- }
37
- else if (cmp > 0) {
38
- if (t.right === null)
39
- break;
40
- //if (i > t.right.key) {
41
- if (comparator(i, t.right.key) > 0) {
42
- const y = t.right; /* rotate left */
43
- t.right = y.left;
44
- y.left = t;
45
- t = y;
46
- if (t.right === null)
47
- break;
48
- }
49
- l.right = t; /* link left */
50
- l = t;
51
- t = t.right;
52
- }
53
- else
54
- break;
55
- }
56
- /* assemble */
57
- l.right = t.left;
58
- r.left = t.right;
59
- t.left = N.right;
60
- t.right = N.left;
61
- return t;
62
- }
63
- function insert(i, data, t, comparator) {
64
- const node = new Node(i, data);
65
- if (t === null) {
66
- node.left = node.right = null;
67
- return node;
68
- }
69
- t = splay(i, t, comparator);
70
- const cmp = comparator(i, t.key);
71
- if (cmp < 0) {
72
- node.left = t.left;
73
- node.right = t;
74
- t.left = null;
75
- }
76
- else if (cmp >= 0) {
77
- node.right = t.right;
78
- node.left = t;
79
- t.right = null;
80
- }
81
- return node;
82
- }
83
- function split(key, v, comparator) {
84
- let left;
85
- let right;
86
- if (v === null) {
87
- left = right = null;
88
- }
89
- else {
90
- v = splay(key, v, comparator);
91
- const cmp = comparator(v.key, key);
92
- if (cmp === 0) {
93
- left = v.left;
94
- right = v.right;
95
- }
96
- else if (cmp < 0) {
97
- right = v.right;
98
- v.right = null;
99
- left = v;
100
- }
101
- else {
102
- left = v.left;
103
- v.left = null;
104
- right = v;
105
- }
106
- }
107
- return { left, right };
108
- }
109
- function merge(left, right, comparator) {
110
- if (right === null)
111
- return left;
112
- if (left === null)
113
- return right;
114
- right = splay(left.key, right, comparator);
115
- right.left = left;
116
- return right;
117
- }
118
- /**
119
- * Prints level of the tree
120
- */
121
- function printRow(root, prefix, isTail, out, printNode) {
122
- if (root) {
123
- out(`${prefix}${isTail ? '└── ' : '├── '}${printNode(root)}\n`);
124
- const indent = prefix + (isTail ? ' ' : '│ ');
125
- if (root.left)
126
- printRow(root.left, indent, false, out, printNode);
127
- if (root.right)
128
- printRow(root.right, indent, true, out, printNode);
129
- }
130
- }
131
- export default class Tree {
132
- constructor(comparator = DEFAULT_COMPARE) {
133
- this._comparator = comparator;
134
- this._root = null;
135
- this._size = 0;
136
- }
137
- /**
138
- * Inserts a key, allows duplicates
139
- */
140
- insert(key, data) {
141
- this._size++;
142
- return this._root = insert(key, data, this._root, this._comparator);
143
- }
144
- /**
145
- * Adds a key, if it is not present in the tree
146
- */
147
- add(key, data) {
148
- const node = new Node(key, data);
149
- if (this._root === null) {
150
- node.left = node.right = null;
151
- this._size++;
152
- this._root = node;
153
- }
154
- const comparator = this._comparator;
155
- const t = splay(key, this._root, comparator);
156
- const cmp = comparator(key, t.key);
157
- if (cmp === 0)
158
- this._root = t;
159
- else {
160
- if (cmp < 0) {
161
- node.left = t.left;
162
- node.right = t;
163
- t.left = null;
164
- }
165
- else if (cmp > 0) {
166
- node.right = t.right;
167
- node.left = t;
168
- t.right = null;
169
- }
170
- this._size++;
171
- this._root = node;
172
- }
173
- return this._root;
174
- }
175
- /**
176
- * @param {Key} key
177
- * @return {Node|null}
178
- */
179
- remove(key) {
180
- this._root = this._remove(key, this._root, this._comparator);
181
- }
182
- /**
183
- * Deletes i from the tree if it's there
184
- */
185
- _remove(i, t, comparator) {
186
- let x;
187
- if (t === null)
188
- return null;
189
- t = splay(i, t, comparator);
190
- const cmp = comparator(i, t.key);
191
- if (cmp === 0) { /* found it */
192
- if (t.left === null) {
193
- x = t.right;
194
- }
195
- else {
196
- x = splay(i, t.left, comparator);
197
- x.right = t.right;
198
- }
199
- this._size--;
200
- return x;
201
- }
202
- return t; /* It wasn't there */
203
- }
204
- /**
205
- * Removes and returns the node with smallest key
206
- */
207
- pop() {
208
- let node = this._root;
209
- if (node) {
210
- while (node.left)
211
- node = node.left;
212
- this._root = splay(node.key, this._root, this._comparator);
213
- this._root = this._remove(node.key, this._root, this._comparator);
214
- return { key: node.key, data: node.data };
215
- }
216
- return null;
217
- }
218
- /**
219
- * Find without splaying
220
- */
221
- findStatic(key) {
222
- let current = this._root;
223
- const compare = this._comparator;
224
- while (current) {
225
- const cmp = compare(key, current.key);
226
- if (cmp === 0)
227
- return current;
228
- else if (cmp < 0)
229
- current = current.left;
230
- else
231
- current = current.right;
232
- }
233
- return null;
234
- }
235
- find(key) {
236
- if (this._root) {
237
- this._root = splay(key, this._root, this._comparator);
238
- if (this._comparator(key, this._root.key) !== 0)
239
- return null;
240
- }
241
- return this._root;
242
- }
243
- contains(key) {
244
- let current = this._root;
245
- const compare = this._comparator;
246
- while (current) {
247
- const cmp = compare(key, current.key);
248
- if (cmp === 0)
249
- return true;
250
- else if (cmp < 0)
251
- current = current.left;
252
- else
253
- current = current.right;
254
- }
255
- return false;
256
- }
257
- forEach(visitor, ctx) {
258
- let current = this._root;
259
- const Q = []; /* Initialize stack s */
260
- let done = false;
261
- while (!done) {
262
- if (current !== null) {
263
- Q.push(current);
264
- current = current.left;
265
- }
266
- else {
267
- if (Q.length !== 0) {
268
- current = Q.pop();
269
- visitor.call(ctx, current);
270
- current = current.right;
271
- }
272
- else
273
- done = true;
274
- }
275
- }
276
- return this;
277
- }
278
- /**
279
- * Walk key range from `low` to `high`. Stops if `fn` returns a value.
280
- */
281
- range(low, high, fn, ctx) {
282
- const Q = [];
283
- const compare = this._comparator;
284
- let node = this._root;
285
- let cmp;
286
- while (Q.length !== 0 || node) {
287
- if (node) {
288
- Q.push(node);
289
- node = node.left;
290
- }
291
- else {
292
- node = Q.pop();
293
- cmp = compare(node.key, high);
294
- if (cmp > 0) {
295
- break;
296
- }
297
- else if (compare(node.key, low) >= 0) {
298
- if (fn.call(ctx, node))
299
- return this; // stop if smth is returned
300
- }
301
- node = node.right;
302
- }
303
- }
304
- return this;
305
- }
306
- /**
307
- * Returns array of keys
308
- */
309
- keys() {
310
- const keys = [];
311
- this.forEach(({ key }) => keys.push(key));
312
- return keys;
313
- }
314
- /**
315
- * Returns array of all the data in the nodes
316
- */
317
- values() {
318
- const values = [];
319
- this.forEach(({ data }) => values.push(data));
320
- return values;
321
- }
322
- min() {
323
- if (this._root)
324
- return this.minNode(this._root).key;
325
- return null;
326
- }
327
- max() {
328
- if (this._root)
329
- return this.maxNode(this._root).key;
330
- return null;
331
- }
332
- minNode(t = this._root) {
333
- if (t)
334
- while (t.left)
335
- t = t.left;
336
- return t;
337
- }
338
- maxNode(t = this._root) {
339
- if (t)
340
- while (t.right)
341
- t = t.right;
342
- return t;
343
- }
344
- /**
345
- * Returns node at given index
346
- */
347
- at(index) {
348
- let current = this._root;
349
- let done = false;
350
- let i = 0;
351
- const Q = [];
352
- while (!done) {
353
- if (current) {
354
- Q.push(current);
355
- current = current.left;
356
- }
357
- else {
358
- if (Q.length > 0) {
359
- current = Q.pop();
360
- if (i === index)
361
- return current;
362
- i++;
363
- current = current.right;
364
- }
365
- else
366
- done = true;
367
- }
368
- }
369
- return null;
370
- }
371
- next(d) {
372
- let root = this._root;
373
- let successor = null;
374
- if (d.right) {
375
- successor = d.right;
376
- while (successor.left)
377
- successor = successor.left;
378
- return successor;
379
- }
380
- const comparator = this._comparator;
381
- while (root) {
382
- const cmp = comparator(d.key, root.key);
383
- if (cmp === 0)
384
- break;
385
- else if (cmp < 0) {
386
- successor = root;
387
- root = root.left;
388
- }
389
- else
390
- root = root.right;
391
- }
392
- return successor;
393
- }
394
- prev(d) {
395
- let root = this._root;
396
- let predecessor = null;
397
- if (d.left !== null) {
398
- predecessor = d.left;
399
- while (predecessor.right)
400
- predecessor = predecessor.right;
401
- return predecessor;
402
- }
403
- const comparator = this._comparator;
404
- while (root) {
405
- const cmp = comparator(d.key, root.key);
406
- if (cmp === 0)
407
- break;
408
- else if (cmp < 0)
409
- root = root.left;
410
- else {
411
- predecessor = root;
412
- root = root.right;
413
- }
414
- }
415
- return predecessor;
416
- }
417
- clear() {
418
- this._root = null;
419
- this._size = 0;
420
- return this;
421
- }
422
- toList() {
423
- return toList(this._root);
424
- }
425
- /**
426
- * Bulk-load items. Both array have to be same size
427
- */
428
- load(keys = [], values = [], presort = false) {
429
- let size = keys.length;
430
- const comparator = this._comparator;
431
- // sort if needed
432
- if (presort)
433
- sort(keys, values, 0, size - 1, comparator);
434
- if (this._root === null) { // empty tree
435
- this._root = loadRecursive(keys, values, 0, size);
436
- this._size = size;
437
- }
438
- else { // that re-builds the whole tree from two in-order traversals
439
- const mergedList = mergeLists(this.toList(), createList(keys, values), comparator);
440
- size = this._size + size;
441
- this._root = sortedListToBST({ head: mergedList }, 0, size);
442
- }
443
- return this;
444
- }
445
- isEmpty() { return this._root === null; }
446
- get size() { return this._size; }
447
- get root() { return this._root; }
448
- toString(printNode = (n) => String(n.key)) {
449
- const out = [];
450
- printRow(this._root, '', true, (v) => out.push(v), printNode);
451
- return out.join('');
452
- }
453
- update(key, newKey, newData) {
454
- const comparator = this._comparator;
455
- let { left, right } = split(key, this._root, comparator);
456
- if (comparator(key, newKey) < 0) {
457
- right = insert(newKey, newData, right, comparator);
458
- }
459
- else {
460
- left = insert(newKey, newData, left, comparator);
461
- }
462
- this._root = merge(left, right, comparator);
463
- }
464
- split(key) {
465
- return split(key, this._root, this._comparator);
466
- }
467
- }
468
- function loadRecursive(keys, values, start, end) {
469
- const size = end - start;
470
- if (size > 0) {
471
- const middle = start + Math.floor(size / 2);
472
- const key = keys[middle];
473
- const data = values[middle];
474
- const node = new Node(key, data);
475
- node.left = loadRecursive(keys, values, start, middle);
476
- node.right = loadRecursive(keys, values, middle + 1, end);
477
- return node;
478
- }
479
- return null;
480
- }
481
- function createList(keys, values) {
482
- const head = new Node(null, null);
483
- let p = head;
484
- for (let i = 0; i < keys.length; i++) {
485
- p = p.next = new Node(keys[i], values[i]);
486
- }
487
- p.next = null;
488
- return head.next;
489
- }
490
- function toList(root) {
491
- let current = root;
492
- const Q = [];
493
- let done = false;
494
- const head = new Node(null, null);
495
- let p = head;
496
- while (!done) {
497
- if (current) {
498
- Q.push(current);
499
- current = current.left;
500
- }
501
- else {
502
- if (Q.length > 0) {
503
- current = p = p.next = Q.pop();
504
- current = current.right;
505
- }
506
- else
507
- done = true;
508
- }
509
- }
510
- p.next = null; // that'll work even if the tree was empty
511
- return head.next;
512
- }
513
- function sortedListToBST(list, start, end) {
514
- const size = end - start;
515
- if (size > 0) {
516
- const middle = start + Math.floor(size / 2);
517
- const left = sortedListToBST(list, start, middle);
518
- const root = list.head;
519
- root.left = left;
520
- list.head = list.head.next;
521
- root.right = sortedListToBST(list, middle + 1, end);
522
- return root;
523
- }
524
- return null;
525
- }
526
- function mergeLists(l1, l2, compare = DEFAULT_COMPARE) {
527
- const head = new Node(null, null); // dummy
528
- let p = head;
529
- let p1 = l1;
530
- let p2 = l2;
531
- while (p1 !== null && p2 !== null) {
532
- if (compare(p1.key, p2.key) < 0) {
533
- p.next = p1;
534
- p1 = p1.next;
535
- }
536
- else {
537
- p.next = p2;
538
- p2 = p2.next;
539
- }
540
- p = p.next;
541
- }
542
- if (p1 !== null)
543
- p.next = p1;
544
- else if (p2 !== null)
545
- p.next = p2;
546
- return head.next;
547
- }
548
- function sort(keys, values, left, right, compare) {
549
- if (left >= right)
550
- return;
551
- const pivot = keys[(left + right) >> 1];
552
- let i = left - 1;
553
- let j = right + 1;
554
- while (true) {
555
- do
556
- i++;
557
- while (compare(keys[i], pivot) < 0);
558
- do
559
- j--;
560
- while (compare(keys[j], pivot) > 0);
561
- if (i >= j)
562
- break;
563
- let tmp = keys[i];
564
- keys[i] = keys[j];
565
- keys[j] = tmp;
566
- tmp = values[i];
567
- values[i] = values[j];
568
- values[j] = tmp;
569
- }
570
- sort(keys, values, left, j, compare);
571
- sort(keys, values, j + 1, right, compare);
572
- }
package/dist/node.d.ts DELETED
@@ -1,8 +0,0 @@
1
- export default class Node<Key, Value> {
2
- key: Key;
3
- data: any;
4
- left: Node<Key, Value> | null;
5
- right: Node<Key, Value> | null;
6
- next: Node<Key, Value> | null;
7
- constructor(key: Key, data?: any);
8
- }
package/dist/node.js DELETED
@@ -1,9 +0,0 @@
1
- export default class Node {
2
- constructor(key, data) {
3
- this.next = null;
4
- this.key = key;
5
- this.data = data;
6
- this.left = null;
7
- this.right = null;
8
- }
9
- }
package/dist/types.d.ts DELETED
@@ -1,2 +0,0 @@
1
- export declare type Key = number | any;
2
- export declare type Value = any;
package/dist/types.js DELETED
File without changes