@datastructures-js/trie
Trie implementation in javascript. Each Trie node holds one character of a word.
Trie
Contents
Installnpm install --save @datastructures-js/trie requireconst { Trie, TrieNode } = require ( '@datastructures-js/trie' ) ; importimport { Trie, TrieNode } from '@datastructures-js/trie' ; API constructorconst dictionary = new Trie ( ) ; .insert(value)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('')
params
return
runtime
value: { toString: () => string }
Trie
O(k): k = length of string value
dictionary
. insert ( 'hi' )
. insert ( 'hit' )
. insert ( 'hide' )
. insert ( 'hello' )
. insert ( 'sand' )
. insert ( 'safe' )
. insert ( 'noun' )
. insert ( 'name' ) ; .has(value)checks if a word exists in the trie.
params
return
runtime
value: { toString: () => string }
boolean
O(k): k = length of string value
dictionary. has ( 'hi' ) ;
dictionary. has ( 'sky' ) ; .find(value)finds a word in the trie and returns the node of its last character.
params
return
runtime
value: { toString: () => string }
TrieNode
O(k): k = length of string value
const hi = dictionary. find ( 'hi' ) ;
const safe = dictionary. find ( 'safe' ) ;
const nothing = dictionary. find ( 'nothing' ) ; .remove(value)removes a word from the trie.
params
return
runtime
value: { toString: () => string }
string: the removed word
O(k): k = length of string value
dictionary. remove ( 'hi' ) ;
dictionary. remove ( 'sky' ) ; .forEach(cb)traverses all words in the trie.
params
runtime
cb: (word: string) => void
O(n): n = number of nodes in the trie
dictionary. forEach ( ( word ) => console. log ( word) ) ;
.toArray()converts the trie into an array of words.
return
runtime
string[]
O(n): n = number of nodes in the trie
console. log ( dictionary. toArray ( ) ) ;
.wordsCount()gets the count of words in the trie.
return
runtime
number
O(1)
console. log ( dictionary. wordsCount ( ) ) ; .nodesCount()gets the count of nodes in the trie.
return
runtime
number
O(1)
console. log ( dictionary. nodesCount ( ) ) ; .clear()clears the trie.
dictionary. clear ( ) ;
console. log ( dictionary. wordsCount ( ) ) ;
console. log ( dictionary. nodesCount ( ) ) ; Trie.fromArray(list)converts an existing array of values into a trie.
params
return
runtime
list: string[]
boolean
O(n * k)
const numbersTrie = Trie. fromArray ( [ 1 , 32 , 123 , 21 , 222 , 132 , 111 , 312 ] ) ;
console. log ( numbersTrie. wordsCount ( ) ) ;
console. log ( numbersTrie. has ( '132' ) ) ;
console. log ( numbersTrie. has ( 123 ) ) ; TrieNode new TrieNode(char)
.isRoot()
.getChar()
.getParent()
.setParent(parent)
params return
parent: TrieNode TrieNode
.isEndOfWord()
.setEndOfWord(isEndOfWord)
params return
isEndOfWord: boolean
TrieNode
.getChild(char)
params return
char: string TrieNode
.hasChild(char)
params return
char: string boolean
.childrenCount()
Buildgrunt build
LicenseThe MIT License. Full License is here