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

@timcash10/bst.js

v1.0.1

Published

![](public/logo.png) # bst.js Supports numbers ```sh npm i @timcash10/bst.js ``` ## Includes standard methods of a BST 1. `insert` 1. `find` 1. `traverseInOrder` 1. `successor` 1. `remove`

Downloads

9

Readme

bst.js

Supports numbers

npm i @timcash10/bst.js

Includes standard methods of a BST

  1. insert
  2. find
  3. traverseInOrder
  4. successor
  5. remove

Additional methods

  1. deepest nodes in the BST and depths
  2. BST can be constructed from an array of numbers

Setup

npm install 
npm test

API

// IMPORT
import { BinarySearchTree, Operators, Node } from "./bst.js";

// CREATE
let bst = new BinarySearchTree([10, -10, 5, -5, 42, 360, 0]);

// INSERT
Operators.insert(bst, 100);
Operators.insert(bst, -100);

// TRAVERSE
let ordered = Operators.traverseInOrder(bst);
console.log(ordered); // [-100, -10, -5, 0, 5, 10, 42, 100, 360]

// FIND
const nodeToRemove = Operators.find(bst, 42);

// REMOVE
Operators.remove(bst, nodeToRemove);
let ordered = Operators.traverseInOrder(bst);
console.log(ordered); // [-100, -10, -5, 0, 5, 10, 100, 360]

// DEEPEST
bst = new BinarySearchTree([12, 11, 90, 82, 7, 9]);
let deepest = Operators.deepest(bst);
t.alike(deepest, {keys:[9], depth:3}, "deepest node single");

bst = new BinarySearchTree([26, 82, 16, 92, 33]);
deepest = Operators.deepest(bst);
t.alike(deepest, {keys:[33, 92], depth:2}, "deepest node multiple");

Coverage & Benchmark

Coverage is reported at the end of each test run

To turn off he benchmark use skip on the test

Binary search tree (BST)

also called an ordered or sorted binary tree

  1. A rooted binary tree data structure
  2. With the key of each internal node being greater than all the keys in the respective node's left subtree and less than the ones in its right subtree.

The time complexity of operations on the binary search tree is directly proportional to the height of the tree.

Binary search trees allow binary search for fast

  1. Lookup (get, search, find, contains)
  2. Addition (put, insert, add, append)
  3. Removal (delete, remove, pop, extract)

Since the nodes in a BST are laid out so that each comparison skips about half of the remaining tree, the lookup performance is proportional to that of binary logarithm. BSTs were devised in the 1960s for the problem of efficient storage of labeled data and are attributed to Conway Berners-Lee and David Wheeler.

The performance of a binary search tree is dependent on the order of insertion of the nodes into the tree since arbitrary insertions may lead to degeneracy; several variations of the binary search tree can be built with guaranteed worst-case performance.

BSTs with guaranteed worst-case complexities perform better than an unsorted array, which would require linear search time.

The complexity analysis of BST shows that, on average, the insert, delete and search takes

  1. O(log n) for n nodes
  2. In the worst case they degrade to that of a singly linked list: O(n)

To address the boundless increase of the tree height with arbitrary insertions and deletions, self-balancing variants of BSTs are introduced to bound the worst lookup complexity to that of the binary logarithm.

  1. AVL trees were the first self-balancing binary search trees, invented in 1962 by Georgy Adelson-Velsky and Evgenii Landis.

Remove

Search

Searching Searching in a binary search tree for a specific key can be programmed recursively or iteratively.

Traversal

A BST can be traversed through three basic algorithms:

  1. inorder
  2. preorder
  3. postorder tree walks. Inorder tree walk: Nodes from the left subtree get visited first, followed by the root node and right subtree. Preorder tree walk: The root node gets visited first, followed by left and right subtrees.

Implement abstract data types

  1. dynamic sets
  2. lookup tables
  3. priority queues
  4. and used in sorting algorithms such as tree sort.

Time complexity

The time complexities of a binary search tree increases boundlessly with the tree height if the nodes are inserted in an arbitrary order, therefore

  1. Self-balancing binary search trees were introduced to bound the height of the tree to O(log_n)

  2. Various height-balanced binary search trees were introduced to confine the tree height, such as AVL trees, Treaps, and red–black trees.