Different binary search tree implementations, including a self-balancing one (AVL)
Package name is
npm install binary-search-tree --savemake test
The API mainly provides 3 functions:
delete. If you do not create a unique-type binary search tree, you can store multiple pieces of data for the same key. Doing so with a unique-type BST will result in an error being thrown. Data is always returned as an array, and you can delete all data relating to a given key, or just one piece of data.
var BinarySearchTree = require'binary-search-tree'BinarySearchTreeAVLTree = require'binary-search-tree'AVLTree // Same API as BinarySearchTree// Creating a binary search treevar bst = ;// Inserting some databstinsert15 'some data for key 15';bstinsert12 'something else';bstinsert18 'hello';// You can insert multiple pieces of data for the same key// if your tree doesn't enforce a unique constraintbstinsert18 'world';// Retrieving data (always returned as an array of all data stored for this key)bstsearch15; // Equal to ['some data for key 15']bstsearch18; // Equal to ['hello', 'world']bstsearch1; // Equal to // Search between bounds with a MongoDB-like query// Data is returned in key order// Note the difference between $lt (less than) and $gte (less than OR EQUAL)bstbetweenBounds $lt: 18 $gte: 12; // Equal to ['something else', 'some data for key 15']// Deleting all the data relating to a keybstdelete15; // bst.search(15) will now give bstdelete18 'world'; // bst.search(18) will now give ['hello']
There are three optional parameters you can pass the BST constructor, allowing you to enforce a key-uniqueness constraint, use a custom function to compare keys and use a custom function to check whether values are equal. These parameters are all passed in an object.
var bst = unique: true ;bstinsert10 'hello';bstinsert10 'world'; // Will throw an error
// Custom key comparison function// It needs to return a negative number if a is less than b,// a positive number if a is greater than b// and 0 if they are equal// If none is provided, the default one can compare numbers, dates and strings// which are the most common usecasesif aage < bage return -1;if aage > bage return 1;return 0;// Now we can use objects with an 'age' property as keysvar bst = compareKeys: compareKeys ;bstinsert age: 23 'Mark';bstinsert age: 47 'Franck';
// Custom value equality checking function used when we try to just delete one piece of data// Returns true if a and b are considered the same, false otherwise// The default function is able to compare numbers and stringsreturn alength === blength;var bst = checkValueEquality: checkValueEquality ;bstinsert10 'hello';bstinsert10 'world';bstinsert10 'howdoyoudo';bstdelete10 'abcde';bstsearch10; // Returns ['howdoyoudo']
(The MIT License)
Copyright (c) 2013 Louis Chatriot <email@example.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.