带你读《图解算法小抄》十三、字典树(2)

简介: 带你读《图解算法小抄》十三、字典树(2)

带你读《图解算法小抄》十三、字典树(1)https://developer.aliyun.com/article/1348164?groupCode=tech_library

Trie

import TrieNode from './TrieNode';
// Character that we will use for trie tree root.const HEAD_CHARACTER = '*';
export default class Trie {
  constructor() {
    this.head = new TrieNode(HEAD_CHARACTER);
  }
  /**
   * @param {string} word
   * @return {Trie}
   */
  addWord(word) {
    const characters = Array.from(word);
    let currentNode = this.head;
    for (let charIndex = 0; charIndex < characters.length; charIndex += 1) {
      const isComplete = charIndex === characters.length - 1;
      currentNode = currentNode.addChild(characters[charIndex], isComplete);
    }
    return this;
  }
  /**
   * @param {string} word
   * @return {Trie}
   */
  deleteWord(word) {
    const depthFirstDelete = (currentNode, charIndex = 0) => {
      if (charIndex >= word.length) {
        // Return if we're trying to delete the character that is out of word's scope.
        return;
      }
      const character = word[charIndex];
      const nextNode = currentNode.getChild(character);
      if (nextNode == null) {
        // Return if we're trying to delete a word that has not been added to the Trie.
        return;
      }
      // Go deeper.
      depthFirstDelete(nextNode, charIndex + 1);
      // Since we're going to delete a word let's un-mark its last character isCompleteWord flag.
      if (charIndex === (word.length - 1)) {
        nextNode.isCompleteWord = false;
      }
      // childNode is deleted only if:
      // - childNode has NO children
      // - childNode.isCompleteWord === false
      currentNode.removeChild(character);
    };
    // Start depth-first deletion from the head node.
    depthFirstDelete(this.head);
    return this;
  }
  /**
   * @param {string} word
   * @return {string[]}
   */
  suggestNextCharacters(word) {
    const lastCharacter = this.getLastCharacterNode(word);
    if (!lastCharacter) {
      return null;
    }
    return lastCharacter.suggestChildren();
  }
  /**
   * Check if complete word exists in Trie.
   *
   * @param {string} word
   * @return {boolean}
   */
  doesWordExist(word) {
    const lastCharacter = this.getLastCharacterNode(word);
    return !!lastCharacter && lastCharacter.isCompleteWord;
  }
  /**
   * @param {string} word
   * @return {TrieNode}
   */
  getLastCharacterNode(word) {
    const characters = Array.from(word);
    let currentNode = this.head;
    for (let charIndex = 0; charIndex < characters.length; charIndex += 1) {
      if (!currentNode.hasChild(characters[charIndex])) {
        return null;
      }
      currentNode = currentNode.getChild(characters[charIndex]);
    }
    return currentNode;
  }}

 

2. 参考

Wikipedia

YouTube

相关文章
|
7月前
|
存储 缓存 算法
带你读《图解算法小抄》一、链表(1)
带你读《图解算法小抄》一、链表(1)
带你读《图解算法小抄》一、链表(1)
|
6天前
|
算法 测试技术 C#
【字典树】【KMP】【C++算法】3045统计前后缀下标对 II
【字典树】【KMP】【C++算法】3045统计前后缀下标对 II
|
6天前
|
算法 测试技术 C++
【动态规划】 【字典树】C++算法:472 连接词
【动态规划】 【字典树】C++算法:472 连接词
|
6天前
|
算法 测试技术 C#
【map】【滑动窗口】【字典树】C++算法:最长合法子字符串的长度
【map】【滑动窗口】【字典树】C++算法:最长合法子字符串的长度
|
6天前
|
算法 搜索推荐 Java
「程序员必须掌握的算法」字典树「上篇」
「程序员必须掌握的算法」字典树「上篇」
|
5月前
|
算法 测试技术 C#
C++字典树算法:找出强数对的最大异或值 II
C++字典树算法:找出强数对的最大异或值 II
|
6月前
|
人工智能 算法 架构师
再现神作!字节算法小抄官方整版,已助1000+应届生拿到25w+年薪
2023年经济下行趋势明显,程序员出路在哪儿? 今年,毕业人数将达到1158万,导致很多公司招聘非常谨慎、要求也变得非常更高。
再现神作!字节算法小抄官方整版,已助1000+应届生拿到25w+年薪
|
6月前
|
SQL 算法 架构师
字节算法中了80%!靠着这份GitHub上的算法小抄,成功斩获Offer
前言 最近,GitHub上的算法小抄又火了!已经有不少人靠它手撕算法题,拿下了字节、腾讯等大厂offer
|
7月前
|
缓存 算法 前端开发
带你读《图解算法小抄》序言(1)
带你读《图解算法小抄》序言(1)
|
7月前
|
算法 前端开发
带你读《图解算法小抄》序言(2)
带你读《图解算法小抄》序言(2)