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

simple-bst

v1.4.1

Published

A simple binary search tree

Downloads

7

Readme

:deciduous_tree: simple-bst

An implementation of a Binary Search Tree (BST)

Table of contents

Installation

Install simple-bst using npm.

npm install simple-bst

Or install using yarn.

yarn add simple-bst

Usage

import { BST, Node } from 'simple-bst';

// also works
// import BST from 'simple-bst';

const tree = new BST();

// Adding and removing nodes
tree.add(5).add(15).add(2).add(new Node(20)).remove(15);

// Shortex syntax
tree.add(8, 72, 95).remove(72, 95);

console.log(tree.size); // 4

// Checking if a node is present
if (tree.has(2)) {
  console.log('The number 2 is present in the tree');
} else {
  console.log('The number 2 is not present in the tree');
}

// Getting the max value
const max = tree.max(); // 20

// Getting the min value
const min = tree.min(); // 2

API

add

Adds a node in the tree.

tree.add(5); // First node becomes the root
tree.add(10);

// Shorter syntax
tree.add(8, 11);

console.log(tree.size); // 4

remove

Removes a node from the tree.

tree.add(5, 10, 20, 2, 4, 18);

tree.remove(10, 18);

console.log(tree.size); // 4

find

Searches for a given value in the tree and returns the node.

tree.add(5, 10);

const node = tree.find(10);

console.log(node.data); // 10

has

Indicates whether a value exists. Optionally takes a root node from which to check the value.

tree.add(5, 10, 15, 9, 7);

console.log(tree.has(5)); // true
console.log(tree.has(14)); // false

const root = tree.find(15);

console.log(tree.has(5, root)); // false

min

Finds the minimum value in the tree. Optionally takes a root node from which to find the value.

tree.add(5, 2, 10, 15, 9, 7);

console.log(tree.min()); // 2

const root = tree.find(10);

console.log(tree.min(root)); // 7

max

Finds the maximum value in the tree. Optionally takes a root node from which to find the value.

tree.add(5, 2, 3, 1, 10, 15, 9, 7);

console.log(tree.max()); // 15

const root = tree.find(3);

console.log(tree.max(root)); // 2

height

Gets the height of the tree.

tree.add(8, 3, 1, 6, 4, 7, 10, 14, 13);

console.log(tree.height()); // 3

// you can also use depth, which is an alias for the height function
console.log(tree.depth()); // 3

Traversing the tree

All traversal functions are generator functions, which return a Generator.

traverse

Traverses the tree using the specified order.

tree.add(4).add(2).add(1).add(3).add(6).add(5).add(7);

for (const node of tree.traverse('inorder')) {
  console.log(node.data);
}

inorder

Traverses the tree from the left subtree to the root, then to the right subtree.

tree.add(4).add(2).add(1).add(3).add(6).add(5).add(7);

const inorder = Array.from(tree.inorder());

// [ 1, 2, 3, 4, 5, 6, 7 ]
console.log(inorder.map(node => node.data));

preorder

Traverses the tree from the root to the left subtree, then to the right subtree.

tree.add(4).add(2).add(1).add(3).add(6).add(5).add(7);

const preorder = Array.from(tree.preorder());

// [ 4, 2, 1, 3, 6, 5, 7 ]
console.log(preorder.map(node => node.data));

postorder

Traverses the tree from the left subtree to the right subtree, then to the root.

tree.add(4).add(2).add(1).add(3).add(6).add(5).add(7);

const postorder = Array.from(tree.postorder());

// [ 1, 3, 2, 5, 7, 6, 4 ]
console.log(postorder.map(node => node.data));

Utility functions

isLeaf

Indicates whether a node has no sub-children (i.e is a leaf).

tree.add(8, 3, 1, 10, 15, 14);

console.log(tree.isLeaf(8)); // false
console.log(tree.isLeaf(3)); // false
console.log(tree.isLeaf(10)); // false

console.log(tree.isLeaf(1)); // true
console.log(tree.isLeaf(14); // true

isBalanced

Indicates whether the tree is balanced.

const balancedTree = new BST([ 3, 1, 4, 2 ]);
const unbalancedTree = new BST([ 1, 2, 3, 4, 5, 6 ]);

console.log(balancedTree.isBalanced()); // true
console.log(unbalancedTree.isBalanced()); // false

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.