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

@truck/binary-search-tree

v1.0.1

Published

A JavaScript implementation of the Binary Search Tree data structure

Downloads

1

Readme

Build Status Coverage Status semantic-release

Binary Search Tree

A JavaScript Binary Search Tree data structure. This Binary Search Tree is not balanced, which makes inserting sorted values a lot slower than unsorted values.

Installation

Install @truck/binary-search-tree via npm:

$ npm install --save @truck/binary-search-tree

Methods

constructor(rootValue: any)

Builds a new Binary Search Tree with rootValue as the value at the root of the tree.

delete(value: any): boolean

O(n). Deletes the given value from the Binary Search Tree. The default === comparator is used to check whether values are equal.

delete(comparator: (value: any) => number): boolean

O(n). Deletes the value that matches comparator from the Binary Search Tree. The comparator must return either 1 to delete to the right, -1 to delete to the left, or 0 if the given value must be deleted.

insert(value: any): void

O(n). Inserts the given value into the Binary Search Tree. The default === comparator is used to check whether values are equal.

insert(value: any, comparator: (value: any) => number): void

O(n). Inserts the given value into the Binary Search Tree. The comparator must return either 1 to insert to the right or -1 to insert to the left.

search(value: any): BinarySearchTree

O(n). Searches for the given value in the Binary Search Tree. Returns a sub-Binary Search Tree if value is found, returns undefined otherwise.

search(comparator: (value: any) => number): BinarySearchTree

O(n). Searches the Binary Search Tree for a value using the given comparator. The comparator must return either 1 to search to the right, -1 to search to the left, or 0 when the value matches. Returns a sub-Binary Search Tree if value is found, returns undefined otherwise.

traverseBreadthFirst(callback: (binarySearchTree: BinarySearchTree) => void): void

O(n). Traverses the Binary Search Tree using Breadth-First traversal algorithm. Calls callback with the current BinarySearchTree on each iteration.

traverseDepthFirstInOrder(callback: (binarySearchTree: BinarySearchTree) => void): void

O(n). Traverses the Binary Search Tree using Depth-First (In-Order) traversal algorithm. Calls callback with the current BinarySearchTree on each iteration.

traverseDepthFirstPostOrder(callback: (binarySearchTree: BinarySearchTree) => void): void

O(n). Traverses the Binary Search Tree using Depth-First (Post-Order) traversal algorithm. Calls callback with the current BinarySearchTree on each iteration.

traverseDepthFirstPreOrder(callback: (binarySearchTree: BinarySearchTree) => void): void

O(n). Traverses the Binary Search Tree using Depth-First (Pre-Order) traversal algorithm. Calls callback with the current BinarySearchTree on each iteration.

Properties

.left: BinarySearchTree

A sub-Binary Search Tree that is the left child of the current. The value of .left will be smaller than the current .value. undefined if none exists.

.maximum: any

O(n). The maximum value in the Binary Search Tree.

.minimum: any

O(n). The minimum value in the Binary Search Tree.

.right: BinarySearchTree

A sub-Binary Search Tree that is the right child of the current. The value of .right will be larger than the current .value. undefined if none exists.

.value: any

The value inserted into the Binary Search Tree.

Examples

A Binary Search Tree is a standard class which can be instantiated with the new keyword:

import BinarySearchTree from '@truck/binary-search-tree';

const binarySearchTree = new BinarySearchTree(10);
// insert some values
binarySearchTree.insert(5);
binarySearchTree.insert(3);
binarySearchTree.insert(7);
binarySearchTree.insert(15);
binarySearchTree.insert(13);
binarySearchTree.insert(17);
// delete some values
binarySearchTree.delete(7); // true
binarySearchTree.delete(15); // true
binarySearchTree.delete(1000); // false
// The tree after insertion/deletion
//      10
//      / \
//    5    17
//   /    /
//  3    13

// search the tree for a value
const subTree = binarySearchTree.search(17); // tree of 17 with a left child of 13
// traverse the tree
const newArray = [];
binarySearchTree.traverseBreadthFirst(tree => newArray.push(tree.value)); // [10, 5, 7, 3, 13]

Testing

Use the following command to run all the tests described below together:

$ docker-compose run --rm app npm test

Commit messages

Commit messages are linted through the use of husky and @commitlint/cli using the @commitlint/config-conventional commit convention.

Please read through the AngularJS Git Commit Message Conventions to get a better understanding of how commit messages are formatted.

After doing an npm install the required git hooks wil be added automatically and commit messages will be linted automatically.

Linting

Linting is done using eslint using the eslint-config-airbnb-base configuration with very few alterations, all of which can be seen in the .eslintrc file in the root of this repository.

Linting can be run in isolation through the command:

$ docker-compose run --rm app npm run test:lint

Auditing

Auditing of dependencies is done through the npm audit command-line tool.

Auditing can be run in isolation through the command:

$ docker-compose run --rm app npm run test:vulnerabilities

Unit testing

Unit testing is done with jest. The test file for each file to be tested is to be placed alongside the file in testing and marked with the .test.js extension.

Unit testing can be run in isolation through the command:

$ docker-compose run --rm app npm run test:scripts

Contributing

Contributions are always welcome, just submit a PR to get the conversation going. Please make sure all tests pass before submitting a PR.

Releases

The moment a PR is merged into the master branch semantic-release will kick-off a new release, thus the importance of clear commit messages.