字典树

字典树是一种树形结构,优点是利用字符串的公共前缀来节约存储空间。在这提供一个自己写的Java实现,非常简洁。

  • 根节点没有字符路径。除根节点外,每一个节点都被一个字符路径找到。
  • 从根节点到某一节点,将路径上经过的字符连接起来,为对应字符串。
  • 每个节点向下所有的字符路径上的字符都不同

每个结点维持两个变量的记录:path表示字符路过这个结点的次数(即表示存在以当前结点为前缀的字符有多少个);end记录以当前结点为结束的字符有多少个。

public class Main {
public static void main(String[] args) {
Trie trie = new Trie();
trie.add("a");
trie.add("ab");
trie.add("ac");
trie.add("abc");
trie.add("acb");
trie.add("abcc");
trie.add("aab");
trie.add("abx");
trie.add("abc"); System.out.println(trie.get("abc"));
System.out.println(trie.getPre("ab"));
}
}
/**
* path表示字符路过这个结点的次数(即表示存在以当前结点为前缀的字符有多少个);
* end记录以当前结点为结束的字符有多少个。
*/
class TrieNode {
public int path;
public int end;
public TrieNode[] nexts; public TrieNode() {
path = 0;
end = 0;
//只能存英文小写字母,如果是ASCII码可以生成256大小的数组
//如果想存更多种类的字符可以改为map结构
nexts = new TrieNode[26];
}
} class Trie {
private TrieNode root; Trie() {
root = new TrieNode();
} /**
* 字典树的加入过程
*/
public void add(String word) {
if (word == null) return;
char[] chars = word.toCharArray();
TrieNode node = root;
int index = 0;
for (char c : chars) {
index = c - 'a';
if (node.nexts[index] == null) {
node.nexts[index] = new TrieNode();
}
node = node.nexts[index];
node.path++;
}
node.end++;
}
/**
* 字典树查询目标单词出现的次数
*/
public int get(String word) {
if (word == null) return 0;
char[] chars = word.toCharArray();
TrieNode node = root;
int index = 0;
for (char c : chars) {
index = c - 'a';
if (node.nexts[index] == null) return 0;
node = node.nexts[index];
}
return node.end;
}
/**
* 字典树查询以目标前缀的单词有多少个
*/
public int getPre(String word) {
if(word ==null) return 0;
char[] chars = word.toCharArray();
TrieNode node = root;
int index = 0;
for (char c : chars) {
index = c - 'a';
if(node.nexts[index]==null)
return 0;
node = node.nexts[index];
}
return node.path;
}
}

字典树(前缀树)-Java实现的更多相关文章

  1. 9-11-Trie树/字典树/前缀树-查找-第9章-《数据结构》课本源码-严蔚敏吴伟民版

    课本源码部分 第9章  查找 - Trie树/字典树/前缀树(键树) ——<数据结构>-严蔚敏.吴伟民版        源码使用说明  链接☛☛☛ <数据结构-C语言版>(严蔚 ...

  2. [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)

    Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...

  3. [LeetCode] 208. Implement Trie (Prefix Tree) 实现字典树(前缀树)

    Implement a trie with insert, search, and startsWith methods. Example: Trie trie = new Trie(); trie. ...

  4. 内存空间有限情况下的词频统计 Trie树 前缀树

    数据结构与算法专题--第十二题 Trie树 https://mp.weixin.qq.com/s/nndr2AcECuUatXrxd3MgCg

  5. Trie - leetcode [字典树/前缀树]

    208. Implement Trie (Prefix Tree) 字母的字典树每个节点要定义一个大小为26的子节点指针数组,然后用一个标志符用来记录到当前位置为止是否为一个词,初始化的时候讲26个子 ...

  6. LeetCode OJ:Implement Trie (Prefix Tree)(实现一个字典树(前缀树))

    Implement a trie with insert, search, and startsWith methods. 实现字典树,前面好像有道题做过类似的东西,代码如下: class TrieN ...

  7. HDU 1251 字典树(前缀树)

    题目大意 :Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀).(单词互不相同) ...

  8. python利用Trie(前缀树)实现搜索引擎中关键字输入提示(学习Hash Trie和Double-array Trie)

    python利用Trie(前缀树)实现搜索引擎中关键字输入提示(学习Hash Trie和Double-array Trie) 主要包括两部分内容:(1)利用python中的dict实现Trie:(2) ...

  9. Trie(前缀树/字典树)及其应用

    Trie,又经常叫前缀树,字典树等等.它有很多变种,如后缀树,Radix Tree/Trie,PATRICIA tree,以及bitwise版本的crit-bit tree.当然很多名字的意义其实有交 ...

  10. 208 Implement Trie (Prefix Tree) 字典树(前缀树)

    实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个方法.注意:你可以假设所有的输入都是小写字母 a-z.详见:https://leetcode.co ...

随机推荐

  1. stream was not readable.

    StreamWriter使用时的报错情况: stream was not readable. 错误原因: 没有指定StreamWriter的写入文件 正确代码示例1: byte[] businessD ...

  2. hadoop2.x学习笔记(一):YARN

    一.YARN产生的背景 MapReduce1.x存在的问题:单点故障&节点压力大不易扩展. 资源利用率&成本 催生了YARN的诞生  不同计算框架可以共享同一个HDFS集群上的数据,享 ...

  3. Windows2012开机启动项设置

    最简单方式 开始->运行->输入shell:startup 在打开的启动文件夹中,将需要启动程序的快捷方式复制进去,完工 重启试试吧 https://blog.csdn.net/tmton ...

  4. DedeCMS修改管理员用户名

    织梦内容管理系统(DedeCMS) 以简单.实用.开源而闻名,是国内最知名的PHP开源网站管理系统,也是使用用户最多的PHP类CMS系统,在经历多年的发展,目前的版本无论在功能,还是在易用性方面,都有 ...

  5. [日常] crontab的秒执行和串行化和多进程实现

    1. crontab的最低运行频率是,按照每分钟执行一次,通过在脚本中简单实现按秒级别运行 比如这条cron规则 , 每分钟执行一次脚本 * * * * * php /var/www/html/tes ...

  6. 【基于初学者的SSH】struts2 环境配置

    01:导入Jar包 下载地址:http://struts.apache.org/ 将下好的jar包放导WEB-INF下的lib文件夹下 02:创建Action:com.action.LoginActi ...

  7. java 二分法

    源码 public class Dichotomy { public static void main(String[] args){ int[] array = new int[12]; for(i ...

  8. mysql 中 max_allowed_packet 查询和修改

    mysql 会根据配置文件限制 server 接收的数据包的大小. 有时候大的插入和更新会被 max_allowed_packet 参数限制,报如下错误: Packet > ). You can ...

  9. spring 中 InitializingBean 接口使用理解

    前言:这两天在看 spring 与 quart 的集成,所以了解到 spring 是如何初始化 org.springframework.scheduling.quartz.SchedulerFacto ...

  10. 使用AutoFac组织多项目应用程序

    较复杂的应用程序都是由多个项目组织成的,项目可以划分成程序集(Assemblies)和宿主(Hosts),也就是应用程序的入口.      Assemblies 通常是常见的类库项目,包括可以重用的功 ...