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

n-ary-huffman

v4.0.0

Published

An n-ary Huffman algorithm implementation.

Downloads

29

Readme

Overview Build Status

var huffman = require("n-ary-huffman")

var names = ["Alfa", "Bravo", "Charlie", "Delta", "Echo", "Foxtrot", "Golf"]
var items = names.map(function(name, index) {
  return {
    name: name,
    weight: index,
    codeWord: null
  }
})

var alphabet = "0123"

var tree = huffman.createTree(items, alphabet.length)
tree.assignCodeWords(alphabet, function(item, codeWord) {
  item.codeWord = codeWord
})

console.log(items)
// [
//   { name: 'Alfa',    weight: 0, codeWord: '13' },
//   { name: 'Bravo',   weight: 1, codeWord: '12' },
//   { name: 'Charlie', weight: 2, codeWord: '11' },
//   { name: 'Delta',   weight: 3, codeWord: '10' },
//   { name: 'Echo',    weight: 4, codeWord: '3'  },
//   { name: 'Foxtrot', weight: 5, codeWord: '2'  },
//   { name: 'Golf',    weight: 6, codeWord: '0'  }
// ]

Installation

npm install n-ary-huffman

var huffman = require('n-ary-huffman')

Usage

createTree(elements, n, [options])

elements is an array of objects. Each object is expected to have a weight property which represents the weight of the object, which is a number.

Returns a new BranchPoint—the root of an n-ary huffman tree, consisting of all the items in elements as well as BranchPoints. Each BranchPoint has n children (except the deepest one which might have fewer, depending on elements.length).

options:

  • sorted: Boolean. Defaults to false. In order to create the tree, elements needs to be sorted after the weights in descending order. Enable this option if elements already is sorted this way, to skip sorting again.

new BranchPoint(children, weight)

Instance properties:

  • children: children
  • weight: weight

See createTree(…) above for more information.

BranchPoint.prototype.assignCodeWords(alphabet, callback, prefix="")

Assign a code word to each element in the tree. The larger the weight of an element, the shorter the code word.

alphabet is a string or an array of characters to use for the code words. Make sure that the alphabet is at least as long the arity of the tree. Note: Don’t repeat characters, or you’ll get invalid code words.

callback(element, codeWord) is run for each object in the tree.

prefix (optional) will be added at the beginning of each code word.

License

The X11 “MIT” License.