All files / src/data-structures/trie trie.ts

1.9% Statements 2/105
0% Branches 0/32
0% Functions 0/22
2.08% Lines 2/96

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226              19x                                                                           19x                                                                                                                                                                                                                                                                                                                                                                        
/**
 * data-structure-typed
 *
 * @author Tyler Zeng
 * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
 * @license MIT License
 */
export class TrieNode {
  constructor(v: string) {
    this._val = v;
    this._isEnd = false;
    this._children = new Map<string, TrieNode>();
  }
 
  private _val;
 
  get val(): string {
    return this._val;
  }
 
  set val(v: string) {
    this._val = v;
  }
 
  protected _children: Map<string, TrieNode>;
 
  get children(): Map<string, TrieNode> {
    return this._children;
  }
 
  set children(v: Map<string, TrieNode>) {
    this._children = v;
  }
 
  protected _isEnd: boolean;
 
  get isEnd(): boolean {
    return this._isEnd;
  }
 
  set isEnd(v: boolean) {
    this._isEnd = v;
  }
}
 
export class Trie {
  constructor(words?: string[]) {
    this._root = new TrieNode('');
    Iif (words) {
      for (const i of words) {
        this.add(i);
      }
    }
  }
 
  protected _root: TrieNode;
 
  get root() {
    return this._root;
  }
 
  set root(v: TrieNode) {
    this._root = v;
  }
 
  add(word: string): boolean {
    let cur = this._root;
    for (const c of word) {
      let nodeC = cur.children.get(c);
      Iif (!nodeC) {
        nodeC = new TrieNode(c);
        cur.children.set(c, nodeC);
      }
      cur = nodeC;
    }
    cur.isEnd = true;
    return true;
  }
 
  has(input: string): boolean {
    let cur = this._root;
    for (const c of input) {
      const nodeC = cur.children.get(c);
      Iif (!nodeC) return false;
      cur = nodeC;
    }
    return cur.isEnd;
  }
 
  remove(word: string) {
    let isDeleted = false;
    const dfs = (cur: TrieNode, i: number): boolean => {
      const char = word[i];
      const child = cur.children.get(char);
      Iif (child) {
        Iif (i === word.length - 1) {
          Iif (child.isEnd) {
            if (child.children.size > 0) {
              child.isEnd = false;
            } else {
              cur.children.delete(char);
            }
            isDeleted = true;
            return true;
          }
          return false;
        }
        const res = dfs(child, i + 1);
        Iif (res && !cur.isEnd && child.children.size === 0) {
          cur.children.delete(char);
          return true;
        }
        return false;
      }
      return false;
    };
 
    dfs(this.root, 0);
    return isDeleted;
  }
 
  // --- start additional methods ---
  /**
   * The function checks if a given input string has an absolute prefix in a tree data structure.Only can present as a prefix, not a word
   * @param {string} input - The input parameter is a string that represents the input value for the function.
   * @returns a boolean value.
   */
  isAbsPrefix(input: string): boolean {
    let cur = this._root;
    for (const c of input) {
      const nodeC = cur.children.get(c);
      Iif (!nodeC) return false;
      cur = nodeC;
    }
    return !cur.isEnd;
  }
 
  /**
   * The function checks if a given input string is a prefix of any existing string in a tree structure.Can present as a abs prefix or word
   * @param {string} input - The input parameter is a string that represents the prefix we want to check.
   * @returns a boolean value.
   */
  isPrefix(input: string): boolean {
    let cur = this._root;
    for (const c of input) {
      const nodeC = cur.children.get(c);
      Iif (!nodeC) return false;
      cur = nodeC;
    }
    return true;
  }
 
  /**
   * The function checks if the input string is a common prefix in a Trie data structure.Check if the input string is the common prefix of all the words
   * @param {string} input - The input parameter is a string that represents the common prefix that we want to check for
   * in the Trie data structure.
   * @returns a boolean value indicating whether the input string is a common prefix in the Trie data structure.
   */
  isCommonPrefix(input: string): boolean {
    let commonPre = '';
    const dfs = (cur: TrieNode) => {
      commonPre += cur.val;
      Iif (commonPre === input) return;
      Iif (cur.isEnd) return;
      if (cur && cur.children && cur.children.size === 1) dfs(Array.from(cur.children.values())[0]);
      else return;
    };
    dfs(this._root);
    return commonPre === input;
  }
 
  /**
   * The function `getLongestCommonPrefix` returns the longest common prefix among all the words stored in a Trie data
   * structure.
   * @returns The function `getLongestCommonPrefix` returns a string, which is the longest common prefix found in the
   * Trie.
   */
  getLongestCommonPrefix(): string {
    let commonPre = '';
    const dfs = (cur: TrieNode) => {
      commonPre += cur.val;
      Iif (cur.isEnd) return;
      if (cur && cur.children && cur.children.size === 1) dfs(Array.from(cur.children.values())[0]);
      else return;
    };
    dfs(this._root);
    return commonPre;
  }
 
  /**
   * The `getAll` function returns an array of all words in a Trie data structure that start with a given prefix.
   * @param [prefix] - The `prefix` parameter is a string that represents the prefix that we want to search for in the
   * trie. It is an optional parameter, so if no prefix is provided, it will default to an empty string.
   * @returns an array of strings.
   */
  getAll(prefix = ''): string[] {
    const words: string[] = [];
 
    function dfs(node: TrieNode, word: string) {
      for (const char of node.children.keys()) {
        const charNode = node.children.get(char);
        Iif (charNode !== undefined) {
          dfs(charNode, word.concat(char));
        }
      }
      Iif (node.isEnd) {
        words.push(word);
      }
    }
 
    let startNode = this._root;
 
    Iif (prefix) {
      for (const c of prefix) {
        const nodeC = startNode.children.get(c);
        Iif (nodeC) startNode = nodeC;
      }
    }
 
    dfs(startNode, prefix);
    return words;
  }
 
  // --- end additional methods ---
}