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

bst-js

v1.0.5

Published

Binary Search Tree in Javascript

Downloads

14

Readme

Binary Search Tree (in Javascript)

Build Status Coverage Status NPM

Usage

Install from NPM:

npm i bst-js
const Node = require("bst-js");
// create a tree with the root node value of 5
const tree = new Node(5);

The above will assume that the value of each node is a number. If you would like to store other forms of data, you will need to use your own comparator so that the tree can properly be sorted:

const tree = new Node("f", (x, y) => {
  if (x < y) return -1;
  if (x > y) return 1;
  return 0;
});

tree.insert("b").insert("p").insert("l").insert("z");
//      f
//    /  \
//  b     p
//      /  \
//    l     z

Methods

insert

Inserts a node with the given value.

const tree = new Node(9).insert(4);
//      9
//     /
//    4

or insert many values at once.

const tree = new Node(5);
const newValues = [10, 1, 8, 7];
tree.insert(...newValues); // or tree.insert(10, 1, 8, 7)
//        5
//      /   \
//    1      10
//          /
//         8
//        /
//       7

delete

Deletes a node with the given value.

const tree = new Node(5).insert(10).insert(1).insert(8).insert(7);
//        5
//      /   \
//    1      10
//          /
//         8
//        /
//       7
tree.delete(10);
//        5
//      /   \
//    1      8
//          /
//         7

or insert many values at once.

const tree = new Node(5).insert(10, 1, 8, 7);
const oldValues = [10, 1];
tree.delete(...oldValues); // or tree.delete(10, 1)
//        5
//      /   \
//    1      8
//          /
//         7

isBalanced

Returns true if the height of the left and right subtrees have a difference of 1 or less.

const tree = new Node(5);
tree.insert(10).insert(1).insert(0).insert(9).insert(53).insert(12);

tree.isBalanced();
// true

height

Returns the height of a given tree or subtree.

const tree = new Node(5).insert(10).insert(1).insert(8).insert(7);

tree.height();
// 3

depth

const tree = new Node(5).insert(10, 1, 8, 7);

tree.depth(8);
// 2

size

Returns the total number of nodes in a tree or subtree.

const tree = new Node(5).insert(10).insert(1).insert(8).insert(7);

tree.size();
// 5

contains

Returns true if a tree/subtree contains the passed value.

const tree = new Node(5).insert(10).insert(1).insert(8).insert(7);

tree.contains(10);
// true

depthFirst

Will execute a callback with each node's context bound to this over a depthFirst PREORDER traversal (by default). It also supports INORDER and POSTORDER traversals.

const tree = new Node(5).insert(10).insert(1).insert(8).insert(7);

tree.depthFirst(function() {
  console.log(this.value);
});
// 5, 10, 1, 8, 7

tree.depthFirst(function() {
  console.log(this.value);
}, "inorder");
// 1, 5, 7, 8, 10

breadthFirst

Will execute a callback with each node's context bound to this over a breadthFirst traversal.

const tree = new Node(5).insert(10).insert(1).insert(0).insert(8).insert(7);
//        5
//      /   \
//    1      10
//   /      /
//  0      8
//        /
//       7

tree.breadthFirst(function() {
  console.log(this.value);
});
// 5, 1, 10, 0, 8, 7