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

    Class TrieMap<V>

    A trie-based map implementation that extends AbstractTrieMap. Associates values with string keys (words) while preserving key lookup efficiency.

    const trieMap = new TrieMap();

    trieMap.set('apple', 1);
    trieMap.set('lemon', 2);

    console.log(trieMap.has('apple')); // true
    console.log([...trieMap]); // [['apple', 1], ['lemon', 2]]

    Type Parameters

    • V extends unknown = unknown

      The type of values stored in the trie-map.

    Hierarchy (View Summary)

    Index

    Constructors

    • Creates a new TrieMap instance.

      Type Parameters

      • V extends unknown = unknown

      Parameters

      • OptionalinitialWordValues:
            | [string, V][]
            | (readonly [string, V])[]
            | readonly [string, V][]
            | readonly (readonly [string, V])[]

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

      Returns TrieMap<V>

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

      // Empty trie-map
      const trieMap1 = new TrieMap();

      // Trie-map with initial entries
      const trieMap2 = new TrieMap([
      ['apple', 1],
      ['lemon', 2]
      ]);

    Accessors

    • get size(): number

      Gets the number of entries stored in the trie-map.

      Returns number

      The total count of word-value pairs.

      const trieMap = new TrieMap([
      ['apple', 1],
      ['lemon', 2]
      ]);
      console.log(trieMap.size); // 2

      trieMap.set('orange', 3);
      console.log(trieMap.size); // 3

    Methods

    • Makes the TrieMap iterable.

      Entries are yielded in their insertion order by default.

      Parameters

      • Optionalreversed: boolean = false

        Optional boolean to reverse iteration order.

      Returns Generator<[string, V], void, unknown>

      An iterator for the trie-map's entries.

      TypeError if reversed is not a boolean value.

      const trieMap = new TrieMap([
      ['apple', 1],
      ['lemon', 2],
      ['orange', 3],
      ]);

      // Forward iteration
      for (const [word, value] of trieMap) {
      console.log(`${word}: ${value}`);
      }
      // Output:
      // apple: 1
      // lemon: 2
      // orange: 2

      // Reverse iteration
      for (const [word, value] of trieMap[Symbol.iterator](true)) {
      console.log(`${word}: ${value}`);
      }
      // Output:
      // orange: 2
      // lemon: 2
      // apple: 1
    • Removes all entries from the trie-map, resetting it to an empty state.

      Returns void

      const trieMap = new TrieMap([
      ['apple', 1],
      ['lemon', 2]
      ]);
      console.log(trieMap.size); // 2

      trieMap.clear();
      console.log(trieMap.size); // 0
    • Removes an entry from the trie-map.

      Parameters

      • word: string

        The key of the entry to remove.

      Returns boolean

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

      TypeError if word is not a string or is empty.

      const trieMap = new TrieMap([
      ['apple', 1],
      ['lemon', 2]
      ]);

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

      console.log(trieMap.size); // 1
    • Returns an iterator of all entries in the trie-map.

      Entries are yielded in their insertion order by default.

      Parameters

      • Optionalreversed: boolean = false

        Optional boolean to reverse iteration order.

      Returns Generator<[string, V], void, unknown>

      An iterator of the trie-map's entries.

      TypeError if reversed is not a boolean value.

      const trieMap = new TrieMap([
      ['apple', 1],
      ['lemon', 2],
      ['orange', 3]
      ]);

      // Forward order
      for (const [word, value] of trieMap.entries()) {
      console.log(`${word}: ${value}`);
      }
      // Output:
      // apple: 1
      // lemon: 2
      // orange: 3

      // Reverse iteration
      for (const [word, value] of trieMap.entries(true)) {
      console.log(`${word}: ${value}`);
      }
      // Output:
      // orange: 3
      // lemon: 2
      // apple: 1
    • Finds all entries whose keys start with the given prefix.

      Parameters

      • prefix: string

        The prefix to search for.

      Returns [string, V][]

      An array of matching [word, value] pairs.

      TypeError if prefix is not a string or is empty.

      const trieMap = new TrieMap([
      ['car', 1],
      ['cat', 2],
      ['cart', 3]
      ]);

      console.log(trieMap.find('car'));
      // [['car', 1], ['cart', 3]]

      console.log(trieMap.find('ca'));
      // [['car', 1], ['cart', 3], ['cat', 2]]
    • Executes a callback for each entry in the trie-map.

      Parameters

      • callback: (value: V, word: string, trieMap: TrieMap<V>) => void
          • (value: V, word: string, trieMap: TrieMap<V>): void
          • Parameters

            • value: V

              The value of the current entry.

            • word: string

              The word of the current entry.

            • trieMap: TrieMap<V>

              The trie-map 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 trieMap = new TrieMap([
      ['apple', 1],
      ['lemon', 2]
      ]);

      const result = [];
      trieMap.forEach((value, word) => {
      result.push(`${word.toUpperCase()}:${value}`);
      });

      console.log(result);
      // ['APPLE:1', 'LEMON:2']
    • Retrieves the value associated with a given key.

      Parameters

      • word: string

        The key to look up.

      Returns undefined | V

      The associated value if found, undefined otherwise.

      TypeError if word is not a string or is empty.

      const trieMap = new TrieMap([
      ['apple', 1],
      ['lemon', 2]
      ]);

      console.log(trieMap.get('apple')); // 1
      console.log(trieMap.get('unknown')); // undefined
    • Checks if an entry with the specified key exists in the trie-map.

      Parameters

      • word: string

        The key to check.

      Returns boolean

      true if an entry with the key exists, false otherwise.

      TypeError if word is not a string or is empty.

      const trieMap = new TrieMap([['apple', 1]]);

      console.log(trieMap.has('apple')); // true
      console.log(trieMap.has('ap')); // false
      console.log(trieMap.has('lemon')); // false
    • Returns an iterator of all keys in the trie-map.

      Keys are yielded in their entries' insertion order.

      Parameters

      • Optionalreversed: boolean = false

        Optional boolean to reverse iteration order.

      Returns Generator<string, void, unknown>

      An iterator of the trie-map's keys.

      TypeError if reversed is not a boolean value.

      const trieMap = new TrieMap([
      ['apple', 1],
      ['lemon', 2],
      ['orange', 3]
      ]);

      // Forward order
      for (const key of trieMap.keys()) {
      console.log(key);
      }
      // Output:
      // apple
      // lemon
      // orange

      // Reverse order
      for (const key of trieMap.keys(true)) {
      console.log(key);
      }
      // Output:
      // orange
      // lemon
      // apple
    • Adds or updates an entry in the trie-map.

      If the key already exists, its value is updated but its position in iteration order remains unchanged.

      Parameters

      • word: string

        The key for the entry.

      • value: V

        The value to associate with the key.

      Returns void

      TypeError if word is not a string or is empty.

      const trieMap = new TrieMap();

      trieMap.set('apple', 1);
      trieMap.set('lemon', 2);

      trieMap.set('apple', 3); // Updating existing entry

      console.log(trieMap.get('apple')); // 3
    • Returns an iterator of all values in the trie-map.

      Values are yielded in their corresponding entries' insertion order.

      Parameters

      • Optionalreversed: boolean = false

        Optional boolean to reverse iteration order.

      Returns Generator<V, void, unknown>

      An iterator of the trie-map's values.

      TypeError if reversed is not a boolean value.

      const trieMap = new TrieMap([
      ['apple', 1],
      ['lemon', 2],
      ['orange', 3]
      ]);

      // Forward order
      for (const value of trieMap.values()) {
      console.log(value);
      }
      // Output:
      // 1
      // 2
      // 3

      // Reverse order
      for (const value of trieMap.values(true)) {
      console.log(value);
      }
      // Output:
      // 3
      // 2
      // 1