在学习字符串算法时,Trie 树(字典树) 是一个绕不开的数据结构。它非常适合解决以下问题:
-
快速查找单词是否存在
-
判断某个前缀是否存在
-
支持大量单词的高效存储
本文将通过分步骤讲解,带你理解 Trie 的工作机制,并展示如何一步步实现它。
示例代码均为 C++17,可直接编译运行。
1. Trie 的基本结构
Trie 是一棵多叉树,每个节点代表一个字符。
从根节点到某个叶子节点的路径,就对应一个单词。
例如:插入 cat 和 car 后,前缀 ca 会被复用:
(root)
|
c
|
a
/ \
t r
这样,公共前缀不会被重复存储,大大节省了空间。
2. 节点定义
每个节点需要两个核心信息:
-
isEnd:标记该节点是否为某个单词的结尾
-
children:存储子节点,最多 52 个(a-z + A-Z)
C++ 实现如下:
// Trie树节点结构体定义
struct Node {
// 标记位:表示当前节点是否为一个单词的结尾
// - true: 从根节点到当前节点的路径构成了一个完整的单词
// - false: 当前节点只是某个单词的前缀部分
bool isEnd = false;
// 子节点指针数组:用于存储当前节点的所有可能子节点
// - 数组大小为52,支持26个小写字母(a-z)和26个大写字母(A-Z)
// - 使用智能指针unique_ptr自动管理内存,避免内存泄漏
// - 数组索引映射规则:
// [0-25] -> 'a'到'z'
// [26-51] -> 'A'到'Z'
// - 如果某个位置的指针为nullptr,表示对应的字母不存在
array<unique_ptr<Node>, 52> children;
};
这里使用 unique_ptr 来管理子节点,避免手动释放内存。
3. 插入单词
插入时,从根节点开始,逐字符处理:
-
如果对应的子节点不存在,就新建一个
-
最后把
isEnd标记为true
void insert(const string& word) {
Node* node = root.get();
for (char ch : word) {
int idx = getIndex(ch);
if (idx == -1) continue; // 跳过非字母
if (!node->children[idx]) node->children[idx] = make_unique<Node>();
node = node->children[idx].get();
}
node->isEnd = true; // 单词结束
}
4. 查找单词
查找过程与插入类似:
-
逐字符向下走,如果中途缺少节点就返回
false -
最后必须保证
isEnd == true
bool findWord(const string& word) {
Node* node = root.get();
for (char ch : word) {
int idx = getIndex(ch);
if (idx == -1 || !node->children[idx]) return false;
node = node->children[idx].get();
}
return node->isEnd;
}
5. 前缀判断
只要能顺利走完前缀路径,就说明该前缀存在。
bool hasPrefix(const string& prefix) {
Node* node = root.get();
for (char ch : prefix) {
int idx = getIndex(ch);
if (idx == -1 || !node->children[idx]) return false;
node = node->children[idx].get();
}
return true;
}
6. 删除单词
删除是 Trie 的一个难点:
-
如果只把
isEnd置为false,可能会导致内存泄漏 -
正确做法是 递归回溯,在没有孩子节点时释放
// 递归删除单词的辅助函数
bool removeHelper(Node* node, const string& word, int depth) {
if (!node) return false; // 当前节点为空,直接返回删除失败
// 递归基:已到达单词末尾
if (depth == word.size()) {
if (!node->isEnd) return false; // 该路径不构成完整单词,删除失败
node->isEnd = false; // 移除单词结束标记
// 检查当前节点是否无子节点(可被安全删除的条件)
return all_of(node->children.begin(), node->children.end(),
[](auto& child){ return !child; });
}
// 计算当前字符对应的子节点索引
int idx = getIndex(word[depth]);
// 索引无效或子节点不存在,删除失败
if (idx == -1 || !node->children[idx]) return false;
// 递归删除后续字符
bool childCanDelete = removeHelper(node->children[idx].get(), word, depth+1);
// 如果子节点可删除,则释放其内存
if (childCanDelete) {
node->children[idx].reset(); // 释放子节点内存
// 检查当前节点是否可删除:非单词结尾且无其他子节点
return !node->isEnd &&
all_of(node->children.begin(), node->children.end(),
[](auto& child){ return !child; });
}
return false; // 子节点不可删除,当前节点也不可删除
}
外层调用时:
bool remove(const string& word) {
return removeHelper(root.get(), word, 0);
}
7. 列出所有单词
递归遍历整个 Trie,收集所有以 isEnd = true 结束的路径。
// 递归收集所有单词的辅助函数
void collectAll(Node* node, string& path, vector<string>& words) {
if (!node) return; // 节点为空,直接返回
if (node->isEnd) words.push_back(path); // 当前节点是单词结尾,保存路径
int limit = caseSensitive ? 52 : 26; // 根据大小写敏感设置遍历范围
for (int i = 0; i < limit; i++) { // 遍历所有可能的子节点
if (node->children[i]) { // 如果子节点存在
// 根据大小写敏感设置转换字符
char c = (caseSensitive ? (i < 26 ? 'a'+i : 'A'+(i-26)) : 'a'+i);
path.push_back(c); // 当前字符加入路径
collectAll(node->children[i].get(), path, words); // 递归处理子节点
path.pop_back(); // 回溯:移除当前字符
}
}
}
// 获取Trie中所有单词的公共接口
vector<string> listAllWords() {
vector<string> words; // 存储结果单词
string path; // 当前路径(构建中的单词)
collectAll(root.get(), path, words); // 从根节点开始递归收集
return words; // 返回所有单词
}
8. 清空 Trie
清空其实很简单:直接重置根节点。
9. 运行效果展示
可以设计一个简单的菜单来测试:
=== Trie 操作菜单 ===
1. 插入单词
2. 删除单词
3. 查找单词
4. 前缀判断
5. 打印所有单词
6. 清空 Trie
0. 退出
运行示例:
请输入要插入的单词: apple
插入成功。
请输入要查找的单词: apple
存在。
请输入前缀: app
存在该前缀。
10. 时间复杂度分析
-
插入 / 查找 / 前缀判断 / 删除
时间复杂度:O(L),其中 L = 单词长度 -
遍历所有单词
时间复杂度:O(N * L),其中 N = 单词数量
总结
Trie 树的核心优势就是 利用公共前缀节省空间,并且能在 O(L) 时间内完成查找和插入。
1658

被折叠的 条评论
为什么被折叠?



