Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
@datastructures-js/trie
Advanced tools
Changelog
[4.2.2] - 2022-08-15
Readme
Trie implementation in javascript. Each Trie node holds one character of a word.
npm install --save @datastructures-js/trie
const { Trie, TrieNode } = require('@datastructures-js/trie');
import { Trie, TrieNode } from '@datastructures-js/trie';
const dictionary = new Trie();
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');
checks if a word exists in the trie.
dictionary.has('hi'); // true
dictionary.has('sky'); // false
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
removes a word from the trie.
dictionary.remove('hi'); // hi
// none existing word
dictionary.remove('sky'); // null
traverses all words in the trie.
dictionary.forEach((word) => console.log(word));
/*
hit
hide
hello
sand
safe
noun
name
*/
converts the trie into an array of words.
console.log(dictionary.toArray());
// ['hit', 'hide', 'hello', 'sand', 'safe', 'noun', 'name']
gets the count of words in the trie.
console.log(dictionary.wordsCount()); // 7
gets the count of nodes in the trie.
console.log(dictionary.nodesCount()); // 23
clears the trie.
dictionary.clear();
console.log(dictionary.wordsCount()); // 0
console.log(dictionary.nodesCount()); // 1
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
checks if node is root.
checks if has no children.
gets the node's char.
gets the node's parent node.
sets the node's parent node.
checks if node's char is last in a word.
sets if node's char is last in a word.
gets the node's child from a char.
checks if the node has a child from a char.
gets the node's children count.
grunt build
The MIT License. Full License is here
FAQs
trie implementation in javascript
The npm package @datastructures-js/trie receives a total of 2,265 weekly downloads. As such, @datastructures-js/trie popularity was classified as popular.
We found that @datastructures-js/trie demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.