标准Trie字典树学习二:Java实现方式之一
特别声明:
博文主要是学习过程中的知识整理,以便之后的查阅回顾。部分内容来源于网络(如有摘录未标注请指出)。内容如有差错,也欢迎指正!
系列文章:
1. 标准Trie字典树学习一:原理解析
Trie树基于Java的一种简单实现, 上代码。
1. 定义节点类TrieNode
/**
* TrieNode 节点类
* @author Konrad created on 2017/10/28
*/
public class TrieNode {
private LinkedList<TrieNode> children; //子节点
private char data; //节点字符
private int freq; //频率
boolean isEnd; //是否为叶子节点 public TrieNode(char data){
this.children = new LinkedList<>();
this.freq = 0;
this.isEnd = false;
this.data = data;
} public TrieNode childNode(char c){
if(null != children){
for(TrieNode child : children){
if(child.getData() == c){
return child;
}
}
}
return null;
} public LinkedList<TrieNode> getChildren() {
return children;
} public void setChildren(LinkedList<TrieNode> children) {
this.children = children;
} public char getData() {
return data;
} public void setData(char data) {
this.data = data;
} public int getFreq() {
return freq;
} public void setFreq(int freq) {
this.freq = freq;
} public void addFreq(int step){
this.freq += step;
} public void subFreq(int step){
this.freq -= step;
} public boolean isEnd() {
return isEnd;
} public void setEnd(boolean end) {
isEnd = end;
}
}
2. 定义Trie树类TrieTree
/**
* TrieTree Trie树类
*
* @author Konrad created on 2017/10/28
*/
public class TrieTree {
private TrieNode root; public TrieTree() {
this.root = new TrieNode(' ');
} //查找是否存在
public boolean search(String word) {...} //查找返回节点
public TrieNode searchNode(String word) {...} //插入
public void insert(String word) {...} //移除
public void remove(String word) {...} //获取词频
public int getFreq(String word) {...}
}
3. 插入节点
public void insert(String word) {
TrieNode current = root;
for (int i = 0; i < word.length(); i++) {
TrieNode child = current.childNode(word.charAt(i));
if (null != child)
current = child;
else {
current.getChildren().add(new TrieNode(word.charAt(i)));
current = current.childNode(word.charAt(i));
}
current.addFreq(1);
}
current.setEnd(true);
}
4.查找节点
//查找是否存在
public boolean search(String word) {
TrieNode current = root;
for (int i = 0; i < word.length(); i++) {
if (null == current.childNode(word.charAt(i)))
return false;
else
current = current.childNode(word.charAt(i));
} if (current.isEnd())
return true;
else
return false;
} //查找返回节点
public TrieNode searchNode(String word) {
TrieNode current = root;
for (int i = 0; i < word.length(); i++) {
if (null == current.childNode(word.charAt(i)))
return null;
else
current = current.childNode(word.charAt(i));
} if (current.isEnd())
return current;
else
return null;
}
5.删除节点
//移除
public void remove(String word) {
if (!search(word))
return; TrieNode current = root;
for (char c : word.toCharArray()) {
TrieNode child = current.childNode(c);
if (child.getFreq() == 1) {
current.getChildren().remove(child);
return;
}else{
child.subFreq(1);
current = child;
}
}
current.setEnd(false);
}
6.获取某个词的频率
//获取词频
public int getFreq(String word) {
TrieNode trieNode = searchNode(word);
if(null != trieNode){
return trieNode.getFreq();
}else{
return 0;
}
}
7.代码测试
/**
* @author Konrad created on 2017/10/28
*/
public class TrieTreeDemo {
public static void main(String[] args) {
String sentence = "today is good day what is you name at facebook how do you do what are you doing here";
TrieTree trieTree = new TrieTree();
for (String str : sentence.split(" ")) {
trieTree.insert(str); //插入节点,构建Trie树
} //判断是否存在
System.out.println("Does the word 'facebook' exists: " + trieTree.search("facebook"));
//统计do的词频
System.out.println("Frequent of the word 'you': " + trieTree.getFreq("do")); //移除today
System.out.println("Does the word 'today' exists - before remove: " + trieTree.search("today"));
trieTree.remove("today");
System.out.println("Does the word 'today' exists - after remove: " + trieTree.search("today"));
}
}
输出结果:

