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

utree

v0.1.6

Published

A rebalancing binary tree for JS

Downloads

6

Readme

btree

A rebalancing binary tree for JS

To install / use:

npm install btree-js

Basic Usage

var BinaryTree = require('btree-js');
var Tree = BinaryTree.Tree;
var Node = BinaryTree.Node;

var tree = new Tree();
tree.insert(new Node({ key: 10, text: 'blah' }));  // becomes tree's root
tree.insert(new Node({ key: 15, text: 'plop' }));  // tree.root.right.key: 15

Options

key

The binary tree defaults to key property unless a key is explicitly passed in.

var tree = new Tree({ key: 'id' });
tree.insert(1);
console.log(tree.root.id); // 1
unique

The binary tree defaults to allowing multiple identical keys. If unique: true is passed in as an option, it will throw an error when inserting a duplicate key.

var tree = new Tree({ unique: true });
tree.bulkInsert(1, 1);  // throws duplicate key violation

API

isEmpty

Returns whether or not the tree has nodes in it.

var tree = new Tree();
console.log(tree.isEmpty()); // true

tree.insert(new Node({ key: 6 });
console.log(tree.isEmpty()); // false

tree.delete(6);
console.log(tree.isEmpty()); // true

delete

Allows you to pass in either a node or a key to delete that node from the tree. If the tree becomes unbalanced as a result, it will rebalance itself.

tree.bulkInsert(5, 3, 10, 8);
tree.print();

         5

    3              10

              8

tree.delete(3);
tree.print();

         8

    5         10

min

Returns the minimum key in the tree. If a node is provided, it will find the minimum key in that subtree.

tree.bulkInsert(50, 25, 90, 180, 40);
console.log(tree.min().key); // 25

tree.delete(tree.min());
console.log(tree.min().key); // 40

max

Returns the maximum key in the tree. If a node is provided, it will find the maximum key in that subtree.

tree.bulkInsert(50, 25, 90, 180, 40);
console.log(tree.max().key); // 180

tree.delete(tree.max());
console.log(tree.max().key); // 90

search

Returns a node with the given key, if found. Otherwise, returns null.

tree.insert(new Node({ key: 10, text: 'blah' }));
tree.insert(new Node({ key: 15, text: 'plop' }));

var node = tree.search(15);
console.log(node.text);       // plop
console.log(node.parent.key); // 10

findPaths

Returns a list of paths from the root to the leaf (array of array of nodes).

tree.bulkInsert(2, 1, 3);
var paths = tree.findPaths();
paths.forEach(function path) {
  var keys = path.map(function (node) { return node.key; });
  console.log(keys.join(' '));
});

// 2 1
// 2 3

height

Returns the height of the tree. With n nodes, the height of the tree will be approximately log(n).

var tree = new Tree();
tree.bulkInsert(10, 5, 15, 2, 7, 12, 18);
tree.height();  // 3

invert

Inverts the tree by taking all the node pointers and flipping them.

tree.bulkInsert(50, 25, 75, 60, 90);
tree.print();

         50

    25              75

              60         90

tree.invert();
tree.print();

         90

    25              50

              60         75

print

To print a text-view of the tree,

tree.print();

                     7

         3                           13

   1            5             9                   17

      2      4      6      8      11         15       18

                                         14               19

inOrderTraversal

An iterator that returns the nodes via:

  1. Return node from left tree (recursive)
  2. Return self
  3. Return node from right tree (recursive)
tree.inOrderTraversal(function (node) {
  console.log(node.value);
});

preOrderTraversal

An iterator that returns the nodes via:

  1. Return self
  2. Return node from left tree (recursive)
  3. Return node from right tree (recursive)
tree.preOrderTraversal(function (node) {
  console.log(node.value);
});

postOrderTraversal

An iterator that returns the nodes via:

  1. Return node from left tree (recursive)
  2. Return node from right tree (recursive)
  3. Return self
tree.postOrderTraversal(function (node) {
  console.log(node.value);
});

Written by JT Bowler, 2016.