树的节点代表集合
树的代表关系

字典树代码

查找代码

#include<iostream>
#include<cstdio>
#include<queue>
#include<stack>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<vector>
using namespace std;#define BASE 26
//字典树节点
class node{public ://结点初始化node(){flag = false;for(int i=0;i<BASE;i++) next[i] = nullptr;//每个字典树的next结点设定为空}~node(){delete node;}bool flag;//当前节点是否独立成词 (是红色结点还是白色结点)node *next[BASE];//节点只存储小写字母
};
//定义字典树类
class Trie{public:Trie(){root = new node();//给root分配新的结点空间}//传入一个字符串然后插入到字典树中去bool insert(string word){//这个方法表示是否是第一次插入这个单词//定义一个指针变量指向根结点node *p = root;for(auto x:word){//遍历每一个单词字母int ind = x-'a';//如果当前节点的边下边没有结点的话新建一个node if(p->next[ind]==nullptr)p->next[ind]=new node();p=p->next[ind];}if(p->flag) return false;//如果p->flag本身就不为true了说明不是第一次插入这个单词//插入完单词后需要将单词最后一个节点变成红色p->flag=true;return true;}//字典树的单词查找操作bool search(string word){node *p = root;//字典树操作永远从根结点开始操作因为根结点代表全集for(auto x:word){int ind = x-'a';p = p->next[ind];//p先沿着边向下走一步if(p==nullptr) return false;//p走到空节点说明字典树中根本就没有这个单词}//此时说明我沿着word每一个单词走的不是空节点 需要判断我们当前所在结点是白色还是红色呀, 只有它是红色才证明它是一个独立的单词return p->flag;}static void clearTrie(node *root){if(root==nullptr)return ;//否则递归的销毁所有的子树for(int i=0;i<BASE;i++)clearTrie(root->next[i]);delete root;return ;}~Trie(){clearTrie(root);}
private:node *root;
};int main(){Trie t;int op;string s;while(cin>>op>>s){switch(op){case 1:t.insert(s);break;case 2:cout<<"search word = "<<s<<", result :"<<t.search(s)<<endl;}}return 0;
}

字典树排序代码:

#include<iostream>
#include<cstdio>
#include<queue>
#include<stack>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<vector>
using namespace std;#define BASE 26
//字典树节点
class node{public ://结点初始化node(){flag = false;for(int i=0;i<BASE;i++) next[i] = nullptr;//每个字典树的next结点设定为空}~node(){}bool flag;//当前节点是否独立成词 (是红色结点还是白色结点)node *next[BASE];//节点只存储小写字母
};
//定义字典树类
class Trie{public:Trie(){root = new node();//给root分配新的结点空间}//传入一个字符串然后插入到字典树中去bool insert(string word){//这个方法表示是否是第一次插入这个单词//定义一个指针变量指向根结点node *p = root;for(auto x:word){//遍历每一个单词字母int ind = x-'a';//如果当前节点的边下边没有结点的话新建一个node if(p->next[ind]==nullptr)p->next[ind]=new node();p=p->next[ind];}if(p->flag) return false;//如果p->flag本身就不为true了说明不是第一次插入这个单词//插入完单词后需要将单词最后一个节点变成红色p->flag=true;return true;}//字典树的单词查找操作bool search(string word){node *p = root;//字典树操作永远从根结点开始操作因为根结点代表全集for(auto x:word){int ind = x-'a';p = p->next[ind];//p先沿着边向下走一步if(p==nullptr) return false;//p走到空节点说明字典树中根本就没有这个单词}//此时说明我沿着word每一个单词走的不是空节点 需要判断我们当前所在结点是白色还是红色呀, 只有它是红色才证明它是一个独立的单词return p->flag;}//字典树中output方法void output(){string s;__output(root,s);return ;}static void __output(node *root,string s){if(root == nullptr)return ;if(root->flag) cout<<"find:"<<s<<endl;for(int i=0;i<BASE;i++){__output(root->next[i],s+char(i+'a'));}return ;}static void clearTrie(node *root){if(root==nullptr)return ;//否则递归的销毁所有的子树for(int i=0;i<BASE;i++)clearTrie(root->next[i]);delete root;return ;}~Trie(){clearTrie(root);}
private:node *root;
};int main(){int n;cin>>n;string s;Trie t;for(int i=0;i<n;i++){cin>>s;t.insert(s);}t.output();return 0;
}

升级版本字典树

#include<iostream>
#include<cstdio>
#include<queue>
#include<stack>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<vector>
using namespace std;#define BASE 26class node{public:int flag;//当前节点是否独立成词int next[BASE];//一个结点在数组中的下标void clear(){flag = 0;for(int i=0;i<BASE;i++){next[i] = 0;}}
}trie[10000];
int cnt,//我能够使用的结点第一个编号
root;//root代表根结点的编号
void clearTrie(){//初始化字典树cnt = 2;root = 1;trie[root].clear();return ;
}int getNewNode(){//返回的是新节点的编号trie[cnt].clear();//在返回cnt 这个结点之前需要将Cnt这个结点的编号进行清空return cnt++;
}
void insert(string s){int p = root;for(auto x:s){int ind = x-'a';if(trie[p].next[ind] == 0)trie[p].next[ind] = getNewNode();p = trie[p].next[ind];}trie[p].flag=1;return ;
}
bool search(string s){int p = root;for(auto x:s){int ind = x-'a';p=trie[p].next[ind];if( p == 0 ) return false;}return trie[p].flag;
}
int main(){cout<<"trie version 3:"<<endl;clearTrie();int op;string s;while(cin>>op>>s){switch(op){case 1:insert(s);break;case 2:cout<<"search word = "<<s<<", result :"<<search(s)<<endl;}}
return 0;

}

双数组字典树

逻辑结构还是那个结构,只是换了一种信息的表示方式(就像从阿拉伯数字变成了罗马数字)

leetcode-1268.搜索推荐系统

//定义一个字典树
#define BASE 26
class Node{public:Node(){flag = false;for(int i=0;i<BASE;i++)next[i] = nullptr;}~Node(){}set<string> s;//在每个节点处存储一个集合bool flag;Node *next[BASE];
};
class Trie{public:Trie(){root = new Node();}bool insert(string word){Node *p = root;   for(auto x:word){int ind = x-'a';if(p->next[ind] == nullptr)p->next[ind] = new Node();p = p->next[ind];p->s.insert(word);if(p->s.size()>3){auto iter = p->s.end();iter--;p->s.erase(iter);}}if(p->flag) return false;p->flag = true;return true;}vector<vector<string>> search(string word){Node *p= root;vector<vector<string>> ret;for(auto x:word){if(p == nullptr){ret.push_back(vector<string>());continue;}int ind = x-'a';p = p->next[ind];vector<string> temp;if(p != nullptr){for(auto s:p->s){temp.push_back(s);}ret.push_back(temp);}}return ret;}static void clearTrie(Node *root){if(root == nullptr) return ;for(int i=0;i<BASE;i++) clearTrie(root->next[i]);delete root;return ;}~Trie(){clearTrie(root);}
private:Node *root;
};
class Solution {public:vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {Trie tree;for(auto s:products)tree.insert(s);vector<vector<string>> ret;return tree.search(searchWord);}
};

字典树与双数组字典树相关推荐

