nodejs-bst
A fast and simple library for binary search tree data structures (BST) manipulation.
Instalation
To install the latest version using yarn:
yarn add nodejs-bst
Using npm:
npm install --save nodejs-bst
The Gist
Instantiation
// Import moduleconst BST = // Successconst emptyBst = // {}const singleBst = 7 // { 7: [] }const multiBst = 3 6 1 9 // { 3: [ { 1: [] }, { 6: [ {}, { 9: [] } ] } ] } // Failureconst errBst1 = 'foo' // Error: Node constructor argument must be a single or array of numbersconst errBst2 = 3 'foo' // Error: Node constructor argument must be a single or array of numbers
Append
// Import moduleconst BST = const bst = // Successbst // { 4: [] }bst // { 4: [ { 3: [] }, { 7, [] } ] } // Failurebst // Error: The addNumber method number argument must be a numberbst // Error: The addNumber method number argument must be a numberbst // Error: The addMultiple method numbers argument must be an array of numberbst // Error: The addMultiple method numbers argument must be an array of number
Removal
// Import moduleconst BST = const bst = 5 2 1 7 9 // { 5: [ { 2: [ { 1: [] }, {} ] }, { 7: [ {}, { 9: [] } ] } ] } // Successbst // { 5: [ { 1: [] }, { 7: [ {}, { 9: [] } ] } ] }bst // { 9: [ { 1: [] }, {} ] } // Failurebst // Error: The removeNumber method argument must be a numberbst // Error: The removeNumber method argument must be a numberbst // Error: The removeMultiple method argument must be an arraybst // Error: The removeMultiple method argument must be an array
Verify
// Import moduleconst BST = const bst = 4 // Successbst // Falsebst // Truebst // True // Failurebst // Error: The hasNumber function argument must be a numberbst // Error: The hasNumber function argument must be a number
Inorder path
// Import moduleconst BST = const bst = 215749 // Inorder pathbst // [ 2, 4, 7, 9, 15 ]
Preorder path
// Import moduleconst BST = const bst = 159851110211718252428 // Preorder pathbst // [ 15, 9, 8, 5, 11, 10, 21, 17, 18, 25, 24, 28 ]
Postorder path
// Import moduleconst BST = const bst = 159851110211718252428 // Postorder pathbst // [ 5, 8, 10, 11, 9, 18, 17, 24, 28, 25, 21, 15 ]