rank-tree
Version:
For the structure of sort sets, refer to readme for details
242 lines (150 loc) • 3.83 kB
Markdown
<p align="center">
<h1 align="center">RANK TREE</h1>
<h4 align="center">A sort set based on red-black tree.</h4>
</p>
```bash
npm install rank-tree
```
```js
const { Rank } = require("rank-tree");
let rank = new Rank();
// ...do somethings for value rank
```
The following methods are supported.
| name | opts | performance |
| ---------------- | ----------------------------- | ----------- |
| tadd | [k: string, v: number] | O(logn) |
| trem | [k: string] | O(logn) |
| tincrease | [k: string, v: number] | O(logn) |
| treduce | [k: string, v: number] | O(logn) |
| trank | [k: string] | O(logn) |
| tcount | [minV: number, maxV: number] | O(logn)+m |
| tcard | [] | O(1) |
| trange | [minI: number, maxI?: number] | O(logn)+m |
| trevrangebyscore | [minV: number, maxV?: number] | O(logn)+m |
| tgetall | [] | O(n) |
| tscore | [k: string] | O(1) |
| tgetwithindex | [index: number] | O(logn) |
Insert a `<key-value>` pair into the rank set, and override if the key already exists.
```js
let rank = new Rank();
rank.tadd("a", 1);
rank.tadd("b", 2);
rank.tadd("c", 3);
// tree
[]
```
Find and delete the specified key.
```js
// tree
[]
let rank = new Rank();
rank.trem("a");
rank.trem(["b", "c"]);
// tree
[]
```
Increases the value of the specified key.
```js
// tree
[]
let rank = new Rank();
rank.tincrease("a", 2);
rank.tincrease("c", 1);
// tree
[]
```
Reduces the value of the specified key.
```js
// tree
[]
let rank = new Rank();
rank.treduce("a", 2);
rank.treduce("c", 1);
// tree
[]
```
Gets the ranking of the specified key.
```js
// tree
[]
let rank = new Rank();
rank.trank("a"); // 1
rank.trank("c"); // 3
```
Gets values between rankA and rankB
```js
// tree
[]
let rank = new Rank();
rank.trange(2, 3); // [b:20, c:31]
rank.trange(1, 1); // [a:17]
rank.trange(3); // [c:31, d:56]
```
Gets total nodes count.
```js
// tree
[]
let rank = new Rank();
rank.tcard(); // 4
```
Gets the quantity of nodes with values between vA and vB.
```js
// tree
[]
let rank = new Rank();
rank.tcount(21, 31); // 1
rank.tcount(20, 31); // 2
```
Gets the nodes with values between vA and vB.
```js
// tree
[]
let rank = new Rank();
rank.trevrangebyscore(21, 31); // [c:31]
rank.trevrangebyscore(20, 31); // [b:20, c:31]
```
Gets all nodes.
```js
// tree
[]
let rank = new Rank();
rank.tgetall(); // [a:17, b:20, c:31, d:56]
```
Gets all value with key.
```js
// tree
[]
let rank = new Rank();
rank.tscore("a"); // 17
```
Gets all value with rank.
```js
// tree
[]
let rank = new Rank();
rank.tscore(2); // 20
```
```bash
npm test
```
This repository is licensed under the "MIT" license. See [LICENSE](LICENSE).