Options
All
  • Public
  • Public/Protected
  • All
Menu

Index

Type aliases

Trie

Trie: { children: Record<string, Trie> } & ({ endOfWord: false } | { endOfWord: true; word: string }) & ({ char: string; parent: Trie; root: false } | { root: true })

A Trie Node that contains

  • the complete word when endOfWord is true
  • a dict of children where the key is the next char in the word
  • a mark endOfWord to store the fact that we added a word up to this node
  • a circular reference to the parent node if root is false

Variables

emptyTrie

emptyTrie: Trie = ...

Empty Root Trie that can be used for empty initiation

Example

import { emptyTrie } from '@micham/trie-ts';
const trie = add("word", emptyTrie);

Functions

Const add

  • This function will return a Trie with the word added to it

    Example

    import { add, emptyTrie } from '@micham/trie-ts';
    const trie = add("word", emptyTrie);

    Parameters

    • word: string

      to add to the trie

    • trie: Trie

      to add the word to

    Returns Trie

    the trie with the word added

Const fromList

  • fromList(words: string[]): Trie
  • Initiate a Trie from a list of words

    Example

    import { fromList } from '@micham/trie-ts';
    const trie = fromList(["word", "hello"]);

    Parameters

    • words: string[]

      words used to initiate the Trie

    Returns Trie

    the initiated Trie

Const has

  • has(word: string, trie: Trie): boolean
  • Check if word is in trie

    Example

    import { add, has, of } from '@micham/trie-ts';
    const trie = of("hello", "world");

    has("hello", trie); // true
    has("hel", trie); // false
    has("hello world", trie); // false
    has("world", trie); // true
    has("wor", trie); // false

    Parameters

    • word: string

      word to check

    • trie: Trie

    Returns boolean

    true if there exists at least one prefix in the trie that is also a prefix of word

Const hasPrefix

  • hasPrefix(word: string, trie: Trie): boolean
  • Check if any of leaf in the trie is a prefix of the input

    Example

    import { add, hasPrefix, of } from '@micham/trie-ts';
    let trie = of("hello", "world");

    hasPrefix("hello", trie); // true
    hasPrefix("hello world", trie); // true
    hasPrefix("hell", trie); // false
    hasPrefix("all", trie); // false

    Parameters

    • word: string

      word to check

    • trie: Trie

    Returns boolean

    true if there exists at least one prefix in the trie that is also a prefix of word

Const of

  • of(...words: string[]): Trie
  • Initiate a Trie from a list of words

    Example

    import { of } from '@micham/trie-ts';
    const trie = of("word", "hello");

    Parameters

    • Rest ...words: string[]

      Word used to initiate the Trie

    Returns Trie

    the initiated Trie

Const remove

  • This function will try to remove a word from the trie. If this word was not present in the trie, the trie will not be updated

    Example

    import { remove, emptyTrie } from '@micham/trie-ts';
    const trie = remove("word", emptyTrie);

    Parameters

    • word: string

      to remove from the trie

    • trie: Trie

      to remove the word from

    Returns Trie

    the trie with the word removed

Const search

  • search(query: string, trie: Trie): string[]
  • Search for all the words in the trie that start with the given prefix

    Example

    import { add, search, of } from '@micham/trie-ts';
    const trie = of("hello", "hello world", "world");

    const result = search("hello", trie);
    // result == ["hello", "hello world"]

    Parameters

    • query: string

      prefix that will be used to search in trie

    • trie: Trie

      trie to scan

    Returns string[]

    the list of word in the trie that contain query as a prefix

Generated using TypeDoc