Skip to content

Latest commit

 

History

History
49 lines (35 loc) · 1.17 KB

README.md

File metadata and controls

49 lines (35 loc) · 1.17 KB

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"