  1. Kiner算法刷题记(二十一):字典树与双数组字典树(数据结构基础篇)

    字典树与双数组字典树(数据结构基础篇) 系列文章导引 系列文章导引 开源项目 本系列所有文章都将会收录到GitHub中统一收藏与管理,欢迎ISSUE和Star. GitHub传送门:Kiner算法算题 ...

  2. python Trie树和双数组TRIE树的实现. 拥有3个功能:插入,删除,给前缀智能找到所有能匹配的单词...

    #coding=utf-8 #字典嵌套牛逼,别人写的,这样每一层非常多的东西,搜索就快了,树高26.所以整体搜索一个不关多大的单词表 #还是O(1). ''' Python 字典 setdefault ...

  3. 字典树与双数组字典树总结

    字典树 字典树比较简单,本质是一个DFA(define finite automata. 具体可 关键词搜索leetcode trie tree 双向数组字典树 参考文献: http://www.do ...

  4. 双数组trie树详解

    目录 双数组trie树的构建 构建base array 构建check array 双数组trie树的查询 双数组trie树的构建 NLP中trie树常用于做快速查询,但普通的trie树由于要保存大量 ...

  5. 用Python实现字典树(Trie)与双数组字典树(DATrie)

    1. 字典树(Trie) 假如我们把字典中的词以记录的形式(无序)存入数据库中.现给定一串字符,要查找该字符串是否为字典中的词.因为数据库中的记录是无序的,所以,最朴素的方法就逐记录匹配.此方法简单, ...

  6. 双数组Trie树(DoubleArrayTrie)Java实现

    http://www.hankcs.com/program/java/%E5%8F%8C%E6%95%B0%E7%BB%84trie%E6%A0%91doublearraytriejava%E5%AE ...

  7. Ansj分词双数组Trie树实现与arrays.dic词典格式

    http://www.hankcs.com/nlp/ansj-word-pairs-array-tire-tree-achieved-with-arrays-dic-dictionary-format ...

  8. 双数组trie树的基本构造及简单优化

    一 基本构造 Trie树是搜索树的一种,来自英文单词"Retrieval"的简写,可以建立有效的数据检索组织结构,是中文匹配分词算法中词典的一种常见实现.它本质上是一个确定的有限状 ...

  9. 数据结构-----基于双数组的Trie树

    Trie树简介 Trie树也称字典树,在字符串的查找中优势比较明显,适用于在海量数据中查找某个数据.因为Trie树的查找时间和数据总量没有关系,只和要查找的数据长度有关.比如搜索引擎中热度词语的统计. ...

最新文章

  1. ---随心买统计查询
  2. Java枚举的小用法
  3. angularjs组件间通讯_详解Angular2组件之间如何通信
  4. 关于AttributeError:‘Flask‘ object has no attribute ‘ensure_sync‘的报错解决
  5. 信息系统项目管理师-信息系统成本管理核心知识点思维脑图
  6. js之数据类型及类型转换
  7. 原生js获取宽高与jquery获取宽高的方法的关系
  8. Matlab数字图像处理——图像增强
  9. 小米 note3 android,小米Note3将更新MIUI10:基于安卓8.1 速度提升明显手感更加丝滑...
  10. HTML+CSS+JavaScript仿京东购物商城网站 web前端制作服装购物商城 html电商购物网站...
  11. HTTP1.0、HTTP2.0、HTTP 3.0及HTTPS简要介绍
  12. 3ds Max 布尔(Boolean)运算——象棋的制作
  13. transition 用法
  14. Animator.paly 跳到动画结束
  15. 国科大学习资料--人工智能原理与算法-第四次作业解析(学长整理)
  16. 多标签分类方法总结——实现方法、评价指标、损失函数
  17. 天朝皇叔:学习笔记 Qt 连接数据库sql server
  18. vc6.0中用GDIPlus实现加载动态gif图片(非MFC实现)
  19. Clean Code(整洁代码)
  20. 虚拟机架设传世服务器,大话西游2特色版虚拟机镜像一键服务端+启动教程+物品ID+GM设置方法+数据库工具等...

热门文章

  1. qq邮箱怎么引流,带你玩转QQ邮箱引流,QQ邮箱引流技巧
  2. 南非世界杯对阵及电视直播表
  3. Webots的示例中心(demo)
  4. 逆战d3dx10_43.dll文件加载失败及dll文件缺失损坏修复解决方案
  5. 服务器电源管理系统SPM 价格,Liebert SPM 2.0服务器电源管理系统:在对比中彰显技术特性和性能优势...
  6. java集成微信支付接口(微信V3版)
  7. 设计师值得去的二十个网站
  8. AD9361 补充(上)
  9. ACM-ICPC比赛随想——刘汝佳
  10. crawler4j mysql_爬虫初探(二)解析crawler4j源码crawler包