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

@datastructures-js/trie

v4.2.2

Published

trie implementation in javascript

Downloads

11,484

Readme

@datastructures-js/trie

npm npm npm

Trie implementation in javascript. Each Trie node holds one character of a word.

Contents

Install

npm install --save @datastructures-js/trie

require

const { Trie, TrieNode } = require('@datastructures-js/trie');

import

import { Trie, TrieNode } from '@datastructures-js/trie';

API

constructor

const dictionary = new Trie();

insert

insert the string form of value (value.toString()) into the trie.

Note: the empty string is not a default word in the trie. empty word can be added by explicitly calling .insert('')

dictionary
  .insert('hi')
  .insert('hit')
  .insert('hide')
  .insert('hello')
  .insert('sand')
  .insert('safe')
  .insert('noun')
  .insert('name');

has

checks if a word exists in the trie.

dictionary.has('hi'); // true
dictionary.has('sky'); // false

find

finds a word in the trie and returns the node of its last character.

const hi = dictionary.find('hi');
// hi.getChar() = 'i'
// hi.getParent().getChar() = 'h'

const safe = dictionary.find('safe');
// safe.getChar() = 'e'
// safe.getParent().getChar() = 'f'
// safe.getParent().getParent().getChar() = 'a'

const nothing = dictionary.find('nothing'); // null

remove

removes a word from the trie.

dictionary.remove('hi'); // hi

// none existing word
dictionary.remove('sky'); // null

forEach

traverses all words in the trie.

dictionary.forEach((word) => console.log(word));

/*
hit
hide
hello
sand
safe
noun
name
*/

toArray

converts the trie into an array of words.

console.log(dictionary.toArray());

// ['hit', 'hide', 'hello', 'sand', 'safe', 'noun', 'name']

wordsCount

gets the count of words in the trie.

console.log(dictionary.wordsCount()); // 7

nodesCount

gets the count of nodes in the trie.

console.log(dictionary.nodesCount()); // 23

clear

clears the trie.

dictionary.clear();
console.log(dictionary.wordsCount()); // 0
console.log(dictionary.nodesCount()); // 1

Trie.fromArray

converts an existing array of values into a trie.

const numbersTrie = Trie.fromArray([1, 32, 123, 21, 222, 132, 111, 312]);

console.log(numbersTrie.wordsCount()); // 8
console.log(numbersTrie.has('132')); // true
console.log(numbersTrie.has(123)); // true

TrieNode

isRoot()

checks if node is root.

isLeaf()

checks if has no children.

getChar()

gets the node's char.

getParent()

gets the node's parent node.

setParent(node: TrieNode)

sets the node's parent node.

isEndOfWord()

checks if node's char is last in a word.

setEndOfWord(endOfWord: boolean)

sets if node's char is last in a word.

getChild(char: string)

gets the node's child from a char.

hasChild(char: string)

checks if the node has a child from a char.

childrenCount()

gets the node's children count.

Build

grunt build

License

The MIT License. Full License is here