【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);
}
热门推荐
世界地贫日丨什么是地中海贫血?可以治愈吗?
饺子馅好吃有诀窍,教你饺子店大厨的不外传秘方,鲜嫩多汁不出水,香迷糊了!
逻辑学中矛盾律的应用:探究逻辑推理的基本原则
远古人类在无衣无房的条件下,如何度过冰川期呢?主要有4种方式
汽车玻璃水冻住了怎么办?老司机总结5个实用解决方案
越橘的功效与作用、禁忌和食用方法
古代埃及“玛阿特”中蕴含的道德心理
古代埃及“玛阿特”中蕴含的道德心理
如何安全使用随车吊?
夫晋,何厌之有?
数控机床的维护与保养:确保设备长期稳定运行的关键
塑料分类知多少?回收时这些细节一定要注意!
PCB概念股低开高走 胜宏科技大涨7%创历史新高
《西游记》里的猪八戒的前妻卯二姐是人还是妖?真的死了吗?
聊一聊芯片中的那些可靠性测试项目
常见的环境可靠性测试项目有哪些?
少儿体适能训练暑期爆火,运动“必修课”还是“智商税”?
晋阳湖片区变身太原文化新地标
民国三年“袁大头”特别版本161万成交,真的
如何分析影响黄金的重要数据特征?这些数据特征如何影响市场预期?
摩托车未佩戴安全头盔的法律处罚及其影响
弹钢琴,如何提高手指灵活度和独立性?
年轻人爱为健康养生买单(大数据观察·青年消费新趋势)
减少公司注册资本可以逃避债务?股东违法减资应承担补充赔偿责任
鼻子上长痘痘怎么办?全方位护肤指南来帮你
如何分析黄金板块股票的投资机会?这种分析方法对投资决策有何影响?
一篇文章教你区分什么是SSO单点登录、IAM统一身份认证、I²M统一身份中台
深层解读:统治地球超过1.7亿年的恐龙,为何没有进化出智慧?
回眸丨钱学森弹道:借现代力学之力,飞出新高度
揭秘"玉灵细胞康养仪"骗局:四大套路专骗老年人