# bintrees

> Binary Search Trees

Latest version **1.0.2** (published 2017-08-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install bintrees
pnpm add bintrees
yarn add bintrees
bun add bintrees
```

## Health

**Score 23/100 (F)** — status: abandoned.

Positive: has types package; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2017-08-05 |
| First published | 2011-08-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/bintrees) |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 222 |
| Author | Vadim Graboys |
| Maintainers | vadimg |
| Keywords | binary tree, red black tree, red-black tree, redblack tree |

## Links

- npm: https://www.npmjs.com/package/bintrees
- Repository: https://github.com/vadimg/js_bintrees
- Homepage: https://github.com/vadimg/js_bintrees#readme
- Issues: https://github.com/vadimg/js_bintrees/issues
- npm.io page: https://npm.io/package/bintrees

## Recent versions

- 1.0.2 (latest) — 2017-08-05
- 1.0.1 — 2015-07-24
- 1.0.0 — 2013-04-27
- 0.0.10 — 2013-04-21
- 0.0.9 — 2012-12-22
- 0.0.8 — 2012-12-22
- 0.0.7 — 2011-10-28
- 0.0.6 — 2011-10-28
- 0.0.5 — 2011-10-28
- 0.0.4 — 2011-08-03
- 0.0.3 — 2011-08-03
- 0.0.2 — 2011-08-03
- 0.0.1 — 2011-08-03

## README

Binary Trees [![Build Status](https://secure.travis-ci.org/vadimg/js_bintrees.png?branch=master)](http://travis-ci.org/vadimg/js_bintrees)
============

This package provides Binary and Red-Black Search Trees written in Javascript. It is released under the MIT License.

Binary Search Trees are a good way to store data in sorted order. A Red-Black tree is a variation of a Binary Tree that balances itself.

Algorithms were taken from Julienne Walker: http://eternallyconfuzzled.com/jsw_home.aspx

Trees
------------

* BinTree - Binary Search Tree
* RBTree - Red-Black Tree

Quickstart
------------
node.js:

```
npm install bintrees
```

```javascript
var RBTree = require('bintrees').RBTree;

var tree = new RBTree(function(a, b) { return a - b; });

tree.insert(2);
tree.insert(-3);
```

see examples/node.js for more info

In the browser:

```html
<script src="/path/to/rbtree.js"></script>
<script>
    var tree = new RBTree(function(a, b) { return a - b; });
    tree.insert(0);
    tree.insert(1);
</script>
```

see examples/client.html for more info

Constructor
------------

Requires 1 argument: a comparator function f(a,b) which returns:
* 0 if a == b
* >0 if a > b
* <0 if a < b

Methods
------------

### insert(item)
> Inserts the item into the tree. Returns true if inserted, false if duplicate.

### remove(item)
> Removes the item from the tree. Returns true if removed, false if not found.

### size
> Number of nodes in the tree.

### clear()
> Removes all nodes from the tree.

### find(item)
> Returns node data if found, null otherwise.

### findIter(item)
> Returns an iterator to the node if found, null otherwise.

### lowerBound(item)
> Returns an iterator to the tree node at or immediately after the item. Returns null-iterator if tree is empty.
>> __NOTE: Changed in version 1.0.0 to match C++ lower_bound__

### upperBound(item)
> Returns an iterator to the tree node immediately after the item. Returns null-iterator if tree is empty.
>> __NOTE: Changed in version 1.0.0 to match C++ upper_bound__

### min()
> Returns the min node data in the tree, or null if the tree is empty.

### max()
> Returns the max node data in the tree, or null if the tree is empty.

### each(f)
> Calls f on each node's data, in order.

### reach(f)
> Calls f on each node's data, in reverse order.

### iterator()
> Returns a null-iterator. See __Iterators__ section below.

Iterators
------------

tree.iterator() will return a null-iterator. On a null iterator,
* next() will return the first element in the tree
* prev() will return the last element in the tree

Otherwise,
* next() will return the next element
* prev() will return the previous element
* data() will return the node the iterator is pointing to

When iteration reaches the end, the iterator becomes a null-iterator again.

Forward iteration example:

```javascript
var it=tree.iterator(), item;
while((item = it.next()) !== null) {
    // do stuff with item
}
```

If you are iterating forward through the tree, you can always call prev() to go back, and vice versa.

__NOTE:__ iterators become invalid when you add or remove elements from the tree.

## Production Usage

* [Coinbase Exchange](https://exchange.coinbase.com/), since Jan 26, 2015.
* If you are using this in production, please let me know! (add your company to this README in a pull request)

---
_Source: https://npm.io/package/bintrees · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
