Trie

Trie

Creates a Trie (prefix tree) data structure. Implemented as a proper ES6 Class to fix prototyping documentation and encapsulate nodes.

Constructor

new Trie(wordsopt, strictopt)

Description:
  • Initializes the Trie class object.
Source:
Parameters:
Name Type Attributes Default Description
words Array.<string> <optional>
[] Initial words to add to the Trie.
strict boolean <optional>
true If true, removing a word cleans up unused nodes. If false, removal is faster but may leave unused nodes.

Classes

Trie

Methods

add(word) → {boolean}

Description:
  • Adds a word to the Trie.
Source:
Parameters:
Name Type Description
word string The word to add.
Returns:
True if the word was added (didn't exist before), false otherwise.
Type
boolean

get(begin) → {Array.<string>}

Description:
  • Returns all words in the Trie that start with the given prefix.
Source:
Parameters:
Name Type Description
begin string The prefix to search for.
Returns:
An array of words starting with the prefix.
Type
Array.<string>

getData() → {Array}

Description:
  • Returns the internal data structure of the Trie.
Source:
Returns:
The root node of the Trie.
Type
Array

has(word) → {boolean}

Description:
  • Checks if a word exists in the Trie.
Source:
Parameters:
Name Type Description
word string The word to check.
Returns:
True if the word exists, false otherwise.
Type
boolean

remove(word) → {boolean}

Description:
  • Removes a word from the Trie.
Source:
Parameters:
Name Type Description
word string The word to remove.
Returns:
True if the word was removed, false if it didn't exist.
Type
boolean