字典树(前缀树)-Java实现
字典树
字典树是一种树形结构,优点是利用字符串的公共前缀来节约存储空间。在这提供一个自己写的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实现的更多相关文章
- 9-11-Trie树/字典树/前缀树-查找-第9章-《数据结构》课本源码-严蔚敏吴伟民版
课本源码部分 第9章 查找 - Trie树/字典树/前缀树(键树) ——<数据结构>-严蔚敏.吴伟民版 源码使用说明 链接☛☛☛ <数据结构-C语言版>(严蔚 ...
- [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...
- [LeetCode] 208. Implement Trie (Prefix Tree) 实现字典树(前缀树)
Implement a trie with insert, search, and startsWith methods. Example: Trie trie = new Trie(); trie. ...
- 内存空间有限情况下的词频统计 Trie树 前缀树
数据结构与算法专题--第十二题 Trie树 https://mp.weixin.qq.com/s/nndr2AcECuUatXrxd3MgCg
- Trie - leetcode [字典树/前缀树]
208. Implement Trie (Prefix Tree) 字母的字典树每个节点要定义一个大小为26的子节点指针数组,然后用一个标志符用来记录到当前位置为止是否为一个词,初始化的时候讲26个子 ...
- LeetCode OJ:Implement Trie (Prefix Tree)(实现一个字典树(前缀树))
Implement a trie with insert, search, and startsWith methods. 实现字典树,前面好像有道题做过类似的东西,代码如下: class TrieN ...
- HDU 1251 字典树(前缀树)
题目大意 :Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀).(单词互不相同) ...
- python利用Trie(前缀树)实现搜索引擎中关键字输入提示(学习Hash Trie和Double-array Trie)
python利用Trie(前缀树)实现搜索引擎中关键字输入提示(学习Hash Trie和Double-array Trie) 主要包括两部分内容:(1)利用python中的dict实现Trie:(2) ...
- Trie(前缀树/字典树)及其应用
Trie,又经常叫前缀树,字典树等等.它有很多变种,如后缀树,Radix Tree/Trie,PATRICIA tree,以及bitwise版本的crit-bit tree.当然很多名字的意义其实有交 ...
- 208 Implement Trie (Prefix Tree) 字典树(前缀树)
实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个方法.注意:你可以假设所有的输入都是小写字母 a-z.详见:https://leetcode.co ...
随机推荐
- Node.js其他模块
清明假期过得挺快,3天说没就没了,天热了今天把房间打扫了一下,看着挺舒心的.周六了解了下进程管理的Process模块,由于进程管理知识也比较多,今天先把其他的一些模块了解一下,进程管理这块以后慢慢学. ...
- [转]ng-grid Auto / Dynamic Height
本文转自:https://stackoverflow.com/questions/23396398/ng-grid-auto-dynamic-height I think I solved this ...
- SQLServer数据库循环表操作每一条数据(游标的使用)
DECLARE @FunctionCode VARCHAR(20)--声明游标变量DECLARE curfuntioncode CURSOR FOR SELECT FunctionalityCode ...
- ASP.NET 省市县三联动 (包含用户控件)
将压缩文件下载解压后,将用户控件拖到解决方案里,直接可以拖到需要用到的页面里 使用: 数据库是最新的(父子级关系表结构----Region2016.sql) 右键记事本打开,放在sqlServerl里 ...
- iOS开发消息推送原理
转载自:http://www.cnblogs.com/cdts_change/p/3240893.html 一.消息推送原理: 在实现消息推送之前先提及几个于推送相关概念,如下图1-1: 1.Prov ...
- 啰里吧嗦kafka
1.kafka是什么 kafka官网: http://kafka.apache.org/ kafka是一种高吞吐量的分布式发布订阅消息系统,用它可以在不同系统中间传递分发消息 2.zookeeper是 ...
- 在IIS7中应用Application Request Routing配置反向代理
配置反向代理软件.zip 开启Proxy项: 该设置表面只有HTTP_HOST为phpweb.leven.com.cn的URL才能通过该规则,如果您绑定了多个域名,可以根据多次增加或者通过正则表达式的 ...
- EF 调用oracle 存储过程
EF是如何调用的存储过程的,本人也是翻遍了个大网站,查阅了很多资料.终于解决了遇到的问题. 第一步:创建存储过程,在这里我就不多说了,不是文章说的重点. declare O_VOUCHER_ACT_D ...
- C# base64 和图片互转
C# imgage图片转base64字符/base64字符串转图片另存成 //图片转为base64编码的字符串 protected string ImgToBase64String(string Im ...
- Code Signal_练习题_adjacentElementsProduct
Given an array of integers, find the pair of adjacent elements that has the largest product and retu ...