https://github.com/unixs/node-btree
Native binary tree for Node.js. Bindings to GLib Balanced Binary Tree (GTree)
https://github.com/unixs/node-btree
btrees glib2 nodejs
Last synced: 5 months ago
JSON representation
Native binary tree for Node.js. Bindings to GLib Balanced Binary Tree (GTree)
- Host: GitHub
- URL: https://github.com/unixs/node-btree
- Owner: unixs
- License: lgpl-3.0
- Created: 2019-08-27T14:52:54.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2026-01-01T19:32:39.000Z (6 months ago)
- Last Synced: 2026-01-07T03:41:27.165Z (6 months ago)
- Topics: btrees, glib2, nodejs
- Language: C
- Homepage:
- Size: 993 KB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# node-btree
[](https://nodei.co/npm/node-btree/)
[](https://github.com/unixs/node-btree/actions)
[](https://codecov.io/gh/unixs/node-btree)
[](https://www.gnu.org/licenses/lgpl-3.0)
[](https://github.com/unixs/node-btree/issues)
## Brief
Node.js native Balanced Binary Tree bindings to [GTree from GLib](https://docs.gtk.org/glib/struct.Tree.html).
Writen on C with [N-API](https://nodejs.org/dist/latest-v12.x/docs/api/n-api.html).
The module implements Map interface: `set()`, `get()`, `has()`, etc.
It has one additional property: `height` that store bTree height, and natively support map/reduce/filter operations.
## Latest release changes
See: [CHANGELOG.md](https://github.com/unixs/node-btree/blob/master/CHANGELOG.md)
## Documentation
See: [GitHub Wiki](https://github.com/unixs/node-btree/wiki/API)
## Limitations
* **All methods will compiled for GLIB version >= 2.68 only!**
* Same as GTree:
> The tree may not be modified while iterating over it (you can't add/remove items).
* Node.js >= 12.
* Support building on POSIX platforms only at this moment.
## Dependency & build
* [CMake tool](https://cmake.org/).
* pkg-config tool (on POSIX)
* libglib binary & C headers
On Ubuntu GNU/Linux this packages must be installed:
* cmake
* build-essentials
* libglib2.0-dev
On MacOS use **brew** and install:
* cmake
* pkg-config
* glib
## Usage
```js
/**
* Basic
*/
const { BTree } = require("node-btree");
function comparator(a, b) {
if (a > b) {
return 1;
}
else if (a < b) {
return -1;
}
else {
return 0;
}
}
const btree = new BTree(comparator);
btree.set("50", 50);
btree.set("30", 30);
btree.set("15", 150);
btree.get("15");
// 150
btree.has("30");
// true
btree.forEach((val, key) => {
console.log(key, val);
});
// "15" 150
// "30" 30
// "50" 50
btree.map((val) => val);
// [150, 30, 50]
btree.reduce((acc, val) => acc + val, 0);
// 230
btree.size;
// 3
btree.height;
// 2
/**
* From Map()
*/
const map = new Map();
map.set(10, "10");
map.set(30, "30");
map.set(80, "80");
map.set(20, "20");
map.set(50, "50");
const btree2 = BTree.from(comparator, map);
btree.size;
// 5
btree.get(30);
// "30"
/**
* From iterable
*/
function* generator() {
for (let i = 0; i < 1000; i++) {
yield {
key: i,
value: `${i}`
};
}
}
const btree = BTree.from(comparator, generator());
btree.size;
// 1000
btree.height;
// 10
btree.get(500);
// "500"
```
See more docs here: [GitHub Wiki](https://github.com/unixs/node-btree/wiki/API)