字典树(查找树)

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. DNS BIND配置 配置基本缓存服务器 DNS正向解析 DNS反向解析

    一. 缓存服务器配置 1.DNS:BIND    Berkeley Internet Name Domain    版本bind97: RPM服务器端包的名字  安装bind-libs    bind ...

  2. destoon标签大集合

    最近没事玩上了destoon,所以就自己花一点时间整理了一下destoon标签,对开发有帮助,本篇文章由博客园-圆柱模板  博主整理发布 1.全局标签 网站名称:{$DT[sitename]} 网站地 ...

  3. 「SDOI2016」征途

    征途 Pine开始了从S地到T地的征途. 从S地到T地的路可以划分成\(n\)段,相邻两段路的分界点设有休息站. Pine计划用\(m\)天到达T地.除第\(m\)天外,每一天晚上Pine都必须在休息 ...

  4. cookies插件 , axios插件,element-ui 插件

    vue-cookie插件 安装 >: cnpm install vue-cookies main.js配置 // 第一种方式 import cookies from 'vue-cookies' ...

  5. Linux安装版本solr-5.3.0

    准备材料:solr-5.3.0.tgz 下载路径:http://mirrors.hust.edu.cn/apache/lucene/solr/ 安装solr 1.解压solr [root@svn-se ...

  6. go语言-for循环

    一.for循环语法: for 循环变量初始化:循环条件:循环变量迭代{ 循环体 }案例: 打印10句hello 方式一 package main import "fmt" func ...

  7. oracle+mybatis报错:BindingException("Invalid bound statement (not found): ")

    oracle+mybatis报错:BindingException("Invalid bound statement (not found): ") 从mysql转到oracle数 ...

  8. 2.1 什么是C++

    C++是一种通用程序设计语言,特别是面向系统程序设计. 1:是一个更好的C 2:支持数据抽象 3:支持面向对象的程序设计 4:支持通用型程序设计

  9. HiveQL 数据装在与导出

    一.向管理表中装载数据 1.向表中装载数据load 1)load语法 2)LOCAL  指的是操作系统的文件路径,否则默认为HDFS的文件路径 3)overwrite关键字 如果用户指定了overwr ...

  10. Filebeat在windows下安装使用

    一.windows下安装Filebeat 官网下载安装包 解压到指定目录,打开解压后的目录,打开filebeat.yml进行配置. 1.配置为输出到ElasticSearch ①:配置 Filebea ...