Skip to content
/ trie Public
forked from datastructures-js/trie

Trie data structure implementation in javascript

License

Notifications You must be signed in to change notification settings

ergeon/trie

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

95 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@datastructures-js/trie

build:? npm npm npm

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

Trie
Trie

Table of 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

Construction

const 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: any 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: any boolean O(k): k = length of string value
dictionary.has('hi'); // true
dictionary.has('sky'); // false

.find(value)

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

params return runtime
value: any TrieNode O(k): k = length of string value
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(value)

removes a word from the trie.

params return runtime
value: any string: the removed word O(k): k = length of string value
dictionary.remove('hi'); // hi

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

.forEach(cb)

traverses all words in the trie.

params runtime
cb: function O(n): n = number of nodes 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.

return runtime
array<string> O(n): n = number of nodes in the trie
console.log(dictionary.toArray());

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

.wordsCount()

gets the count of words in the trie.

return runtime
number O(1)
console.log(dictionary.wordsCount()); // 7

.nodesCount()

gets the count of nodes in the trie.

return runtime
number O(1)
console.log(dictionary.nodesCount()); // 23

.clear()

clears the trie.

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

Trie.fromArray(list)

converts an existing array of values into a trie.

params return runtime
list: array<any> boolean O(n * k)
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

new TrieNode(char)

params
char: string

.isRoot()

return
boolean

.getChar()

return
string

.getParent()

return
TrieNode

.setParent(trieNode)

params
trieNode: TrieNode

.isEndOfWord()

return
boolean

.setEndOfWord(isEndOfWord)

params
isEndOfWord: boolean

.getChild(char)

paramsreturn
char: stringTrieNode

.hasChild(char)

paramsreturn
char: stringboolean

.childrenCount()

return
number

Build

grunt build

License

The MIT License. Full License is here

About

Trie data structure implementation in javascript

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%