npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-avl-tree

v0.1.1

Published

AVL-tree for Node.js and the browser.

Downloads

3

Readme

NPM Version Build Status Built with Grunt

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 = new AVLTree();
t.is_empty();  // true
t.root;  // null

Insert

var t = new AVLTree();
var k = 1;  // Key is used to determine order
var v = 'hello';  // User data to be associated with key
var n = t.insert(k, v);  // returns newly created AVLNode
n.key;  // k
n.value;  // v
t.root;  // n

Search

var t = new AVLTree();
var a = t.insert(1, 'hello');
var b = t.insert(2, 'world');
t.search(1);  // returns a
t.search(3);  // returns b, the last leaf visited while searching for 3

Remove

var t = new AVLTree();
var a = t.insert(1, 'hello');
var b = t.insert(2, 'world');

a.is_valid();  // true
t.remove(1);  // Removal by key, returns 'hello'
a.is_valid();  // false, node has been removed

b.is_valid();  // true
b.remove();  // Removal by node, returns 'world'
b.is_valid();  // 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 default = function(a, b) { return a - b; }  // This is the default comparator
var reversed = function(a, b) { return b - a; }

var t1 = new AVLTree();  // == new AVLTree(default)
t1.insert(1); t1.insert(2); t1.insert(3);

/*
 * Tree structure:
 *    2
 *   / \
 *  1   3
 */

var t2 = new AVLTree(reversed);
t2.insert(1); t2.insert(2); t2.insert(3);

/*
 * Tree structure:
 *    2
 *   / \
 *  3   1
 */

Example: Sorting

var t = new AVLTree();
t.insert(2, 'world');
t.insert(6, 'day');
t.insert(5, 'beautiful');
t.insert(3, 'what');
t.insert(1, 'hello');
t.insert(4, 'a');

result = []
while (!t.is_empty()) {
    result.push(t.search(0).remove());
}

result.join()  // 'hello world what a beautiful day'

License

This software is licensed under the MIT License. See the LICENSE file for more information.