【数据结构-前缀树】力扣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函数的时候,可以检查字典树中表示的字符串是否是一个单词。

相关推荐
Fanxt_Ja1 天前
【LeetCode】算法详解#15 ---环形链表II
数据结构·算法·leetcode·链表
今后1231 天前
【数据结构】二叉树的概念
数据结构·二叉树
元亓亓亓1 天前
LeetCode热题100--105. 从前序与中序遍历序列构造二叉树--中等
算法·leetcode·职场和发展
仙俊红1 天前
LeetCode每日一题,20250914
算法·leetcode·职场和发展
散1122 天前
01数据结构-01背包问题
数据结构
消失的旧时光-19432 天前
Kotlinx.serialization 使用讲解
android·数据结构·android jetpack
Gu_shiwww2 天前
数据结构8——双向链表
c语言·数据结构·python·链表·小白初步
苏小瀚2 天前
[数据结构] 排序
数据结构
_不会dp不改名_2 天前
leetcode_21 合并两个有序链表
算法·leetcode·链表
吃着火锅x唱着歌2 天前
LeetCode 3302.字典序最小的合法序列
leetcode