当前位置:网站首页>Dictionary tree (review)
Dictionary tree (review)
2022-06-27 20:36:00 【Lao Yan is trying】
Trie [traɪ] Pronunciation and try identical , Some of its other names are : Dictionary tree , Prefix tree , Word search tree, etc .
Dictionary tree
It contains three words “sea”,“sells”,"she" Of Trie The appearance of :

Defining classes Trie
class Trie {
private:
bool isEnd;
Trie* next[26];
public:
// Insert
// lookup
// Prefix matching
};Insert
towards Trie Insert a word in word
Start with the child nodes of the root node word To match the first character of , Until there is no corresponding character in the prefix chain , At this time, new nodes are constantly opened up , Until the end of insertion word Last character of .
At the same time, the last node isEnd=true, It means it's the end of a word
void Trie::insert(string word){
Trie* node = this;
for (char c : word) {
if (node->next[c - 'a'] == nullptr) {
node->next[c - 'a'] = new Trie();
}
node = node->next[c - 'a'];
}
// Set the last node to isEnd=true, Indicates the end of a word
node->isEnd = true;
}lookup
lookup Trie Whether there are words in word
Start with the children of the root node , Match all the way down , If the node value is null Then return to false.
If it matches the last character , Judge node->isEnd And back to .
bool Trie::search(string word) {
Trir* node = this;
for (char c : word) {
node = node->next[c - 'a'];
if (node == nullptr) {
return false;
}
return node->isEnd;
}
}Prefix matching
Judge Trie Is there anything in the book to prefix Prefixed words
Be similar to search, There is no need to judge the last character node isEnd
bool Trie::startsWith(string prefix) {
Trie* node = this;
for (char c : prefix) {
node = node->next[c - 'a'];
if (node == nullptr) {
return false;
}
}
return true;
}summary :
- Trie The shape of is independent of the order in which words are inserted or deleted , That is to say, for any given set of words ,Trie All shapes are unique
- Find or insert a length of L 's words , visit next The maximum number of arrays is L+1, and Trie It doesn't matter how many words it contains .
- Trie There is an alphabet in each node of , It's very space consuming . If Trie The height of n, The size of the alphabet is m, The worst case scenario is Trie Words with the same prefix don't exist in , So the spatial complexity is
.
208. Realization Trie ( Prefix tree ) - Power button (LeetCode)
class Trie {
private:
bool isEnd;
Trie* next[26];
public:
Trie() {
isEnd = false;
memset(next, 0, sizeof(next));
}
void insert(string word) {
Trie* node=this;
for(char c:word){
if(node->next[c-'a']==nullptr){
node->next[c-'a']=new Trie();
}
node=node->next[c-'a'];
}
node->isEnd=true;
}
bool search(string word) {
Trie* node=this;
for(char c:word){
node=node->next[c-'a'];
if(node==nullptr){
return false;
}
}
return node->isEnd;
}
bool startsWith(string prefix) {
Trie* node=this;
for(char c:prefix){
node=node->next[c-'a'];
if(node==nullptr){
return false;
}
}
return true;
}
};
/**
* Your Trie object will be instantiated and called as such:
* Trie* obj = new Trie();
* obj->insert(word);
* bool param_2 = obj->search(word);
* bool param_3 = obj->startsWith(prefix);
*/1804. Realization Trie ( Prefix tree ) II - Power button (LeetCode)
Here we refer to an old brother's solution .. It breaks away from the traditional way of writing dictionary tree , Use a hash table to implement , Simple comparison
It should be noted that cnt+=it->second, instead of cnt++;
class Trie {
private:
unordered_map<string,int>m;
public:
Trie() {
m.clear();
}
void insert(string word) {
++m[word];
}
int countWordsEqualTo(string word) {
if(m.count(word)){
return m[word];
}
return 0;
}
int countWordsStartingWith(string prefix) {
int cnt=0;
for(auto it=m.begin();it!=m.end();++it){
if(it->first.find(prefix)==0){
cnt+=it->second;
}
}
return cnt;
}
void erase(string word) {
--m[word];
}
};
/**
* Your Trie object will be instantiated and called as such:
* Trie* obj = new Trie();
* obj->insert(word);
* int param_2 = obj->countWordsEqualTo(word);
* int param_3 = obj->countWordsStartingWith(prefix);
* obj->erase(word);
*/
边栏推荐
- Csdn Skills Tree use Experience and Product Analysis (1)
- 智联招聘的基于 Nebula Graph 的推荐实践分享
- 微信iOS版8.0.24更新发布 缓存细分清理上线
- Manage rust project through cargo
- Data intelligence enters the "deep water area", and data governance is the key
- NVIDIA three piece environment configuration
- (LC)46. Full Permutation
- Data intelligence enters the "deep water area", and data governance is the key
- pfSense Plus22.01中文定制版发布
- I haven't thought about the source for some time. After upgrading to the latest version 24, the data encryption problem is repeatedly displayed
猜你喜欢

Safety is the last word, Volvo xc40 recharge
Record a failure caused by a custom redis distributed lock

muduo

Grasp the detailed procedure of function call stack from instruction reading

从指令交读掌握函数调用堆栈详细过程

Postman 汉化教程(Postman中文版)

海量数据出席兰州openGauss Meetup(生态全国行)活动,以企业级数据库赋能用户应用升级

A distribution fission activity is more than just a circle of friends

SQL审核平台权限模块介绍和账号创建教程

Redis cluster
随机推荐
Cloud native Security Guide: learn kubernetes attack and defense from scratch
[bug] Lenovo Xiaoxin has a problem. Your pin is unavailable.
数据仓库体系之贴源层、历史层
[STL programming] [common competition] [Part 3]
元宇宙虚拟数字人离我们更近了|华锐互动
Logcli-loki 命令行工具
数据库锁问题
什么是堆栈?
Installation and configuration of grayog new generation log collection early warning system
#yyds干货盘点#SQL 子查询
智联招聘的基于 Nebula Graph 的推荐实践分享
Pyhton crawls Baidu library text and writes it into word document
实现字符串MyString
database engine
Redis persistence
【bug】上传图片出现错误(413 Request Entity Too Large)
Backtracking related issues
Redis cluster Series II
本周二晚19:00战码先锋第8期直播丨如何多方位参与OpenHarmony开源贡献
SQL reported an unusual error, which confused the new interns
.