核心思想

空间换时间,是一种用于快速减速的多叉树结构,利用字符串的公共前缀来降低时间

优缺点:

优点:查询效率高,减少字符比较

缺点:内存消耗较大

每次都会从头向下一直到字符串结尾

前缀树

1 单个字符串从前到后加到一棵多叉树上

2 每隔字符串都会有自己所在节点的两个属性path和end,path代表经过,end代表这个字符结尾

3 所有的插入操作都是一样的插入方式,有就复用没有就新开辟一条路

4 经过节点path += 1 ;每个字符串结尾 end += 1

5 可以快速查询前缀和完全匹配的数量

画图解释

如图所示 我们插入第一个字符串“abc”,从a开始,没有a就开辟一个a的路把经过的地方都标记path += 1

结果相同方式遍历b和c,最后c结果end +=1

相同的方式插入ab,每次都会从头开始第一个起始点path += 1,a存在a的path += 1,b也存在b的path +=1 ,b是结尾所以b的end +=1

实现

两种方式实现,第一种会用列表来储存,一种会用字典来储存

实现方式都一样,看会一种即可。

第一种

class Trie:

    def __init__(self):
"""
Initialize your data structure here.
"""
self.children = [None] * 26
self.path = 0
self.isEnd = 0 def insert(self, word: str) -> None:
"""
Inserts a word into the trie.
"""
node = self
node.path += 1
for ch in word:
offset = ord(ch) - ord('a')
# node.path += 1
if not node.children[offset]:
node.children[offset] = Trie()
node = node.children[offset]
node.path += 1 node.isEnd += 1 def startsWith(self, prefix: str) :
node = self
for ch in prefix:
offset = ord(ch) - ord('a')
if not node.children[offset]:
return None
node = node.children[offset] return node.path def search(self, prefix: str) :
node = self
for ch in prefix:
offset = ord(ch) - ord('a')
if not node.children[offset]:
return None
node = node.children[offset] return node.isEnd

第二种

class Trie:

    def __init__(self):
"""
Initialize your data structure here.
"""
self.children = dict()
self.path = 0
self.isEnd = 0 def insert(self, word: str) -> None:
"""
Inserts a word into the trie.
"""
node = self
node.path += 1
for ch in word:
offset = ord(ch) - ord('a')
if offset not in node.children:
node.children[offset] = Trie()
node = node.children[offset]
node.path += 1 node.isEnd += 1 def startsWith(self, prefix: str) :
node = self
for ch in prefix:
offset = ord(ch) - ord('a')
if offset not in node.children:
return None
node = node.children[offset] return node.path def search(self, prefix: str) :
node = self
for ch in prefix:
offset = ord(ch) - ord('a')
if offset not in node.children:
return None
node = node.children[offset] return node.isEnd

