Skip to content

Commit 24a97e4

Browse files
authored
3.1.2 (#40)
* Update package.json * Update * Readme
1 parent d016cc7 commit 24a97e4

File tree

4 files changed

+61
-59
lines changed

4 files changed

+61
-59
lines changed

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
registry=https://registry.npmjs.org/

Readme.md

Lines changed: 58 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -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)
22

33
[Splay-tree](https://en.wikipedia.org/wiki/Splay_tree): **[fast](#benchmarks)**(non-recursive) and **simple**(< 1000 lines of code)
44
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.
55

6-
76
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
137

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
1513

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)
2215

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)** |
2322

2423
## Install
2524

@@ -28,68 +27,71 @@ npm i -S splaytree
2827
```
2928

3029
```js
31-
import SplayTree from 'splaytree';
30+
import SplayTree from "splaytree";
3231
const tree = new SplayTree();
3332
```
3433

3534
Or get it from CDN
35+
3636
```html
3737
<script src="https://unpkg.com/splaytree"></script>
3838
<script>
3939
var tree = new SplayTree();
4040
...
4141
</script>
4242
```
43+
4344
Or use the compiled version 'dist/splay.js'.
4445

4546
[Try it in your browser](https://npm.runkit.com/splaytree)
4647

4748
## API
4849

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).
6970

7071
**Comparator**
7172

7273
`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`
7674

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.
8284

83-
**Duplicate keys**
85+
**Duplicate keys**
8486

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
8890

8991
## Example
9092

9193
```js
92-
import Tree from 'splaytree';
94+
import Tree from "splaytree";
9395

9496
const t = new Tree();
9597
t.insert(5);
@@ -99,18 +101,18 @@ t.insert(33);
99101
t.insert(2);
100102

101103
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
105107

106108
t.remove(0);
107-
console.log(t.size); // 4
109+
console.log(t.size); // 4
108110
```
109111

110112
**Custom comparator (reverse sort)**
111113

112114
```js
113-
import Tree from 'splaytree';
115+
import Tree from "splaytree";
114116

115117
const t = new Tree((a, b) => b - a);
116118
t.insert(5);
@@ -125,11 +127,11 @@ console.log(t.keys()); // [33, 5, 2, 0, -10]
125127
**Bulk insert**
126128

127129
```js
128-
import Tree from 'splaytree';
130+
import Tree from "splaytree";
129131

130132
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]
133135
console.log(t.values()); // ['A', 'B', 'C', 'D']
134136
```
135137

@@ -196,7 +198,6 @@ npm run build
196198

197199
- [ ] try and add parent fields for efficient `.prev()` and `.next()`, or iterators
198200

199-
200201
## License
201202

202203
The MIT License (MIT)

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "splaytree",
3-
"version": "3.1.1",
3+
"version": "3.1.2",
44
"author": "Alexander Milevski <info@w8r.name>",
55
"license": "MIT",
66
"description": "Fast Splay tree for Node and browser",

0 commit comments

Comments
 (0)