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

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

十三、字典树

访问 www.coding-time.cn 阅读原文动画效果,体验更佳。

 

在计算机科学中, 字典树(trie,中文又被称为”单词查找树“或 ”键树“), 也称为数字树,有时候也被称为基数树或前缀树(因为它们可以通过前缀搜索),它是一种搜索树--一种已排序的数据结构,通常用于存储动态集或键为字符串的关联数组。

 

与二叉搜索树不同, 树上没有节点存储与该节点关联的键; 相反,节点在树上的位置定义了与之关联的键。一个节点的全部后代节点都有一个与该节点关联的通用的字符串前缀, 与根节点关联的是空字符串。

 

值对于字典树中关联的节点来说,不是必需的,相反,值往往和相关的叶子相关,以及与一些键相关的内部节点相关。

 

有关字典树的空间优化示意,请参阅紧凑前缀树

image.png

 

Trie

1. TrieNode

import HashTable from '../hash-table/HashTable';
export default class TrieNode {
  /**
   * @param {string} character
   * @param {boolean} isCompleteWord
   */
  constructor(character, isCompleteWord = false) {
    this.character = character;
    this.isCompleteWord = isCompleteWord;
    this.children = new HashTable();
  }
  /**
   * @param {string} character
   * @return {TrieNode}
   */
  getChild(character) {
    return this.children.get(character);
  }
  /**
   * @param {string} character
   * @param {boolean} isCompleteWord
   * @return {TrieNode}
   */
  addChild(character, isCompleteWord = false) {
    if (!this.children.has(character)) {
      this.children.set(character, new TrieNode(character, isCompleteWord));
    }
    const childNode = this.children.get(character);
    // In cases similar to adding "car" after "carpet" we need to mark "r" character as complete.
    childNode.isCompleteWord = childNode.isCompleteWord || isCompleteWord;
    return childNode;
  }
  /**
   * @param {string} character
   * @return {TrieNode}
   */
  removeChild(character) {
    const childNode = this.getChild(character);
    // Delete childNode only if:
    // - childNode has NO children,
    // - childNode.isCompleteWord === false.
    if (
      childNode
      && !childNode.isCompleteWord
      && !childNode.hasChildren()
    ) {
      this.children.delete(character);
    }
    return this;
  }
  /**
   * @param {string} character
   * @return {boolean}
   */
  hasChild(character) {
    return this.children.has(character);
  }
  /**
   * Check whether current TrieNode has children or not.
   * @return {boolean}
   */
  hasChildren() {
    return this.children.getKeys().length !== 0;
  }
  /**
   * @return {string[]}
   */
  suggestChildren() {
    return [...this.children.getKeys()];
  }
  /**
   * @return {string}
   */
  toString() {
    let childrenAsString = this.suggestChildren().toString();
    childrenAsString = childrenAsString ? `:${childrenAsString}` : '';
    const isCompleteString = this.isCompleteWord ? '*' : '';
    return `${this.character}${isCompleteString}${childrenAsString}`;
  }}

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

相关文章
|
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)