Binary Search Tree(BST) and Red Black Tree(RBT) structures
This is an implementation of BST and RBT using the theory explained in Introduction to Algorithms relative to this structures. This first version of the package only support numeric values for tree nodes, in future releases this can be changed and object as values can be used ;)
Example
const createRBT = ; const newRBT = ;newRBT;newRBT; // the tree now has 2,23,-1,4 newRBT; // truenewRBT; // Node Structure with 2 as key value newRBT;newRBT; // truenewRBT; // false newRBT; // false,only true if the tree contain all values newRBT; // 23newRBT; // -1
API
The module exposes as main functions createBST
for binary search trees
creation and createRBT
for red black trees as well, regardless of the type of
tree created they have the same API.
Tree Creation
const createRBT = ; // create empty treeconst emptyTree = ; // init tree with some valuesconst treeWithValues = ; // values can be specified using arrayconst anotherTree = ; // array and values can be mixed on tree creationconst newTree = ;
Insertion
const newTree = ; newTree; // new value inserted newTree; // inserting more than one value newTree; // inserting more than one value using arrays newTree;newTree; // the 11 value is just inserted one time so the tree remain unchanged newTree; // here arrays and value can be missed as well newTree ; // insert call can be chained
Deletion
const newTree = ; newTree; // 3 value remove from the tree; newTree; // nothing occur the tree remain without any change newTree; // removing various elements in one single call newTree; // removing various elements using array as argument newTree ; // remove call can be chained
Searching
Sometimes when a search is did against the tree we receive as result a node and
to get the node value we must call the getValue
function to esxtract correctly
the value.Search in the tree can be done using the following functions
contain
,find
,the first return a boolean to tell if the tree contain he value
and the latter return the node with the value specified or undefined
if the
tree not contain the value, if any of this functions is called without any
argument then and error is throw.Examples:
const newTree = ; // asking if the value are in the tree newTree; // truenewTree; // false // using contain with several values only return true if all values are in the tree newTree; // true newTree; // false // retrieving nodes,check the use of getValue function newTree; // 3newTree; // undefined; //retrieving various nodes in one single call newTree; // [Node(1),Node(2),Node(3)] newTree; // [Node(1)] not present values are ignored newTree; // [Node(4),Node(5)] newTree; // [Node(1),Node(2),Node(3),Node(4)]
Max and Min values
We can know at any time the node with the max value in the tree or the node that contain the min value,if the node it not the desired result then we can know only the maximum or minimum value
const newRBT = ; newRBT; // Node 10newRBT; // Node 1 console; // print 10console; // print 1
Iteration
Every tree has a function iterator
that can be used to build a iterator from
the tree, when this new iterator is consumed the tree values are returned in
order
const newTree = ;const iterator = newTree; console; // {value: 1,done: false};console; // {value: 2,done: false};console; // {value: 3,done: false};console; // {value: undefined,done: true}; // if we call next again when the iterator has finished the same object is returnedconsole; // {value: undefined,done: true};
The tree also implement the especial Symbol.iterator
so it can be used with any of the standard consumer that used this symbol
const newTree = ; // the following for print the tree values in order forlet value of newTree console;
Array like methods
Reduce
We can use reduce over one tree created just like we do with standard arrays:
const newTree = ; const sum = acc + val; const product = acc * val; const treeTotal = newTree; const treeFactorial = newTree; console; // 15 console; // 120
Filter
The filter function is also available, this function is similar to the filter function of the arrays with the difference that a new tree is not created after the filtering process but the existing tree is modified:
const newTree = ; newTree; // after this the tree only has the value 6 newTree; // now the tree is empty