AVL Tree
Javascript impl of a traditional avl tree
Supports all basic operations and custom sort comparison functions.
- insert
tree;
- delete
tree;
- search
var element = tree; returns the element if it exists
- forEach
var sum = 0; tree;
- getMin
var min = tree;
- getMax
var max = tree;
- deleteMax
var max = tree;
- getElementsAtDepth
var nodesAtZero = tree;
returns an array of elements at depth 0 (in this case, the root);
- Storing objects in the tree by using a custom ordering function, you can easily store objects in the tree, ordered by some property.
// Create a custom sorting function that will order people by age. var { if personAage < personBage return -1 else if personAage > personBage return 1; return 0; }; // pass in the custom sorting function the the tree's constructor var personTree = personSortingFunction; // create some test people to add to the tree var person1 = age: 1 ; var person2 = age: 2 ; var person3 = age: 3 ; var person4 = age: 4 ; // add the people to the tree in any order personTree; personTree; personTree; personTree; personTree;
output 1 2 3 4
Tests are run using mocha in the test directory.
npm install -g mocha
npm test
If you wish to help improve the speed there are some benchmarks in the speedTest directory.
npm run speedTest
does not support duplicate values at this time.