1 functional-red-black-tree
2 =========================
3 A [fully persistent](http://en.wikipedia.org/wiki/Persistent_data_structure) [red-black tree](http://en.wikipedia.org/wiki/Red%E2%80%93black_tree) written 100% in JavaScript. Works both in node.js and in the browser via [browserify](http://browserify.org/).
5 Functional (or fully presistent) data structures allow for non-destructive updates. So if you insert an element into the tree, it returns a new tree with the inserted element rather than destructively updating the existing tree in place. Doing this requires using extra memory, and if one were naive it could cost as much as reallocating the entire tree. Instead, this data structure saves some memory by recycling references to previously allocated subtrees. This requires using only O(log(n)) additional memory per update instead of a full O(n) copy.
7 Some advantages of this is that it is possible to apply insertions and removals to the tree while still iterating over previous versions of the tree. Functional and persistent data structures can also be useful in many geometric algorithms like point location within triangulations or ray queries, and can be used to analyze the history of executing various algorithms. This added power though comes at a cost, since it is generally a bit slower to use a functional data structure than an imperative version. However, if your application needs this behavior then you may consider using this module.
11 npm install functional-red-black-tree
15 Here is an example of some basic usage:
19 var createTree = require("functional-red-black-tree")
24 //Insert some items into the tree
25 var t2 = t1.insert(1, "foo")
26 var t3 = t2.insert(2, "bar")
36 var createTree = require("functional-red-black-tree")
41 - [Tree methods](#tree-methods)
42 - [`var tree = createTree([compare])`](#var-tree-=-createtreecompare)
43 - [`tree.keys`](#treekeys)
44 - [`tree.values`](#treevalues)
45 - [`tree.length`](#treelength)
46 - [`tree.get(key)`](#treegetkey)
47 - [`tree.insert(key, value)`](#treeinsertkey-value)
48 - [`tree.remove(key)`](#treeremovekey)
49 - [`tree.find(key)`](#treefindkey)
50 - [`tree.ge(key)`](#treegekey)
51 - [`tree.gt(key)`](#treegtkey)
52 - [`tree.lt(key)`](#treeltkey)
53 - [`tree.le(key)`](#treelekey)
54 - [`tree.at(position)`](#treeatposition)
55 - [`tree.begin`](#treebegin)
56 - [`tree.end`](#treeend)
57 - [`tree.forEach(visitor(key,value)[, lo[, hi]])`](#treeforEachvisitorkeyvalue-lo-hi)
58 - [`tree.root`](#treeroot)
59 - [Node properties](#node-properties)
60 - [`node.key`](#nodekey)
61 - [`node.value`](#nodevalue)
62 - [`node.left`](#nodeleft)
63 - [`node.right`](#noderight)
64 - [Iterator methods](#iterator-methods)
65 - [`iter.key`](#iterkey)
66 - [`iter.value`](#itervalue)
67 - [`iter.node`](#iternode)
68 - [`iter.tree`](#itertree)
69 - [`iter.index`](#iterindex)
70 - [`iter.valid`](#itervalid)
71 - [`iter.clone()`](#iterclone)
72 - [`iter.remove()`](#iterremove)
73 - [`iter.update(value)`](#iterupdatevalue)
74 - [`iter.next()`](#iternext)
75 - [`iter.prev()`](#iterprev)
76 - [`iter.hasNext`](#iterhasnext)
77 - [`iter.hasPrev`](#iterhasprev)
81 ### `var tree = createTree([compare])`
82 Creates an empty functional tree
84 * `compare` is an optional comparison function, same semantics as array.sort()
86 **Returns** An empty tree ordered by `compare`
89 A sorted array of all the keys in the tree
92 An array array of all the values in the tree
95 The number of items in the tree
98 Retrieves the value associated to the given key
100 * `key` is the key of the item to look up
102 **Returns** The value of the first node associated to `key`
104 ### `tree.insert(key, value)`
105 Creates a new tree with the new pair inserted.
107 * `key` is the key of the item to insert
108 * `value` is the value of the item to insert
110 **Returns** A new tree with `key` and `value` inserted
112 ### `tree.remove(key)`
113 Removes the first item with `key` in the tree
115 * `key` is the key of the item to remove
117 **Returns** A new tree with the given item removed if it exists
120 Returns an iterator pointing to the first item in the tree with `key`, otherwise `null`.
123 Find the first item in the tree whose key is `>= key`
125 * `key` is the key to search for
127 **Returns** An iterator at the given element.
130 Finds the first item in the tree whose key is `> key`
132 * `key` is the key to search for
134 **Returns** An iterator at the given element
137 Finds the last item in the tree whose key is `< key`
139 * `key` is the key to search for
141 **Returns** An iterator at the given element
144 Finds the last item in the tree whose key is `<= key`
146 * `key` is the key to search for
148 **Returns** An iterator at the given element
150 ### `tree.at(position)`
151 Finds an iterator starting at the given element
153 * `position` is the index at which the iterator gets created
155 **Returns** An iterator starting at position
158 An iterator pointing to the first element in the tree
161 An iterator pointing to the last element in the tree
163 ### `tree.forEach(visitor(key,value)[, lo[, hi]])`
164 Walks a visitor function over the nodes of the tree in order.
166 * `visitor(key,value)` is a callback that gets executed on each node. If a truthy value is returned from the visitor, then iteration is stopped.
167 * `lo` is an optional start of the range to visit (inclusive)
168 * `hi` is an optional end of the range to visit (non-inclusive)
170 **Returns** The last value returned by the callback
173 Returns the root node of the tree
177 Each node of the tree has the following properties:
180 The key associated to the node
183 The value associated to the node
186 The left subtree of the node
189 The right subtree of the node
194 The key of the item referenced by the iterator
197 The value of the item referenced by the iterator
200 The value of the node at the iterator's current position. `null` is iterator is node valid.
203 The tree associated to the iterator
206 Returns the position of this iterator in the sequence.
209 Checks if the iterator is valid
212 Makes a copy of the iterator
215 Removes the item at the position of the iterator
217 **Returns** A new binary search tree with `iter`'s item removed
219 ### `iter.update(value)`
220 Updates the value of the node in the tree at this iterator
222 **Returns** A new binary search tree with the corresponding node updated
225 Advances the iterator to the next position
228 Moves the iterator backward one element
231 If true, then the iterator is not at the end of the sequence
234 If true, then the iterator is not at the beginning of the sequence
237 (c) 2013 Mikola Lysenko. MIT License