1.0.3 • Published 2 years ago

easy-trie v1.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

easy-trie

An easy-to-use implementation of the Trie data structure. This can be used for searching for words to autocomplete and also for spell-checking.

Installation

npm install --save easy-trie

Usage

Add words to dictionary

Words can be added to the dictionary one at a time, or an array of words can be added at the same time.

Adding a single word

const Trie = require("easy-trie");
const trie = new Trie();
trie.addWord("word");

Adding an array of words

const Trie = require("easy-trie");
const trie = new Trie();
trie.addWords(["hello", "world", "today", "home"]);

Searching for words

trie.search("");        // results = ["hello", "world", "today", "home"]

trie.search("h");       // results = ["hello", "home"]

trie.search("ho");      // results = ["home"]

trie.search("world");   // results = ["world"]

trie.search("invalid"); // results = []

Longest common prefix

Find the longest common prefix between all the words. If no common prefix exists, return an empty string.

const trie = new Trie();
trie.addWords(["hello", "he", "her"]);
trie.longestCommonPrefix(); // results = "he"
1.0.2

2 years ago

1.0.1

2 years ago

1.0.3

2 years ago

1.0.0

3 years ago

0.0.4

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago