当前位置: 首页 > article >正文

【数据结构-前缀树】力扣208. 实现 Trie (前缀树)

Trie(发音类似 “try”)或者说 前缀树 是一种树形数据结构,用于高效地存储和检索字符串数据集中的键。这一数据结构有相当多的应用情景,例如自动补全和拼写检查。

请你实现 Trie 类:

Trie() 初始化前缀树对象。
void insert(String word) 向前缀树中插入字符串 word 。
boolean search(String word) 如果字符串 word 在前缀树中,返回 true(即,在检索之前已经插入);否则,返回 false 。
boolean startsWith(String prefix) 如果之前已经插入的字符串 word 的前缀之一为 prefix ,返回 true ;否则,返回 false 。

示例:
输入
[“Trie”, “insert”, “search”, “search”, “startsWith”, “insert”, “search”]
[[], [“apple”], [“apple”], [“app”], [“app”], [“app”], [“app”]]
输出
[null, null, true, false, true, null, true]

解释
Trie trie = new Trie();
trie.insert(“apple”);
trie.search(“apple”); // 返回 True
trie.search(“app”); // 返回 False
trie.startsWith(“app”); // 返回 True
trie.insert(“app”);
trie.search(“app”); // 返回 True

在这里插入图片描述

字典树

class Trie {
private:
    vector<Trie*> children;
    bool isEnd;

    Trie* searchPrefix(string prefix){
        Trie* node = this;
        for(char ch : prefix){
            ch -= 'a';
            if(node->children[ch] == nullptr){
                return nullptr;
            }
            node = node->children[ch];
        }
        return node;
    }
public:
    Trie(): children(26, nullptr), isEnd(false) {
    }
    
    void insert(string word) {
        Trie* node = this;
        for(char ch : word){
            ch -= 'a';
            if(node->children[ch] == nullptr){
                node->children[ch] = new Trie();
            }
            node = node->children[ch];
        }
        node->isEnd = true;
    }
    
    bool search(string word) {
        Trie* node = this->searchPrefix(word);
        return node != nullptr && node->isEnd;
    }
    
    bool startsWith(string prefix) {
        return this->searchPrefix(prefix) != nullptr;
    }
};

时间复杂度:初始化为 O(1),其余操作为 O(∣S∣),其中 ∣S∣ 是每次插入或查询的字符串的长度。
空间复杂度:O(∣T∣⋅Σ),其中 ∣T∣ 为所有插入字符串的长度之和,Σ 为字符集的大小,本题 Σ=26。

我们要构造前缀树,首先我们构造一个vector容器children,每个元素是一个Trie,并且我们初始化children(26, nullptr),说明一个节点node最多可以有26个子节点,也就是26个不同字符。

字典数的核心是searchPrefix函数,也就是查找这个字典树的前缀是否包含word单词。首先我们将字符串prefix拆解成一个个字符,先通过-'a’将它换算成ascii码,然后从字典树的根节点进行查找,根节点不储存信息,所以我们检查node->children[ch]是否存在,如果该节点存在的话我们就将node移到该节点,然后我们继续遍历prefix的下一个字符,如果prefix所有字符都能依次在字典树沿着某条路径找到,那么他就会返回prefix的最后一个节点的指针。

这么做的目的就是比如我们在调用startsWith函数的时候,我们只要检查返回的node是不是一个空指针就能判断之前已经插入的字符串 word 的前缀之一有没有 prefix。

接下来我们查看insert函数,我们一样把要插入的单词拆成一个个字符,如果我们沿着字典树某个路径找不到某个字符,那么就会new一个node->children[ch],并且将node移到新开辟的节点,在插入完最后一个字符的时候,会进行一个标记node->isEnd = true;,代表该节点是一个单词的结尾,这样做的目的是调用search函数的时候,可以检查字典树中表示的字符串是否是一个单词。


http://www.kler.cn/a/528523.html

相关文章:

  • 告别复杂,拥抱简洁:用plusDays(7)代替plus(7, ChronoUnit.DAYS)
  • DIFY源码解析
  • gentoo 中更改$PS1
  • 精准化糖尿病知识问答(LLM+机器学习预测模型)
  • 低代码系统-产品架构案例介绍、轻流(九)
  • mysql教程
  • Baklib揭示内容中台实施最佳实践的策略与实战经验
  • 好用的翻译工具
  • 基于VMware的ubuntu与vscode建立ssh连接
  • java练习(1)
  • 网络编程套接字(中)
  • 力扣动态规划-17【算法学习day.111】
  • 深入解析“legit”的地道用法——从俚语到正式表达:Sam Altman用来形容DeepSeek: legit invigorating(真的令人振奋)
  • 计算机网络之物理层通信基础(编码与调制)
  • java练习(2)
  • 初识ArkTS语言
  • Java小白入门教程:泛型,泛型类型参数<T> <K> <V> <E>(必须Get的知识)
  • WSL2中安装的ubuntu开启与关闭探讨
  • 使用Pygame制作“吃豆人”游戏
  • Spring Boot 实例解析:HelloWorld 探究
  • 【LeetCode 刷题】二叉树-二叉搜索树的属性
  • 我用Ai学Android Jetpack Compose之Box
  • 3 Flink 运行架构
  • 关于系统重构实践的一些思考与总结
  • 攻防世界_PHP2
  • Web-3.0(Solidity)ERC-20