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

bstree

v0.2.0-a

Published

Binary search tree implementation

Downloads

37

Readme

#bstree A binary search tree implementation for node and browser.


Install

npm install bstree

Example:

var BSTree = require('bstree');

var tree = new BSTree();

tree.add(2)
tree.add(3)
tree.add(1)

tree.top() // 3
tree.bottom() // 1

tree.removeTop() //3
tree.top() // 2

tree.remove(1) // 1
tree.removeBottom() // 2

Test

#required: sudo npm install -g grunt-cli
grunt test

#constructor(comparator)


Parameters:

  • comparator: A function that takes two arguments that correspond to tree node values. The return of this function is a boolean. If the value returned is true, the first argument is less than the second argument. If the value returned is false, the two arguments are equal or the second argument is greater than the first argument. Defaults to: function(a, b) { return a < b }

###Example:

var BSTree = require('bstree')

var tree = new BSTree(function(a, b) {
    return b > a
})

// or

var otherTree = new BSTree()

Algorithm complexity:

Constant

#add(element)


Adds the given element from the tree.

Example

var tree = new BSTree();
tree.add(5)

Algorithm complexity:

In the average case: logarithmic in #length

In the worst case: linear in #length

#bottom()


Returns the minimum value in the tree.

Example:

var tree = new BSTree()
;[7,3,10,5,1,6,9,8,2].forEach(tree.add, tree)

console.log(tree.bottom()) // 1

Algorithm complexity:

Constant in #length

#length


property that indicates the number of elements in tree.

Example:

var tree = new BSTree()
console.log(tree.length) // 0

tree.add('item')
console.log(tree.length) // 1

#top()


Returns the maximum value in the tree.

Example:

var tree = new BSTree()
;[7,3,10,5,1,6,9,8,2].forEach(tree.add, tree);

console.log(tree.top()) // 10

Algorithm complexity:

Constant in #length

#remove(element)


Removes the given element from the tree if it exists. Returns element if a removal occurred. Returns undefined if not.

Example

var tree = new BSTree()
;[7,3,10,5,1,6,9,8,2].forEach(tree.add, tree);

console.log(tree.remove(8)) // 8

var sorted_array = []
tree.forEach(function (element) {
  sorted_array.push(element)
})

console.log(sorted_array) // [1, 2, 3, 4, 5, 6, 7, 9, 10]

Algorithm complexity:

In the average case: logarithmic in #length

In the worst case: linear in #length

#removeBottom()


Removes and returns the bottom element of tree relative to comparator. If it does not exist (tree is empty), returns undefined

###Example:

var tree = BSTree()
;[7,3,10,5,1,6,9,8,2].forEach(tree.add, tree);

console.log(tree.removeBottom()) // 1

var sorted_array = []
tree.forEach(function (element) {
  sorted_array.push(element)
})

console.log(sorted_array) // [2, 3, 4, 5, 6, 7, 8, 9, 10]

Algorithm complexity:

In the average case: logarithmic in #length

In the worst case: linear in #length

#removeTop()


Removes and returns the top element of tree relative to comparator. If it does not exist (tree is empty), returns undefined

###Example:

var tree = BSTree()
;[7,3,10,5,1,6,9,8,2].forEach(tree.add, tree);

console.log(tree.removeTop()) // 10

var sorted_array = []
tree.forEach(function (element) {
  sorted_array.push(element)
})

console.log(sorted_array) // [1, 2, 3, 4, 5, 6, 7, 8, 9]

Algorithm complexity:

In the average case: logarithmic in #length

In the worst case: linear in #length