当前位置:网站首页>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);
*/
边栏推荐
- Database index
- OpenSSL client programming: SSL session failure caused by an obscure function
- Redis data structure
- 二叉树相关问题2
- 主键选择选择自增还是序列?
- Crontab's learning essays
- Hash table - Review
- 数据库优化
- Redis持久化
- At 19:00 on Tuesday evening, the 8th live broadcast of battle code Pioneer - how to participate in openharmony's open source contribution in multiple directions
猜你喜欢

数据库锁问题

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

Cloud native Security Guide: learn kubernetes attack and defense from scratch

Redis 大 key 问题处理总结

谈谈我写作生涯的画图技巧

openssl客户端编程:一个不起眼的函数导致的SSL会话失败问题

数智化进入“深水区”,数据治理是关键

本周二晚19:00战码先锋第8期直播丨如何多方位参与OpenHarmony开源贡献

Bit. Store: long bear market, stable stacking products may become the main theme
Record a failure caused by a custom redis distributed lock
随机推荐
数据库日志
ABAP essays - interview memories hope that everyone's demand will not increase and the number of people will soar
UOS prompts for password to unlock your login key ring solution
NVIDIA三件套环境配置
redis数据结构
Select auto increment or sequence for primary key selection?
数智化进入“深水区”,数据治理是关键
海量数据出席兰州openGauss Meetup(生态全国行)活动,以企业级数据库赋能用户应用升级
Data intelligence enters the "deep water area", and data governance is the key
Linux system plays Oracle database multi table query connection query with a smile
智联招聘的基于 Nebula Graph 的推荐实践分享
It took me 6 months to complete the excellent graduation project of undergraduate course. What have I done?
Type the URL to the web page display. What happened during this period?
Mass lucky hash game system development - conflict resolution (code analysis)
Redis持久化
Longitude and latitude analysis
移动低代码开发专题月 | 可视化开发 一键生成专业级源码
[bug] Lenovo Xiaoxin has a problem. Your pin is unavailable.
低代码开发平台是什么?为什么现在那么火?
muduo
.