Binary Trees Build Status

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
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:

<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:

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


Binary Trees Build Status

此包提供了以Javascript编写的二进制和红黑搜索树。它是根据MIT许可证发布的。

二进制搜索树是以排序顺序存储数据的好方法。红黑树是平衡本身的二叉树的变体。

从Julienne Walker获取算法: http://eternallyconfuzzled.com/jsw_home.aspx

  • BinTree - 二进制搜索树
  • RBTree - 红黑树
快速入门

node.js:

npm install bintrees
var RBTree = require('bintrees').RBTree;

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

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

有关详细信息,请参阅examples / node.js

在浏览器中:

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

请参阅examples / client.html了解更多信息

构造函数

需要1个参数:比较函数f(a,b)返回:

  • 如果一个== b
  • 则为0
  • 0如果&gt; b

  • &lt; 0如果&lt; b

方法

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.

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

迭代器

tree.iterator()将返回一个空迭代器。在null迭代器上,

  • next()将返回树中的第一个元素
  • prev()将返回树中的最后一个元素

否则,

  • next()将返回下一个元素
  • prev()将返回上一个元素
  • data()将返回迭代器指向
  • 的节点

当迭代到达结束时,迭代器将再次成为一个空迭代器。

转发迭代示例:

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

如果您正在遍历树,可以随时调用prev()返回,反之亦然。

注意:当您从树中添加或删除元素时,迭代器将变为无效。

生产用途

  • Coinbase Exchange ,自2015年1月26日起。
  • 如果您正在制作中使用,请让我知道! (请将您的公司添加到此README中)




相关问题推荐