1.0.0 • Published 10 months ago

namastey-trie v1.0.0

Weekly downloads
-
License
ISC
Repository
-
Last release
10 months ago

namastey-trie

Brief Description

namastey-trie is a JavaScript package that implements the Trie data structure. A Trie is a tree-like data structure used to efficiently store and retrieve keys in a dataset of strings. It's particularly useful for searching words and prefixes.

Features

  • insert(word): Inserts a word into the Trie.
  • search(word): Searches for a word in the Trie. Returns true if the word exists, otherwise false.
  • startsWith(prefix): Checks if there is any word in the Trie that starts with the given prefix. Returns true if such a prefix exists, otherwise false.
  • delete(word): Deletes a word from the Trie if it exists.

Installation

To install the package globally, run the following command:

npm install -g namastey-trie

Examples

const Trie = require('namastey-trie');

const trie = new Trie();

// Insert words
trie.insert('apple');
trie.insert('app');
trie.insert('bat');
trie.insert('ball');

// Search words
console.log(trie.search('apple')); // Output: true
console.log(trie.search('app'));   // Output: true
console.log(trie.search('bat'));   // Output: true
console.log(trie.search('ball'));  // Output: true
console.log(trie.search('cat'));   // Output: false

// Check if a prefix exists
console.log(trie.startsWith('app')); // Output: true
console.log(trie.startsWith('ba'));  // Output: true
console.log(trie.startsWith('ca'));  // Output: false

// Delete a word
trie.delete('bat');
console.log(trie.search('bat')); // Output: false