这只是Trie树的实现方法之一,也可以使用其他不同方式来实现。
标准Trie字典树学习二:Java实现方式之一的更多相关文章
- 标准Trie字典树学习一:原理解析
特别声明: 博文主要是学习过程中的知识整理,以便之后的查阅回顾.部分内容来源于网络(如有摘录未标注请指出).内容如有差错,也欢迎指正! 系列文章: 1. 字典树Trie学习一:原理解析 2.字典树Tr ...
- 萌新笔记——C++里创建 Trie字典树(中文词典)(一)(插入、遍历)
萌新做词典第一篇,做得不好,还请指正,谢谢大佬! 写了一个词典,用到了Trie字典树. 写这个词典的目的,一个是为了压缩一些数据,另一个是为了尝试搜索提示,就像在谷歌搜索的时候,打出某个关键字,会提示 ...
- 算法导论:Trie字典树
1. 概述 Trie树,又称字典树,单词查找树或者前缀树,是一种用于快速检索的多叉树结构,如英文字母的字典树是一个26叉树,数字的字典树是一个10叉树. Trie一词来自retrieve,发音为/tr ...
- C++里创建 Trie字典树(中文词典)(一)(插入、遍历)
萌新做词典第一篇,做得不好,还请指正,谢谢大佬! 写了一个词典,用到了Trie字典树. 写这个词典的目的,一个是为了压缩一些数据,另一个是为了尝试搜索提示,就像在谷歌搜索的时候,打出某个关键字,会提示 ...
- Trie字典树 动态内存
Trie字典树 #include "stdio.h" #include "iostream" #include "malloc.h" #in ...
- 817E. Choosing The Commander trie字典树
LINK 题意:现有3种操作 加入一个值,删除一个值,询问pi^x<k的个数 思路:很像以前lightoj上写过的01异或的字典树,用字典树维护数求异或值即可 /** @Date : 2017- ...
- 数据结构 -- Trie字典树
简介 字典树:又称单词查找树,Trie树,是一种树形结构,是一种哈希树的变种. 优点:利用字符串的公共前缀来减少查询时间,最大限度地减少无谓的字符串比较,查询效率比哈希树高. 性质: 1. 根节 ...
- 踹树(Trie 字典树)
Trie 字典树 ~~ 比 KMP 简单多了,无脑子选手学不会KMP,不会结论题~~ 自己懒得造图了OI WIKI 真棒 字典树大概长这么个亚子 呕吼真棒 就是将读进去的字符串根据当前的字符是什么和所 ...
- Trie字典树的学习及理解
字典树详解见此 我这里学习时主要是看了李煜东的进阶指南里的讲解,以下是书中介绍的内容. Trie,又称字典树,是一种用于实现字符串快速检索的多叉树结构,Tire的每个节点都拥有若干个字符指针,若在插入 ...
随机推荐
- Delphi XE7的安卓程序如何调用JAVA的JAR,使用JAVA的类?
本文使用工具和全部源码下载: http://download.csdn.net/detail/sunylat/8190765 为什么我们要在Delphi XE7的安卓程序调用JAVA的JAR,使用JA ...
- c++11新特性总结(转。加上重点)
1.类型与变量相关 1.1.nullptr: 取代了NULL,专用于空指针 1.2.constexpr: 近似const, 可以修饰变量,也可以修饰函数, 修饰变量如: const int globa ...
- 深浅copy和浅copy
深浅copy 1,先看赋值运算. l1 = [1,2,3,['barry','alex']] l2 = l1 l1[0] = 111 print(l1) # [111, 2, 3, ['barry', ...
- Java中运算符“|”和“||”以及“&”和“&&”区别
1.“|”运算符:不论运算符左侧为true还是false,右侧语句都会进行判断,下面代码 int a =1,b=1; if(a++ == 1 | ++b == 2) System.out.printl ...
- acedSSGet 翻译
ObjectARX 参考指南 > 全局函数 > AcEd 全局函数 > acedSSGet 函数 acedSSGet 折叠全部 C++ int acedSSGet( const AC ...
- 初探APT 攻击
作者:joe 所属团队:Arctic Shell 本文编写参考: https://www.freebuf.com/vuls/175280.html https://www.freebuf. ...
- [Linux] 如何在 Linux 中提取随机数
如何在 Linux 中提取随机数 一.设备文件 /dev/random & /dev/urandom 字符特殊文件 /dev/random 和 /dev/urandom (存在于Linux 1 ...
- 使用python 模仿mybinlog 命令 二进制分析mysql binlog
出处:黑洞中的奇点 的博客 http://www.cnblogs.com/kelvin19840813/ 您的支持是对博主最大的鼓励,感谢您的认真阅读.本文版权归作者所有,欢迎转载,但请保留该声明. ...
- Mac下使用Wine安装Xshell 4和Xftp 4
下载: (链接: https://pan.baidu.com/s/1o78qisM 密码: 79sq) 安装: 1.安装Wine 参考:http://www.cnblogs.com/EasonJim/ ...
- RHCE 共享文件系统
9.1 共享文件系统 概述: 共享文件系统通常有两种方式: 基于文件共享,一种直接共享文件夹给client端,常见的技术有NFS(Network File System )和 SMB (Server ...