varbst=require('node-bst'),BSTNode=bst.BSTNode,BSTree=bst.BSTree;// create a nodevarnode=newBSTNode({data : 1}).extend({customMethod : function(){}},true);// pass in true flag to override existing properties or functions// ORvarnode=newBSTNode({data : {firstname : "johnny",lastname : "appleseed"}// data can be an object as well}).extend({isEqualTo : function(otherNode){// be sure to override comparators!returnthis.data.firstname===otherNode.data.firstname&&this.data.lastname===otherNode.data.lastname;},// and so forth for isGreaterThan and isLessThan},true);// OR// declare comparators separatelyvarcomp={isGreaterThan : function(otherNode){// do something},isLessThan : function(otherNode){// do something},isEqualTo : function(otherNode){// do something}};varnode=newBSTNode({data : 1}).extend(comp,true);// Once you have a node, create a tree and append your nodevartree=newBSTree();tree.append(singleNode);// OR append an array of nodes. Nodes will be appended in the order they appear// in the arraytree.append([appendFirst,appendSecond,appendThird]);tree.findNode(1,callback(foundNode){// find your nodeconsole.log(foundNode);});
Traversing
tree.traverse(order, process, onComplete);
order - specifies the node visitation order ('preOrder' || 'inOrder' || 'postOrder')
process - callback to process a node's data. Takes the node's data as an argument
onComplete - callback fires when all nodes have been processed
Full examples under examples/ directory