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/Readme.md CHANGED
@@ -1,25 +1,24 @@
1
- # Fast splay tree [![npm version](https://badge.fury.io/js/splaytree.svg)](https://badge.fury.io/js/splaytree) [![build](https://travis-ci.org/w8r/splay-tree.svg?branch=master)](https://travis-ci.org/w8r/splay-tree) ![deps](https://david-dm.org/w8r/splay-tree/status.svg) [![codecov](https://codecov.io/gh/w8r/splay-tree/branch/master/graph/badge.svg)](https://codecov.io/gh/w8r/splay-tree)
1
+ # Fast splay tree [![npm version](https://badge.fury.io/js/splaytree.svg)](https://badge.fury.io/js/splaytree) [![codecov](https://codecov.io/gh/w8r/splay-tree/branch/master/graph/badge.svg)](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
- ![Splay-tree](https://i.stack.imgur.com/CNSAZ.png)
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
- | 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)** |
14
+ ![Splay-tree](https://i.stack.imgur.com/CNSAZ.png)
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 'splaytree';
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
- * `new SplayTree([comparator])`, where `comparator` is optional comparison function
50
- * `tree.insert(key:any, [data:any]):Node` - Insert item, allow duplicate keys
51
- * `tree.add(key:any, [data:any]):Node` - Insert item if it is not present
52
- * `tree.remove(key:any)` - Remove item
53
- * `tree.find(key):Node|Null` - Return node by its key
54
- * `tree.findStatic(key):Node|Null` - Return node by its key (doesn't re-balance the tree)
55
- * `tree.at(index:Number):Node|Null` - Return node by its index in sorted order of keys
56
- * `tree.contains(key):Boolean` - Whether a node with the given key is in the tree
57
- * `tree.forEach(function(node) {...}):Tree` In-order traversal
58
- * `tree.keys():Array<key>` - Returns the array of keys in order
59
- * `tree.values():Array<*>` - Returns the array of data fields in order
60
- * `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.
61
- * `tree.pop():Node` - Removes smallest node
62
- * `tree.min():key` - Returns min key
63
- * `tree.max():key` - Returns max key
64
- * `tree.minNode():Node` - Returns the node with smallest key
65
- * `tree.maxNode():Node` - Returns the node with highest key
66
- * `tree.prev(node):Node` - Predecessor node
67
- * `tree.next(node):Node` - Successor node
68
- * `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).
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
- The comparator function is extremely important, in case of errors you might end
78
- up with a wrongly constructed tree or would not be able to retrieve your items.
79
- It is crucial to test the return values of your `comparator(a,b)` and `comparator(b,a)`
80
- to make sure it's working correctly, otherwise you may have bugs that are very
81
- unpredictable and hard to catch.
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
- **Duplicate keys**
85
+ **Duplicate keys**
84
86
 
85
- * `insert()` method allows duplicate keys. This can be useful in certain applications (example: overlapping
86
- points in 2D).
87
- * `add()` method will not allow duplicate keys - if key is already present in the tree, no new node is created
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 'splaytree';
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); // 5
103
- console.log(t.min()); // -10
104
- console.log(t.max()); // -33
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); // 4
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 'splaytree';
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 'splaytree';
130
+ import Tree from "splaytree";
129
131
 
130
132
  const t = new Tree();
131
- t.load([3,2,-10,20], ['C', 'B', 'A', 'D']);
132
- console.log(t.keys()); // [-10, 2, 3, 20]
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.0
2
+ * splaytree v3.1.2
3
3
  * Fast Splay tree for Node and browser
4
4
  *
5
5
  * @author Alexander Milevski <info@w8r.name>
@@ -7,6 +7,49 @@
7
7
  * @preserve
8
8
  */
9
9
 
10
+ /*! *****************************************************************************
11
+ Copyright (c) Microsoft Corporation. All rights reserved.
12
+ Licensed under the Apache License, Version 2.0 (the "License"); you may not use
13
+ this file except in compliance with the License. You may obtain a copy of the
14
+ License at http://www.apache.org/licenses/LICENSE-2.0
15
+
16
+ THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17
+ KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
18
+ WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
19
+ MERCHANTABLITY OR NON-INFRINGEMENT.
20
+
21
+ See the Apache Version 2.0 License for specific language governing permissions
22
+ and limitations under the License.
23
+ ***************************************************************************** */
24
+
25
+ function __generator(thisArg, body) {
26
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
27
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
28
+ function verb(n) { return function (v) { return step([n, v]); }; }
29
+ function step(op) {
30
+ if (f) throw new TypeError("Generator is already executing.");
31
+ while (_) try {
32
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
33
+ if (y = 0, t) op = [op[0] & 2, t.value];
34
+ switch (op[0]) {
35
+ case 0: case 1: t = op; break;
36
+ case 4: _.label++; return { value: op[1], done: false };
37
+ case 5: _.label++; y = op[1]; op = [0]; continue;
38
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
39
+ default:
40
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
41
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
42
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
43
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
44
+ if (t[2]) _.ops.pop();
45
+ _.trys.pop(); continue;
46
+ }
47
+ op = body.call(thisArg, _);
48
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
49
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
50
+ }
51
+ }
52
+
10
53
  var Node = /** @class */ (function () {
11
54
  function Node(key, data) {
12
55
  this.next = null;
@@ -498,6 +541,37 @@ var Tree = /** @class */ (function () {
498
541
  Tree.prototype.split = function (key) {
499
542
  return split(key, this._root, this._comparator);
500
543
  };
544
+ Tree.prototype[Symbol.iterator] = function () {
545
+ var current, Q, done;
546
+ return __generator(this, function (_a) {
547
+ switch (_a.label) {
548
+ case 0:
549
+ current = this._root;
550
+ Q = [];
551
+ done = false;
552
+ _a.label = 1;
553
+ case 1:
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];
559
+ case 2:
560
+ if (!(Q.length !== 0)) return [3 /*break*/, 4];
561
+ current = Q.pop();
562
+ return [4 /*yield*/, current];
563
+ case 3:
564
+ _a.sent();
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*/];
572
+ }
573
+ });
574
+ };
501
575
  return Tree;
502
576
  }());
503
577
  function loadRecursive(keys, values, start, end) {
@@ -1 +1 @@
1
- {"version":3,"file":"splay.esm.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"splay.esm.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}