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

basic-binary-search-tree

v1.0.4

Published

Basic binary search tree

Downloads

37

Readme

Binary Search Tree (BST)

This package provides a TypeScript implementation of a Binary Search Tree (BST). A BST is a data structure where each node has at most two children referred to as left and right. The tree maintains the property that for any given node, its left children contain values less than the node's value, and its right children contain values greater than the node's value. This makes searching, insertion, and deletion operations efficient, typically with time complexity of O(log n).

Features

This package supports the following operations:

  1. add: Inserts a new value into the tree.
  2. remove: Removes a value from the tree, adjusting the structure accordingly.
  3. has: Checks if the value exists and returns true if exists or false if it doesn't.
  4. toArray: Applies inorder traverse to the tree and return an array of values.
  5. getMin: Retrieves the smallest value in the tree.
  6. getMax: Retrieves the largest value in the tree.

Installation

To install the package from npm, use the following command:

npm install basic-binary-search-tree

Usage

Importing the BinarySearchTree

import { BinarySearchTree } from 'basic-binary-search-tree';

Creating a Tree and Adding Values

const bst = new BinarySearchTree();

bst.add(10);
bst.add(5);
bst.add(20);

console.log(bst.toArray()); // [5, 10, 20]

Checking if Value Exists

console.log(bst.has(10)); // true
console.log(bst.has(15)); // false

Removing a Value

bst.remove(10);
console.log(bst.toArray()); // [5, 20]

Finding Minimum and Maximum Values

console.log(bst.getMin()); // 5
console.log(bst.getMax()); // 20

Public API

add(value: number)

  • Description: Inserts a value into the BST. If the value already exists, it will not be added.
  • Parameters: value (number) – The value to add to the tree.
  • Returns: void.

toArray()

  • Description: Returns an array of all values in the tree, in ascending order.
  • Returns: number[] – The sorted array of tree values.

remove(value: number)

  • Description: Removes a value from the tree, if it exists.
  • Parameters: value (number) – The value to remove from the tree.
  • Returns: void.

has(value: number)

  • Description: Searches for a value in the tree.
  • Parameters: value (number) – The value to search for.
  • Returns: booleantrue if the value exists, otherwise false.

getMin()

  • Description: Retrieves the minimum value in the tree.
  • Returns: number | undefined – The minimum value, or undefined if the tree is empty.

getMax()

  • Description: Retrieves the maximum value in the tree.
  • Returns: number | undefined – The maximum value, or undefined if the tree is empty.