easy-trie

1.0.3 • Public • Published

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"

Dependencies (0)

    Dev Dependencies (14)

    Package Sidebar

    Install

    npm i easy-trie

    Weekly Downloads

    1

    Version

    1.0.3

    License

    MIT

    Unpacked Size

    53.8 kB

    Total Files

    4

    Last publish

    Collaborators

    • argha