bbtree
Description
A JavaScript implementation of a self balancing binary search (AVL) tree.
Installation
$ npm install --save bbtree
Usage
Instantiation
The bbtree.createTree([comparer(a, b)])
method instantiates a new tree.
The tree will be ordered based on the comparer function.
If provided, the comparer must return:
- a negative number if
a
is less thanb
- a positive number if
a
is greater thanb
- zero if
a
andb
are equal
let bbtree = ; let { return aaccountNumber - baccountNumber;}; let tree = bbtree; let tree = bbtree;
If no comparer is provided then the following is used to determine order:
{ if a < b return -1; if a > b return 1; return 0;}
API
bulkInsert(values)
Inserts multiple values into the tree.
values
: an array of values to insert.
Returns a promise that resolves to an array of objects containing the value and error that were not inserted or an empty array if all values were inserted.
let bbtree = ;let tree = bbtree; tree ; tree = bbtree;tree ;
count()
Returns a promise that resolves to the number of values contained in the tree.
tree ;
find(matcher(value))
Finds zero or more values.
matcher
: a function that accepts a value and returns true
or false
to indicate whether the value is included in the results.
Returns a promise that resolves to an array of values that match the matcher function or an empty array if no matches are found.
let bbtree = ; let { return aaccountNumber - baccountNumber;}; let tree = bbtree; tree ;
get(key)
Retrieves a value from the tree.
key
: the key used to match the value.
Returns a promise that resolves to the value found or rejects if no match is found. Only the first value found is returned.
let bbtree = ; let { return aaccountNumber - baccountNumber;}; let tree = bbtree; let memberAccount = accountNumber: 42 firstName: 'Arthur' lastName: 'Dent' ;tree ;;
inOrderValues
Gets an iterator for all the values in the tree in sorted order.
let bbtree = ; let { return aaccountNumber - baccountNumber;}; let tree = bbtree; tree ;
insert(value)
Inserts a value into the tree.
value
: the value to insert.
Returns a promise that resolves to the tree if the insert is successful or rejects if the value is invalid or already contained in the tree.
let bbtree = ; let { return aaccountNumber - baccountNumber;}; let tree = bbtree;let memberAccount = 42 'Arthur' 'Dent'; tree ;
remove(key)
Removes a value from the tree.
key
: the key used to determine which value to remove.
Returns a promise that resolves to the tree if the removal is successful or rejects if the key is not found.
let bbtree = ; let { return aaccountNumber - baccountNumber;}; let tree = bbtree; let memberAccount = accountNumber: 42 firstName: 'Arthur' lastName: 'Dent' ;tree ;
traversePreOrder(callback(value))
traverseInOrder(callback(value))
traversePostOrder(callback(value))
Traverses the tree in order calling the callback function with the value of the node.
callback
: the callback that is called for each node.
Each one takes a callback function that is called with the value of each node as the tree is traversed.
let bbtree = ; let { return aaccountNumber - baccountNumber;}; let tree = bbtree;let firstNames = ;tree ;
License
Apache-2.0 © Mark Smith