包详细信息

@datastructures-js/trie

datastructures-js13.5kMIT4.2.2

trie implementation in javascript

trie, trie es6, trie js

自述文件

@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

更新日志

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.

[Unreleased]

[4.2.2] - 2022-08-15

Fixed

  • add types to package.json

[4.2.1] - 2022-06-06

Fixed

  • readme.

[4.2.0] - 2021-12-01

Added

  • isLeaf() to TrieNode: leaf is a node that has no children.

[4.2.0] - 2021-12-01

Added

  • isLeaf() to TrieNode: leaf is a node that has no children.

Fixed

  • remove(word) two edge cases that were not covered:

    1. the case when removing a word that does not exist, count should not change.
    2. the case when another word overlaps with the word being deleted, it was removing all the word chars regardless if one char is an end of another word.

    Credit: 王悠悠 https://github.com/anson09

[4.1.1] - 2021-06-20

Fixed

  • index.d.ts

[4.1.0] - 2021-06-20

Added

  • typescript.

[4.0.1] - 2021-02-25

Fixed

  • README

[4.0.0] - 2021-02-23

Changed

  • .insert can be chained.
  • .remove now returns the removed word.
  • better handling for null & undefined.

Added

  • .fromArray static function to convert a list into a trie.

Fixed

  • jsdoc
  • README

[3.0.1] - 2020-04-18

Fixed

  • jsdoc
  • README

[3.0.0] - 2020-04-09

Changed

  • renamed .getWordsCount() & .getNodesCount() to .wordsCount() & .nodesCount().

Fixed

  • README
  • jsdoc

[2.0.0] - 2020-03-24

Changed

  • new implementation and interface