前缀树(Tire)—Python的更多相关文章

  1. python利用Trie(前缀树)实现搜索引擎中关键字输入提示(学习Hash Trie和Double-array Trie)

    python利用Trie(前缀树)实现搜索引擎中关键字输入提示(学习Hash Trie和Double-array Trie) 主要包括两部分内容:(1)利用python中的dict实现Trie:(2) ...

  2. 支持中文的基于词为基本粒度的前缀树(prefix trie)python实现

    Trie树,也叫字典树.前缀树.可用于"predictive text"和"autocompletion".亦可用于统计词频(边插入Trie树边更新或加入词频) ...

  3. 【python】Leetcode每日一题-前缀树(Trie)

    [python]Leetcode每日一题-前缀树(Trie) [题目描述] Trie(发音类似 "try")或者说 前缀树 是一种树形数据结构,用于高效地存储和检索字符串数据集中的 ...

  4. 【Todo】字符串相关的各种算法,以及用到的各种数据结构,包括前缀树后缀树等各种树

    另开一文分析字符串相关的各种算法,以及用到的各种数据结构,包括前缀树后缀树等各种树. 先来一个汇总, 算法: 本文中提到的字符串匹配算法有:KMP, BM, Horspool, Sunday, BF, ...

  5. Trie(前缀树/字典树)及其应用

    Trie,又经常叫前缀树,字典树等等.它有很多变种,如后缀树,Radix Tree/Trie,PATRICIA tree,以及bitwise版本的crit-bit tree.当然很多名字的意义其实有交 ...

  6. trie树(前缀树)详解——PHP代码实现

    trie树常用于搜索提示.如当输入一个网址,可以自动搜索出可能的选择.当没有完全匹配的搜索结果,可以返回前缀最相似的可能. 一.Tire树的基本性质 根节点不包含字符,除根节点外每一个节点都只包含一个 ...

  7. 4.14——208. 实现 Trie (前缀树)

    前缀树(字典树)是经典的数据结构,以下图所示: 本来处理每个节点的子节点集合需要用到set,但是因为输入规定了只有26个小写字母,可以直接用一个[26]的数组来存储. 关于ASCII代码: Java ...

  8. 【LeetCode】208. Implement Trie (Prefix Tree) 实现 Trie (前缀树)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,Trie, 前缀树,字典树,20 ...

  9. HDU1671——前缀树的一点感触

    题目http://acm.hdu.edu.cn/showproblem.php?pid=1671 题目本身不难,一棵前缀树OK,但是前两次提交都没有成功. 第一次Memory Limit Exceed ...

  10. [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)

    Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...

随机推荐

  1. 微信小程序分享好友,朋友圈

    <template> <view> <button open-type="share">发送给好友</button> </vi ...

  2. 如何优雅的备份MySQL数据?看这篇文章就够了

    大家好,我是一灯,今天一块学习一下如何优雅安全的备份MySQL数据? 1. 为什么要备份数据 先说一下为什么需要备份MySQL数据? 一句话总结就是:为了保证数据的安全性. 如果我们把数据只存储在一个 ...

  3. FluentValidation 验证(二):WebApi 中使用 注入服务

    比如你要验证用户的时候判断一下这个用户名称在数据库是否已经存在了,这时候FluentValidation 就需要注入查询数据库 只需要注入一下就可以了 public class Login3Reque ...

  4. springboot中使用mybatisplus自带插件实现分页

    springboot中使用mybatisplus自带插件实现分页 1.导入mybatisplus分页依赖 <dependency> <groupId>com.baomidou& ...

  5. 基于雪花算法的增强版ID生成器

    sequence 基于雪花算法的增强版ID生成器 解决了时间回拨的问题 无需手动指定workId, 微服务环境自适应 可配置化 快速开始 依赖引入 <dependency> <gro ...

  6. 通过 Github Action 实现定时推送天气预报

    偶然间,看到 GitHub Actions 教程:定时发送天气邮件 - 阮一峰的网络日志 这篇文章,没错,这个正好能打发自己的折腾之心,也能通过代码给生活引入一些变化. 还是在这里简单记录一下实现过程 ...

  7. HTML基础知识(3)浮动、塌陷问题

    1.浮动 1.1 代码 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> & ...

  8. 知识图谱-生物信息学-医学顶刊论文(Bioinformatics-2021)-MUFFIN:用于DDI预测的多尺度特征融合

    2.(2021.3.15)Bioinformatics-MUFFIN:用于DDI预测的多尺度特征融合 论文标题: MUFFIN: multi-scale feature fusion for drug ...

  9. ansible使用临时命令通过模块来执行任务

    使用临时命令通过模块来执行任务 一.查看系统上安装的所有模块 ansible-doc -l 查看ping模块帮助文档 ansible-doc ping 1.ansible模块 文件模块: copy:将 ...

  10. spring框架-jdbcTemplate

    首先 dao层: dao -bookdao(interface) -bookdaoimpl service层: bookService 实体类对象 entiry-book 测试类 Test-TestB ...