bstree
A binary search tree implementation for node and browser.
Install
npm install bstree
Example:
var BSTree = ;var tree = ;treetreetreetreetop // 3treebottom // 1tree //3treetop // 2tree // 1tree // 2
Test
#required: sudo npm install -g grunt-cligrunt test
#constructor(comparator)
Parameters:
- comparator:
A
function
that takes two arguments that correspond to tree node values. The return of this function is a boolean. If the value returned is true, the first argument is less than the second argument. If the value returned is false, the two arguments are equal or the second argument is greater than the first argument. Defaults to:function(a, b) { return a < b }
Example:
var BSTree =var tree = {return b > a}// orvar otherTree =
Algorithm complexity:
Constant
#add(element)
Adds the given element from the tree.
Example
var tree = ;tree
Algorithm complexity:
In the average case: logarithmic in #length
In the worst case: linear in #length
#bottom()
Returns the minimum value in the tree.
Example:
var tree =;7310516982console // 1
Algorithm complexity:
Constant in #length
#length
property that indicates the number of elements in tree.
Example:
var tree =console // 0treeconsole // 1
#top()
Returns the maximum value in the tree.
Example:
var tree =;7310516982;console // 10
Algorithm complexity:
Constant in #length
#remove(element)
Removes the given element from the tree if it exists. Returns element
if a removal occurred. Returns undefined
if not.
Example
var tree =;7310516982;console // 8var sorted_array =treeconsole // [1, 2, 3, 4, 5, 6, 7, 9, 10]
Algorithm complexity:
In the average case: logarithmic in #length
In the worst case: linear in #length
#removeBottom()
Removes and returns the bottom element of tree relative to comparator.
If it does not exist (tree is empty), returns undefined
Example:
var tree =;7310516982;console // 1var sorted_array =treeconsole // [2, 3, 4, 5, 6, 7, 8, 9, 10]
Algorithm complexity:
In the average case: logarithmic in #length
In the worst case: linear in #length
#removeTop()
Removes and returns the top element of tree relative to comparator.
If it does not exist (tree is empty), returns undefined
Example:
var tree =;7310516982;console // 10var sorted_array =treeconsole // [1, 2, 3, 4, 5, 6, 7, 8, 9]
Algorithm complexity:
In the average case: logarithmic in #length
In the worst case: linear in #length