Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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/).
  4. 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.
  5. 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.
  6. # Install
  7. npm install functional-red-black-tree
  8. # Example
  9. Here is an example of some basic usage:
  10. ```javascript
  11. //Load the library
  12. var createTree = require("functional-red-black-tree")
  13. //Create a tree
  14. var t1 = createTree()
  15. //Insert some items into the tree
  16. var t2 = t1.insert(1, "foo")
  17. var t3 = t2.insert(2, "bar")
  18. //Remove something
  19. var t4 = t3.remove(1)
  20. ```
  21. # API
  22. ```javascript
  23. var createTree = require("functional-red-black-tree")
  24. ```
  25. ## Overview
  26. - [Tree methods](#tree-methods)
  27. - [`var tree = createTree([compare])`](#var-tree-=-createtreecompare)
  28. - [`tree.keys`](#treekeys)
  29. - [`tree.values`](#treevalues)
  30. - [`tree.length`](#treelength)
  31. - [`tree.get(key)`](#treegetkey)
  32. - [`tree.insert(key, value)`](#treeinsertkey-value)
  33. - [`tree.remove(key)`](#treeremovekey)
  34. - [`tree.find(key)`](#treefindkey)
  35. - [`tree.ge(key)`](#treegekey)
  36. - [`tree.gt(key)`](#treegtkey)
  37. - [`tree.lt(key)`](#treeltkey)
  38. - [`tree.le(key)`](#treelekey)
  39. - [`tree.at(position)`](#treeatposition)
  40. - [`tree.begin`](#treebegin)
  41. - [`tree.end`](#treeend)
  42. - [`tree.forEach(visitor(key,value)[, lo[, hi]])`](#treeforEachvisitorkeyvalue-lo-hi)
  43. - [`tree.root`](#treeroot)
  44. - [Node properties](#node-properties)
  45. - [`node.key`](#nodekey)
  46. - [`node.value`](#nodevalue)
  47. - [`node.left`](#nodeleft)
  48. - [`node.right`](#noderight)
  49. - [Iterator methods](#iterator-methods)
  50. - [`iter.key`](#iterkey)
  51. - [`iter.value`](#itervalue)
  52. - [`iter.node`](#iternode)
  53. - [`iter.tree`](#itertree)
  54. - [`iter.index`](#iterindex)
  55. - [`iter.valid`](#itervalid)
  56. - [`iter.clone()`](#iterclone)
  57. - [`iter.remove()`](#iterremove)
  58. - [`iter.update(value)`](#iterupdatevalue)
  59. - [`iter.next()`](#iternext)
  60. - [`iter.prev()`](#iterprev)
  61. - [`iter.hasNext`](#iterhasnext)
  62. - [`iter.hasPrev`](#iterhasprev)
  63. ## Tree methods
  64. ### `var tree = createTree([compare])`
  65. Creates an empty functional tree
  66. * `compare` is an optional comparison function, same semantics as array.sort()
  67. **Returns** An empty tree ordered by `compare`
  68. ### `tree.keys`
  69. A sorted array of all the keys in the tree
  70. ### `tree.values`
  71. An array array of all the values in the tree
  72. ### `tree.length`
  73. The number of items in the tree
  74. ### `tree.get(key)`
  75. Retrieves the value associated to the given key
  76. * `key` is the key of the item to look up
  77. **Returns** The value of the first node associated to `key`
  78. ### `tree.insert(key, value)`
  79. Creates a new tree with the new pair inserted.
  80. * `key` is the key of the item to insert
  81. * `value` is the value of the item to insert
  82. **Returns** A new tree with `key` and `value` inserted
  83. ### `tree.remove(key)`
  84. Removes the first item with `key` in the tree
  85. * `key` is the key of the item to remove
  86. **Returns** A new tree with the given item removed if it exists
  87. ### `tree.find(key)`
  88. Returns an iterator pointing to the first item in the tree with `key`, otherwise `null`.
  89. ### `tree.ge(key)`
  90. Find the first item in the tree whose key is `>= key`
  91. * `key` is the key to search for
  92. **Returns** An iterator at the given element.
  93. ### `tree.gt(key)`
  94. Finds the first item in the tree whose key is `> key`
  95. * `key` is the key to search for
  96. **Returns** An iterator at the given element
  97. ### `tree.lt(key)`
  98. Finds the last item in the tree whose key is `< key`
  99. * `key` is the key to search for
  100. **Returns** An iterator at the given element
  101. ### `tree.le(key)`
  102. Finds the last item in the tree whose key is `<= key`
  103. * `key` is the key to search for
  104. **Returns** An iterator at the given element
  105. ### `tree.at(position)`
  106. Finds an iterator starting at the given element
  107. * `position` is the index at which the iterator gets created
  108. **Returns** An iterator starting at position
  109. ### `tree.begin`
  110. An iterator pointing to the first element in the tree
  111. ### `tree.end`
  112. An iterator pointing to the last element in the tree
  113. ### `tree.forEach(visitor(key,value)[, lo[, hi]])`
  114. Walks a visitor function over the nodes of the tree in order.
  115. * `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.
  116. * `lo` is an optional start of the range to visit (inclusive)
  117. * `hi` is an optional end of the range to visit (non-inclusive)
  118. **Returns** The last value returned by the callback
  119. ### `tree.root`
  120. Returns the root node of the tree
  121. ## Node properties
  122. Each node of the tree has the following properties:
  123. ### `node.key`
  124. The key associated to the node
  125. ### `node.value`
  126. The value associated to the node
  127. ### `node.left`
  128. The left subtree of the node
  129. ### `node.right`
  130. The right subtree of the node
  131. ## Iterator methods
  132. ### `iter.key`
  133. The key of the item referenced by the iterator
  134. ### `iter.value`
  135. The value of the item referenced by the iterator
  136. ### `iter.node`
  137. The value of the node at the iterator's current position. `null` is iterator is node valid.
  138. ### `iter.tree`
  139. The tree associated to the iterator
  140. ### `iter.index`
  141. Returns the position of this iterator in the sequence.
  142. ### `iter.valid`
  143. Checks if the iterator is valid
  144. ### `iter.clone()`
  145. Makes a copy of the iterator
  146. ### `iter.remove()`
  147. Removes the item at the position of the iterator
  148. **Returns** A new binary search tree with `iter`'s item removed
  149. ### `iter.update(value)`
  150. Updates the value of the node in the tree at this iterator
  151. **Returns** A new binary search tree with the corresponding node updated
  152. ### `iter.next()`
  153. Advances the iterator to the next position
  154. ### `iter.prev()`
  155. Moves the iterator backward one element
  156. ### `iter.hasNext`
  157. If true, then the iterator is not at the end of the sequence
  158. ### `iter.hasPrev`
  159. If true, then the iterator is not at the beginning of the sequence
  160. # Credits
  161. (c) 2013 Mikola Lysenko. MIT License