Binary Search Trees
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
npm install bintrees
var RBTree = require'bintrees'RBTree;var tree = return a - b; ;treeinsert2;treeinsert-3;
see examples/node.js for more info
In the browser:
see examples/client.html for more info
Requires 1 argument: a comparator function f(a,b) which returns:
0 if a > b
Inserts the item into the tree. Returns true if inserted, false if duplicate.
Removes the item from the tree. Returns true if removed, false if not found.
Number of nodes in the tree.
Removes all nodes from the tree.
Returns node data if found, null otherwise.
Returns an iterator to the node if found, null otherwise.
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
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
Returns the min node data in the tree, or null if the tree is empty.
Returns the max node data in the tree, or null if the tree is empty.
Calls f on each node's data, in order.
Calls f on each node's data, in reverse order.
Returns a null-iterator. See Iterators section below.
tree.iterator() will return a null-iterator. On a null iterator,
When iteration reaches the end, the iterator becomes a null-iterator again.
Forward iteration example:
var it=treeiterator item;whileitem = itnext !== 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.