字典树(查找树)

26个分支
作用:检测字符串是否在这个字典里面
插入、查找

字典树与哈希表的对比:
时间复杂度:以字符来看:O(N)、O(N) 以字符串来看:O(1)、O(1)
空间复杂度:字典树远远小于哈希表

前缀相关的题目字典树优于哈希表
字典树可以查询abc是否有ab的前缀

字典树常考点:
1.字典树实现
2.利用字典树前缀特性解题
3.矩阵类字符串一个一个字符深度遍历的问题(DFS+trie)
dfs树和trie树同时遍历

word searchII
dfs+hash:时间复杂度大,后面遍历到有些字符就不用遍历了。
剪枝

在写结构体struct的时候,注意必须在{}之后加分号,不然会编译报错。

//错误
struct TrieNode {
TrieNode* child[];
bool isWord = false;
TrieNode(){
for(int i = ;i < ;i++)
child[i] = NULL;
}
}
//正确
struct TrieNode {
TrieNode* child[];
bool isWord = false;
TrieNode(){
for(int i = ;i < ;i++)
child[i] = NULL;
}
};

leetcode 208. Implement Trie (Prefix Tree)

https://www.cnblogs.com/grandyang/p/4491665.html

注意:在insert或者add新的词的时候,必须在最后将isWord置为true,以表示这是一个单词的结尾。

class Trie {
public:
struct TrieNode {
public:
TrieNode *child[];
bool isWord;
TrieNode() : isWord(false) {
for (auto &a : child) a = NULL;
}
};
/** Initialize your data structure here. */
Trie() {
root = new TrieNode();
} /** Inserts a word into the trie. */
void insert(string word) {
TrieNode* p = root;
for(char w : word){
int i = w - 'a';
if(!p->child[i])
p->child[i] = new TrieNode();
p = p->child[i];
}
p->isWord = true;
return;
} /** Returns if the word is in the trie. */
bool search(string word) {
TrieNode* p = root;
for(char w : word){
int i = w - 'a';
if(!p->child[i])
return false;
p = p->child[i];
}
return p->isWord;
} /** Returns if there is any word in the trie that starts with the given prefix. */
bool startsWith(string prefix) {
TrieNode* p = root;
for(char w : prefix){
int i = w - 'a';
if(!p->child[i])
return false;
p = p->child[i];
}
return true;
}
TrieNode* root;
};

211. Add and Search Word - Data structure design

https://www.cnblogs.com/grandyang/p/4507286.html

class WordDictionary {
public:
struct TrieNode {
public:
TrieNode *child[];
bool isWord;
TrieNode() : isWord(false) {
for (auto &a : child) a = NULL;
}
};
/** Initialize your data structure here. */
WordDictionary() {
root = new TrieNode();
} /** Adds a word into the data structure. */
void addWord(string word) {
TrieNode* p = root;
for(char w : word){
int i = w - 'a';
if(!p->child[i])
p->child[i] = new TrieNode();
p = p->child[i];
}
p->isWord = true;
return;
} /** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */
bool search(string word) {
return search_core(word,root,);
}
bool search_core(string word,TrieNode* p,int index){
if(index == word.size())
return p->isWord;
if(word[index] == '.'){
for(TrieNode* tmp : p->child){
if(tmp && search_core(word,tmp,index+))
return true;
}
return false;
}
else{
int i = word[index] - 'a';
if(!p->child[i])
return false;
return search_core(word,p->child[i],index+);
}
}
TrieNode* root;
};

字典树(查找树) leetcode 208. Implement Trie (Prefix Tree) 、211. Add and Search Word - Data structure design的更多相关文章

  1. LeetCode208 Implement Trie (Prefix Tree). LeetCode211 Add and Search Word - Data structure design

    字典树(Trie树相关) 208. Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith  ...

  2. [LeetCode] 211. Add and Search Word - Data structure design 添加和查找单词-数据结构设计

    Design a data structure that supports the following two operations: void addWord(word) bool search(w ...

  3. Java for LeetCode 211 Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...

  4. 【LeetCode】211. Add and Search Word - Data structure design

    Add and Search Word - Data structure design Design a data structure that supports the following two ...

  5. 【刷题-LeetCode】211. Add and Search Word - Data structure design

    Add and Search Word - Data structure design Design a data structure that supports the following two ...

  6. (*medium)LeetCode 211.Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word) bool search(w ...

  7. leetcode 211. Add and Search Word - Data structure design Trie树

    题目链接 写一个数据结构, 支持两种操作. 加入一个字符串, 查找一个字符串是否存在.查找的时候, '.'可以代表任意一个字符. 显然是Trie树, 添加就是正常的添加, 查找的时候只要dfs查找就可 ...

  8. [leetcode]211. Add and Search Word - Data structure design添加查找单词 - 数据结构设计

    Design a data structure that supports the following two operations: void addWord(word) bool search(w ...

  9. [leetcode trie]211. Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word) bool search(w ...

随机推荐

  1. linux 的常用命令(1)

    1.关于ls [选项][目录名] -a  列出包括.a开头的隐藏文件的所有文件-A  通-a,但不列出"."和".."-l  列出文件的详细信息-c  根据ct ...

  2. Ajax -02 -JQuery+Servlet -实现页面点击刷出表格数据

    demo功能分析 jquery 的js文件需要导入,json的三个文件需要导入,不然writeValueAsString 会转化成JsonArray(json 数组)失败 $("#mytbo ...

  3. IPV4地址耗尽,了解IPV6。

    北京时间 2019 年 11 月 26 日下午,负责互联网资源分配的最后一个信息中心——欧洲网络信息中心(RIPE NCC)宣布耗尽了最后一个 IPv4 地址区块,至此,全球所有 43 亿个 IPv4 ...

  4. CF600E Lomsat gelral 和 CF741D Dokhtar-kosh paths

    Lomsat gelral 一棵以\(1\)为根的树有\(n\)个结点,每个结点都有一种颜色,每个颜色有一个编号,求树中每个子树的最多的颜色编号(若有数量一样的,则求编号和). \(n \le 10^ ...

  5. CString、char*l类型转换

    环境:VS2010 1.CString to char* 第一种方法: 需头文件:atlconv.h CString   host_string; //CString to char* USES_CO ...

  6. 安装 selenium 对于python而言属于一个第三方的模块

    针对第三方的模块,如何安装 在dos界面输入python -m pip install 模块名称 安装相关的浏览器以及浏览器的驱动 下载谷歌浏览器的驱动,淘宝镜像 下载后,解压,然后将得到的exe文件 ...

  7. kafka2.12 集群搭建

    前提: 1.下载 kafka http://kafka.apache.org/downloads 2.下载配置zookeeper http://www.cnblogs.com/eggplantpro/ ...

  8. jsp+ tinymce粘贴word

    最近公司做项目需要实现一个功能,在网页富文本编辑器中实现粘贴Word图文的功能. 我们在网站中使用的Web编辑器比较多,都是根据用户需求来选择的.目前还没有固定哪一个编辑器 有时候用的是UEditor ...

  9. am335x system upgrade kernel uart(七)

    1      Scope of Document This document describes UART hardware design, uart driver porting 2      Re ...

  10. PPP

    名称 chat–调制解调器的自动对话脚本 命令格式 chat [options] script 描述 Chat程序定义了一个计算机和调制解调器之间对话交流,其主要目的是用来在本地PPPD和远端PPPD ...