【高级数据结构】 Trie | 字典树 前缀树

数据结构可视化:https://www.cs.usfca.edu/~galles/visualization/Trie.html

字典树结构,每个结点有两个成员。

struct TrieNode {
	bool isWorldEnd;	// 标记当前字母是否为单词的结尾
    TrieNode* children[26];	// 使用26个位置标记字母
} 

其中,children数组的26个位置分别代表26个字母。children初始状态都为nullptr,如果children[i] 不为nullptr,则表示当前结点有字母(i+‘a’)。
在这里插入图片描述

如图所示,如果我们存储book,与bone,则字典树结构为:
在这里插入图片描述
如果我们还想存一个books,只需在原有book前缀的基础上在增加一个字母s即可。
在这里插入图片描述

代码:

class Trie {
private:
    vector<Trie*> children;	// 26个字母
    bool isEnd;				// 标记是否为单词结尾

	// 查找
    Trie* searchPrefix(string prefix) {
        Trie* node = this;
        for (char ch : prefix) {
            ch -= 'a';	// 以ascii充当下标
            if (node->children[ch] == nullptr) {
                return nullptr;	// 查找前缀不存在
            }
            node = node->children[ch];	// 继续向下查找
        }
        return node; // 找到符合要求的尾结点
    }

public:
    Trie() : children(26), 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;
    }
};

练习:

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我叫RT

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值