Installation
Install via npm: npm install node-avl-tree
The dist/
directory contains both a normal (avl.js
) as well as a minified version of the library (avl.min.js
).
Import either into Node.js using require("avl")
or directly include in the browser using <script src="avl.min.js"></script>
Usage
Create
var t = ;t; // truetroot; // null
Insert
var t = ;var k = 1; // Key is used to determine ordervar v = 'hello'; // User data to be associated with keyvar n = t; // returns newly created AVLNodenkey; // knvalue; // vtroot; // n
Search
var t = ;var a = t;var b = t;t; // returns at; // returns b, the last leaf visited while searching for 3
Remove
var t = ;var a = t;var b = t; a; // truet; // Removal by key, returns 'hello'a; // false, node has been removed b; // trueb; // Removal by node, returns 'world'b; // false, node has been removed
Custom Comparator
The default tree will assume its keys are numbers and imposes the natural ordering on its nodes based on them. You may supply your own compare function during instantiation to customize this behavior.
var { return a - b; } // This is the default comparatorvar { return b - a; } var t1 = ; // == new AVLTree(default)t1; t1; t1; /* * Tree structure: * 2 * / \ * 1 3 */ var t2 = reversed;t2; t2; t2; /* * Tree structure: * 2 * / \ * 3 1 */
Example: Sorting
var t = ;t;t;t;t;t;t; result = while !t result; result // 'hello world what a beautiful day'
License
This software is licensed under the MIT License. See the LICENSE file for more information.