【数据结构-前缀树】力扣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

字典树

csharp 复制代码
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函数的时候,可以检查字典树中表示的字符串是否是一个单词。

相关推荐
এ᭄画画的北北2 小时前
力扣-234.回文链表
算法·leetcode·链表
wuqingshun3141594 小时前
蓝桥杯 11. 打印大X
数据结构·算法·职场和发展·蓝桥杯·深度优先
wuqingshun3141596 小时前
蓝桥杯 2. 确定字符串是否是另一个的排列
数据结构·c++·算法·职场和发展·蓝桥杯
长沙火山7 小时前
9.ArkUI List的介绍和使用
数据结构·windows·list
1白天的黑夜17 小时前
贪心算法-2208.将数组和减半的最小操作数-力扣(LeetCode)
c++·算法·leetcode·贪心算法
AAAA劝导tx8 小时前
List--链表
数据结构·c++·笔记·链表·list
格格Code8 小时前
八大排序——冒泡排序/归并排序
数据结构·算法·排序算法
Dream it possible!8 小时前
LeetCode 热题 100_最小路径和(92_64_中等_C++)(多维动态规划)
c++·leetcode·动态规划
vim怎么退出8 小时前
46.二叉树展开为链表
前端·leetcode
C语言魔术师8 小时前
62.不同路径
算法·leetcode·动态规划