【LeetCode】实现 Trie(前缀树)
创作时间:
作者:
@小白创作中心
【LeetCode】实现 Trie(前缀树)
引用
CSDN
1.
https://m.blog.csdn.net/qq_41840843/article/details/144995861
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
提示:
1 <= word.length, prefix.length <= 2000
word 和 prefix 仅由小写英文字母组成
insert、search 和 startsWith 调用次数 总计 不超过 3 * 10^4 次
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#define ALPHABET_SIZE 26
typedef struct TrieNode {
struct TrieNode* children[ALPHABET_SIZE];
bool isEndOfWord;
} TrieNode;
typedef struct {
TrieNode* root;
} Trie;
// 创建新的 Trie 节点
TrieNode* createNode() {
TrieNode* node = (TrieNode*)malloc(sizeof(TrieNode));
node->isEndOfWord = false;
for (int i = 0; i < ALPHABET_SIZE; i++) {
node->children[i] = NULL;
}
return node;
}
// 初始化 Trie 树
Trie* trieCreate() {
Trie* trie = (Trie*)malloc(sizeof(Trie));
trie->root = createNode();
return trie;
}
// 向前缀树中插入字符串
void trieInsert(Trie* obj, char* word) {
TrieNode* curr = obj->root;
for (int i = 0; word[i]!= '\0'; i++) {
int index = word[i] - 'a';
if (curr->children[index] == NULL) {
curr->children[index] = createNode();
}
curr = curr->children[index];
}
curr->isEndOfWord = true;
}
// 搜索字符串是否存在
bool trieSearch(Trie* obj, char* word) {
TrieNode* curr = obj->root;
for (int i = 0; word[i]!= '\0'; i++) {
int index = word[i] - 'a';
if (curr->children[index] == NULL) {
return false;
}
curr = curr->children[index];
}
return curr->isEndOfWord;
}
// 搜索前缀是否存在
bool trieStartsWith(Trie* obj, char* prefix) {
TrieNode* curr = obj->root;
for (int i = 0; prefix[i]!= '\0'; i++) {
int index = prefix[i] - 'a';
if (curr->children[index] == NULL) {
return false;
}
curr = curr->children[index];
}
return true;
}
// 释放 Trie 树内存
void trieFree(Trie* obj) {
// 辅助函数,用于递归释放节点内存
void freeNodes(TrieNode* node) {
if (node == NULL) {
return;
}
for (int i = 0; i < ALPHABET_SIZE; i++) {
freeNodes(node->children[i]);
}
free(node);
}
freeNodes(obj->root);
free(obj);
}
热门推荐
旧车牌能“搬家”到新车上吗?车管所详解条件与流程!
买车分期好还是去银行贷款好?一文详解购车贷款方式
为什么出车祸人要拘留:交通事故责任追究的法律依据与实务分析
伤情鉴定都需要哪些资料和手续
派出所出具伤情鉴定委托书需要什么材料
利用AI高效撰写年终总结报告的五大技巧
双髻鲨:海洋中的“锤头战士”
ROS核心概念解析:从Node到Master,再到roslaunch的全面指南
大小眼的三种改善方法:从按摩到手术治疗
动眼神经损伤的症状
动眼神经损伤的症状
怎样在银行电子银行上查询贷款信息?
【个人信用报告解读】信贷交易信息明细
千穗谷Amaranthus hypochondriacus Linn.
《哪吒2》盗版冰箱贴卖出10万份,IP衍生品为何盗版满天飞?
克服考前紧张的5个终极绝招,学员必看
平台买币卖币犯法吗?虚拟货币交易法律问题解析
对外汉语线上教学真的可以月入过万?
如何进行黄金基本面和技术面的分析?这种分析方法的有效性如何验证?
炒菜的起源:追溯中国烹饪艺术的一个重要里程碑
利培酮片的注意事项与禁忌症
肉苁蓉的六大功效:增强免疫力、抗衰老、保护心脑血管
二桃杀三士:一个成语故事里的智慧与人性
二桃杀三士的故事
筒灯VS射灯:解锁照明艺术的奥秘,打造理想空间新风尚
核电站能运行多久?监管者考虑运行100年
肠息肉术后1至7天的食谱
肠息肉术后1至7天的食谱
金融行业现状如何影响股票?这种影响带来哪些变化?
淘宝冷门蓝海类目:探寻电商新机遇