splaytree 3.1.1 → 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/Readme.md +58 -57
- package/dist/splay.esm.js +21 -8
- package/dist/splay.esm.js.map +1 -1
- package/dist/splay.js +21 -8
- package/dist/splay.js.map +1 -1
- package/package.json +2 -2
- package/src/index.ts +16 -4
package/Readme.md
CHANGED
|
@@ -1,25 +1,24 @@
|
|
|
1
|
-
# Fast splay tree [](https://badge.fury.io/js/splaytree) [](https://badge.fury.io/js/splaytree) [](https://codecov.io/gh/w8r/splay-tree)
|
|
2
2
|
|
|
3
3
|
[Splay-tree](https://en.wikipedia.org/wiki/Splay_tree): **[fast](#benchmarks)**(non-recursive) and **simple**(< 1000 lines of code)
|
|
4
4
|
Implementation is adapted directly from Wikipedia with the same API as [w8r/avl](https://github.com/w8r/avl), to run the benchmarks against other trees.
|
|
5
5
|
|
|
6
|
-
|
|
7
6
|
This tree is based on **top-down** splaying algorithm by D.Sleator. It supports
|
|
8
|
-
- splitting, merging
|
|
9
|
-
- updating of the keys
|
|
10
|
-
- bulk loading of the items into an empty or non-empty tree
|
|
11
|
-
- insertion with duplicates or no duplicates
|
|
12
|
-
- lookup without splaying
|
|
13
7
|
|
|
14
|
-
|
|
8
|
+
- splitting, merging
|
|
9
|
+
- updating of the keys
|
|
10
|
+
- bulk loading of the items into an empty or non-empty tree
|
|
11
|
+
- insertion with duplicates or no duplicates
|
|
12
|
+
- lookup without splaying
|
|
15
13
|
|
|
16
|
-
|
|
17
|
-
| ------------- | ------------- | ---------------------- |
|
|
18
|
-
| Space | **O(n)** | **O(n)** |
|
|
19
|
-
| Search | **O(log n)** | **amortized O(log n)** |
|
|
20
|
-
| Insert | **O(log n)** | **amortized O(log n)** |
|
|
21
|
-
| Delete | **O(log n)** | **amortized O(log n)** |
|
|
14
|
+

|
|
22
15
|
|
|
16
|
+
| Operation | Average | Worst case |
|
|
17
|
+
| --------- | ------------ | ---------------------- |
|
|
18
|
+
| Space | **O(n)** | **O(n)** |
|
|
19
|
+
| Search | **O(log n)** | **amortized O(log n)** |
|
|
20
|
+
| Insert | **O(log n)** | **amortized O(log n)** |
|
|
21
|
+
| Delete | **O(log n)** | **amortized O(log n)** |
|
|
23
22
|
|
|
24
23
|
## Install
|
|
25
24
|
|
|
@@ -28,11 +27,12 @@ npm i -S splaytree
|
|
|
28
27
|
```
|
|
29
28
|
|
|
30
29
|
```js
|
|
31
|
-
import SplayTree from
|
|
30
|
+
import SplayTree from "splaytree";
|
|
32
31
|
const tree = new SplayTree();
|
|
33
32
|
```
|
|
34
33
|
|
|
35
34
|
Or get it from CDN
|
|
35
|
+
|
|
36
36
|
```html
|
|
37
37
|
<script src="https://unpkg.com/splaytree"></script>
|
|
38
38
|
<script>
|
|
@@ -40,56 +40,58 @@ Or get it from CDN
|
|
|
40
40
|
...
|
|
41
41
|
</script>
|
|
42
42
|
```
|
|
43
|
+
|
|
43
44
|
Or use the compiled version 'dist/splay.js'.
|
|
44
45
|
|
|
45
46
|
[Try it in your browser](https://npm.runkit.com/splaytree)
|
|
46
47
|
|
|
47
48
|
## API
|
|
48
49
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
50
|
+
- `new SplayTree([comparator])`, where `comparator` is optional comparison function
|
|
51
|
+
- `tree.insert(key:any, [data:any]):Node` - Insert item, allow duplicate keys
|
|
52
|
+
- `tree.add(key:any, [data:any]):Node` - Insert item if it is not present
|
|
53
|
+
- `tree.remove(key:any)` - Remove item
|
|
54
|
+
- `tree.find(key):Node|Null` - Return node by its key
|
|
55
|
+
- `tree.findStatic(key):Node|Null` - Return node by its key (doesn't re-balance the tree)
|
|
56
|
+
- `tree.at(index:Number):Node|Null` - Return node by its index in sorted order of keys
|
|
57
|
+
- `tree.contains(key):Boolean` - Whether a node with the given key is in the tree
|
|
58
|
+
- `tree.forEach(function(node) {...}):Tree` In-order traversal
|
|
59
|
+
- `tree.keys():Array<key>` - Returns the array of keys in order
|
|
60
|
+
- `tree.values():Array<*>` - Returns the array of data fields in order
|
|
61
|
+
- `tree.range(lo, high, function(node) {} [, context]):Tree` - Walks the range of keys in order. Stops, if the visitor function returns a non-zero value.
|
|
62
|
+
- `tree.pop():Node` - Removes smallest node
|
|
63
|
+
- `tree.min():key` - Returns min key
|
|
64
|
+
- `tree.max():key` - Returns max key
|
|
65
|
+
- `tree.minNode():Node` - Returns the node with smallest key
|
|
66
|
+
- `tree.maxNode():Node` - Returns the node with highest key
|
|
67
|
+
- `tree.prev(node):Node` - Predecessor node
|
|
68
|
+
- `tree.next(node):Node` - Successor node
|
|
69
|
+
- `tree.load(keys:Array<*>, [values:Array<*>][,presort=false]):Tree` - Bulk-load items. It expects values and keys to be sorted, but if `presort` is `true`, it will sort keys and values using the comparator(in-place, your arrays are going to be altered).
|
|
69
70
|
|
|
70
71
|
**Comparator**
|
|
71
72
|
|
|
72
73
|
`function(a:key,b:key):Number` - Comparator function between two keys, it returns
|
|
73
|
-
* `0` if the keys are equal
|
|
74
|
-
* `<0` if `a < b`
|
|
75
|
-
* `>0` if `a > b`
|
|
76
74
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
75
|
+
- `0` if the keys are equal
|
|
76
|
+
- `<0` if `a < b`
|
|
77
|
+
- `>0` if `a > b`
|
|
78
|
+
|
|
79
|
+
The comparator function is extremely important, in case of errors you might end
|
|
80
|
+
up with a wrongly constructed tree or would not be able to retrieve your items.
|
|
81
|
+
It is crucial to test the return values of your `comparator(a,b)` and `comparator(b,a)`
|
|
82
|
+
to make sure it's working correctly, otherwise you may have bugs that are very
|
|
83
|
+
unpredictable and hard to catch.
|
|
82
84
|
|
|
83
|
-
|
|
85
|
+
**Duplicate keys**
|
|
84
86
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
87
|
+
- `insert()` method allows duplicate keys. This can be useful in certain applications (example: overlapping
|
|
88
|
+
points in 2D).
|
|
89
|
+
- `add()` method will not allow duplicate keys - if key is already present in the tree, no new node is created
|
|
88
90
|
|
|
89
91
|
## Example
|
|
90
92
|
|
|
91
93
|
```js
|
|
92
|
-
import Tree from
|
|
94
|
+
import Tree from "splaytree";
|
|
93
95
|
|
|
94
96
|
const t = new Tree();
|
|
95
97
|
t.insert(5);
|
|
@@ -99,18 +101,18 @@ t.insert(33);
|
|
|
99
101
|
t.insert(2);
|
|
100
102
|
|
|
101
103
|
console.log(t.keys()); // [-10, 0, 2, 5, 33]
|
|
102
|
-
console.log(t.size);
|
|
103
|
-
console.log(t.min());
|
|
104
|
-
console.log(t.max());
|
|
104
|
+
console.log(t.size); // 5
|
|
105
|
+
console.log(t.min()); // -10
|
|
106
|
+
console.log(t.max()); // -33
|
|
105
107
|
|
|
106
108
|
t.remove(0);
|
|
107
|
-
console.log(t.size);
|
|
109
|
+
console.log(t.size); // 4
|
|
108
110
|
```
|
|
109
111
|
|
|
110
112
|
**Custom comparator (reverse sort)**
|
|
111
113
|
|
|
112
114
|
```js
|
|
113
|
-
import Tree from
|
|
115
|
+
import Tree from "splaytree";
|
|
114
116
|
|
|
115
117
|
const t = new Tree((a, b) => b - a);
|
|
116
118
|
t.insert(5);
|
|
@@ -125,11 +127,11 @@ console.log(t.keys()); // [33, 5, 2, 0, -10]
|
|
|
125
127
|
**Bulk insert**
|
|
126
128
|
|
|
127
129
|
```js
|
|
128
|
-
import Tree from
|
|
130
|
+
import Tree from "splaytree";
|
|
129
131
|
|
|
130
132
|
const t = new Tree();
|
|
131
|
-
t.load([3,2
|
|
132
|
-
console.log(t.keys());
|
|
133
|
+
t.load([3, 2, -10, 20], ["C", "B", "A", "D"]);
|
|
134
|
+
console.log(t.keys()); // [-10, 2, 3, 20]
|
|
133
135
|
console.log(t.values()); // ['A', 'B', 'C', 'D']
|
|
134
136
|
```
|
|
135
137
|
|
|
@@ -196,7 +198,6 @@ npm run build
|
|
|
196
198
|
|
|
197
199
|
- [ ] try and add parent fields for efficient `.prev()` and `.next()`, or iterators
|
|
198
200
|
|
|
199
|
-
|
|
200
201
|
## License
|
|
201
202
|
|
|
202
203
|
The MIT License (MIT)
|
package/dist/splay.esm.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* splaytree v3.1.
|
|
2
|
+
* splaytree v3.1.2
|
|
3
3
|
* Fast Splay tree for Node and browser
|
|
4
4
|
*
|
|
5
5
|
* @author Alexander Milevski <info@w8r.name>
|
|
@@ -542,20 +542,33 @@ var Tree = /** @class */ (function () {
|
|
|
542
542
|
return split(key, this._root, this._comparator);
|
|
543
543
|
};
|
|
544
544
|
Tree.prototype[Symbol.iterator] = function () {
|
|
545
|
-
var
|
|
545
|
+
var current, Q, done;
|
|
546
546
|
return __generator(this, function (_a) {
|
|
547
547
|
switch (_a.label) {
|
|
548
548
|
case 0:
|
|
549
|
-
|
|
549
|
+
current = this._root;
|
|
550
|
+
Q = [];
|
|
551
|
+
done = false;
|
|
550
552
|
_a.label = 1;
|
|
551
553
|
case 1:
|
|
552
|
-
if (
|
|
553
|
-
return [
|
|
554
|
+
if (!!done) return [3 /*break*/, 6];
|
|
555
|
+
if (!(current !== null)) return [3 /*break*/, 2];
|
|
556
|
+
Q.push(current);
|
|
557
|
+
current = current.left;
|
|
558
|
+
return [3 /*break*/, 5];
|
|
554
559
|
case 2:
|
|
560
|
+
if (!(Q.length !== 0)) return [3 /*break*/, 4];
|
|
561
|
+
current = Q.pop();
|
|
562
|
+
return [4 /*yield*/, current];
|
|
563
|
+
case 3:
|
|
555
564
|
_a.sent();
|
|
556
|
-
|
|
557
|
-
return [3 /*break*/,
|
|
558
|
-
case
|
|
565
|
+
current = current.right;
|
|
566
|
+
return [3 /*break*/, 5];
|
|
567
|
+
case 4:
|
|
568
|
+
done = true;
|
|
569
|
+
_a.label = 5;
|
|
570
|
+
case 5: return [3 /*break*/, 1];
|
|
571
|
+
case 6: return [2 /*return*/];
|
|
559
572
|
}
|
|
560
573
|
});
|
|
561
574
|
};
|
package/dist/splay.esm.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"splay.esm.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"splay.esm.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
package/dist/splay.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* splaytree v3.1.
|
|
2
|
+
* splaytree v3.1.2
|
|
3
3
|
* Fast Splay tree for Node and browser
|
|
4
4
|
*
|
|
5
5
|
* @author Alexander Milevski <info@w8r.name>
|
|
@@ -548,20 +548,33 @@
|
|
|
548
548
|
return split(key, this._root, this._comparator);
|
|
549
549
|
};
|
|
550
550
|
Tree.prototype[Symbol.iterator] = function () {
|
|
551
|
-
var
|
|
551
|
+
var current, Q, done;
|
|
552
552
|
return __generator(this, function (_a) {
|
|
553
553
|
switch (_a.label) {
|
|
554
554
|
case 0:
|
|
555
|
-
|
|
555
|
+
current = this._root;
|
|
556
|
+
Q = [];
|
|
557
|
+
done = false;
|
|
556
558
|
_a.label = 1;
|
|
557
559
|
case 1:
|
|
558
|
-
if (
|
|
559
|
-
return [
|
|
560
|
+
if (!!done) return [3 /*break*/, 6];
|
|
561
|
+
if (!(current !== null)) return [3 /*break*/, 2];
|
|
562
|
+
Q.push(current);
|
|
563
|
+
current = current.left;
|
|
564
|
+
return [3 /*break*/, 5];
|
|
560
565
|
case 2:
|
|
566
|
+
if (!(Q.length !== 0)) return [3 /*break*/, 4];
|
|
567
|
+
current = Q.pop();
|
|
568
|
+
return [4 /*yield*/, current];
|
|
569
|
+
case 3:
|
|
561
570
|
_a.sent();
|
|
562
|
-
|
|
563
|
-
return [3 /*break*/,
|
|
564
|
-
case
|
|
571
|
+
current = current.right;
|
|
572
|
+
return [3 /*break*/, 5];
|
|
573
|
+
case 4:
|
|
574
|
+
done = true;
|
|
575
|
+
_a.label = 5;
|
|
576
|
+
case 5: return [3 /*break*/, 1];
|
|
577
|
+
case 6: return [2 /*return*/];
|
|
565
578
|
}
|
|
566
579
|
});
|
|
567
580
|
};
|
package/dist/splay.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"splay.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"splay.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "splaytree",
|
|
3
|
-
"version": "3.1.
|
|
3
|
+
"version": "3.1.2",
|
|
4
4
|
"author": "Alexander Milevski <info@w8r.name>",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"description": "Fast Splay tree for Node and browser",
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
"chai": "^4.2.0",
|
|
46
46
|
"codecov": "^3.8.3",
|
|
47
47
|
"mocha": "^6.2.0",
|
|
48
|
-
"nodemon": "^
|
|
48
|
+
"nodemon": "^2.0.20",
|
|
49
49
|
"nyc": "^14.1.1",
|
|
50
50
|
"reify": "*",
|
|
51
51
|
"rollup": "*",
|
package/src/index.ts
CHANGED
|
@@ -531,10 +531,22 @@ export default class Tree<Key=number, Value=any> {
|
|
|
531
531
|
}
|
|
532
532
|
|
|
533
533
|
*[Symbol.iterator]() {
|
|
534
|
-
let
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
534
|
+
let current = this._root;
|
|
535
|
+
const Q: Node<Key, Value>[] = []; /* Initialize stack s */
|
|
536
|
+
let done = false;
|
|
537
|
+
|
|
538
|
+
while (!done) {
|
|
539
|
+
if (current !== null) {
|
|
540
|
+
Q.push(current);
|
|
541
|
+
current = current.left;
|
|
542
|
+
} else {
|
|
543
|
+
if (Q.length !== 0) {
|
|
544
|
+
current = Q.pop()!;
|
|
545
|
+
yield current;
|
|
546
|
+
|
|
547
|
+
current = current.right;
|
|
548
|
+
} else done = true;
|
|
549
|
+
}
|
|
538
550
|
}
|
|
539
551
|
}
|
|
540
552
|
}
|