• 7-前缀/字典树


    重点知识

    字典树也称作前缀树或者Trie树,是一种哈希树的变种。

    典型应用:用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计。

    优点:利用字符串的公共前缀来减少查询时间,最大限度地减少无谓的字符串比较,查询效率比哈希树高。

    字典树的特点
    根节点不包含字符,除根节点外每一个节点都只包含一个字符;
    从根节点到某一节点,路径上经过的字符连接起来,为该节点对应的字符串;
    每个节点的所有子节点包含的字符都不相同。

    字典树数据结构的实现方法
    字典树要实现以下两种基本功能:

    • 插入单词
    • 搜索单词
    • 搜索前缀

    练习

    leetCode 820. 单词的压缩编码

    class MyPrefixTree:
        def __init__(self):
            self.trees = {}
    
        def search(self, word: str):
            my_tree = self.trees
            for s in word:
                if s not in my_tree:
                    return False
                my_tree = my_tree[s]
            if my_tree.get("is_word"):
                return True
            return False
    
        def insert(self, word: str):
            my_tree = self.trees
            for s in word:
                if s in my_tree:
                    my_tree = my_tree[s]
                    my_tree["is_word"] = False
                else:
                    my_tree[s] = {"is_word": False}
                    my_tree = my_tree[s]
            if len(my_tree) == 1:
                my_tree["is_word"] = True
    
        def search_prefix(self, prefix: str):
            my_tree = self.trees
            step = 0
            for s in prefix:
                if s not in my_tree:
                    return False, step
                my_tree = my_tree[s]
                step += 1
            if not my_tree.get("is_word"):
                step = 0
            return True, step
    
    
    class Solution:
        def minimumLengthEncoding(self, words: list[str]) -> int:
            my_prefix_tree = MyPrefixTree()
            for word in set(words):
                reversed_w = word[-1:-len(word)-1:-1]
                my_prefix_tree.insert(reversed_w)
            ret = 0
            for word in set(words):
                reversed_w = word[-1:-len(word) - 1:-1]
                _, step = my_prefix_tree.search_prefix(reversed_w)
                if step:
                    ret += step
                    ret += 1
            return ret
    
    
    • 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
  • 相关阅读:
    SAP MM模块业务流程
    设计模式-Factory
    基于SpringBoot+Druid实现多数据源:注解+编程式
    Twins: Revisiting the Design of Spatial Attention inVision Transformers
    lodash防抖节流
    『现学现忘』Docker基础 — 34、DockerFile文件详解
    【网络篇】如何给虚拟机添加网卡,设置固定ip
    4、SpringBoot_Mybatis、Druid、Juint整合
    STL- 函数对象
    C语言入门(八)一维数组
  • 原文地址:https://blog.csdn.net/luofeng_/article/details/127798124