Prefix Tries - v1.0.1
    Preparing search index...

    Class CompressedTrie

    A memory-optimized trie (radix tree) implementation that extends AbstractTrie.

    const trie = new CompressedTrie();

    trie.add('apple');
    trie.add('lemon');

    console.log(trie.has('apple')); // true
    console.log([...trie]); // ['apple', 'lemon']

    Hierarchy (View Summary)

    Index

    Constructors

    • Creates a new CompressedTrie instance.

      Parameters

      • OptionalinitialWords: string[] | readonly string[]

        Optional array of [word, value] pairs to initialize the compressed-trie with.

      Returns CompressedTrie

      TypeError if initialWords is defined and contains an invalid word (empty string or non-string value).

      // Empty trie
      const trie1 = new CompressedTrie();

      // Trie with initial words
      const trie2 = new CompressedTrie(['apple', 'lemon']);

    Accessors

    • get size(): number

      Gets the number of words stored in the compressed-trie.

      Returns number

      The total count of words.

      const trie = new CompressedTrie(['apple', 'lemon']);
      console.log(trie.size); // 2

      trie.add('orange');
      console.log(trie.size); // 3

    Methods

    • Makes the CompressedTrie iterable.

      Words are yielded in their insertion order by default.

      Parameters

      • Optionalreversed: boolean = false

        Optional boolean to reverse iteration order.

      Returns Generator<string, void, unknown>

      An iterator for the compressed-trie's entries.

      TypeError if reversed is not a boolean value.

      const trie = new CompressedTrie(['apple', 'lemon', 'orange']);

      // Forward iteration
      for (const word of trie) {
      console.log(word);
      }
      // Output:
      // apple
      // lemon
      // orange

      // Reverse iteration
      for (const word of trie[Symbol.iterator](true)) {
      console.log(word);
      }
      // Output:
      // orange
      // lemon
      // apple
    • Adds a word to the compressed-trie. If the word already exists, it will not create duplicates and the original insertion order is preserved.

      Parameters

      • word: string

        The word to add.

      Returns void

      TypeError if word is not a string or is empty.

      const trie = new CompressedTrie();

      trie.add('apple');
      trie.add('lemon');

      trie.add('apple'); // Won't add duplicate

      console.log(trie.size); // 2
    • Removes all words from the trie, resetting it to an empty state.

      Returns void

      const trie = new CompressedTrie(['apple', 'lemon']);
      console.log(trie.size); // 2

      trie.clear();
      console.log(trie.size); // 0
    • Removes a word from the compressed-trie.

      Parameters

      • word: string

        The word to remove.

      Returns boolean

      true if the word was found and removed, false if not found.

      TypeError if word is not a string or is empty.

      const trie = new CompressedTrie(['apple', 'lemon']);

      console.log(trie.delete('apple')); // true
      console.log(trie.delete('unknown')); // false

      console.log(trie.size); // 1
    • Returns an iterator of all words in the compressed-trie.

      Words are yielded in their insertion order by default.

      Parameters

      • Optionalreversed: boolean = false

        Optional boolean to reverse iteration order.

      Returns Generator<string, void, unknown>

      An iterator of the compressed-trie's entries.

      TypeError if reversed is not a boolean value.

      const trie = new CompressedTrie(['apple', 'lemon', 'orange']);

      // Forward order
      for (const word of trie.entries()) {
      console.log(word);
      }
      // Output:
      // apple
      // lemon
      // orange

      // Reverse order
      for (const word of trie.entries(true)) {
      console.log(word);
      }
      // Output:
      // orange
      // lemon
      // apple
    • Finds all words that start with the given prefix.

      Parameters

      • prefix: string

        The prefix to search for.

      Returns string[]

      An array of matching words.

      TypeError if prefix is not a string or is empty.

      const trie = new CompressedTrie(['car', 'cat', 'cart']);

      console.log(trie.find('car'));
      // ['car', 'cart']

      console.log(trie.find('ca'));
      // ['car', 'cart', 'cat']
    • Executes a callback function for each entry in the compressed-trie.

      Parameters

      • callback: (word: string, trie: CompressedTrie) => void
          • (word: string, trie: CompressedTrie): void
          • Parameters

            • word: string

              The word of the current entry.

            • trie: CompressedTrie

              The compressed-trie instance being traversed.

            Returns void

      • OptionalthisArg: any

        Optional value to use as this in the callback.

      Returns void

      TypeError if callback is not a function.

      const trie = new CompressedTrie(['apple', 'lemon']);

      const result = [];
      trie.forEach((word) => {
      result.push(word.toUpperCase());
      });

      console.log(result);
      // ['APPLE', 'LEMON']
    • Checks if a word exists in the compressed-trie.

      Parameters

      • word: string

        The word to check.

      Returns boolean

      true if the exact word exists, false otherwise.

      TypeError if word is not a string or is empty.

      const trie = new CompressedTrie(['apple']);

      console.log(trie.has('apple')); // true
      console.log(trie.has('ap')); // false
      console.log(trie.has('lemon')); // false