标准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的每个节点都拥有若干个字符指针,若在插入 ...
随机推荐
- python---基本数据类型 dict(字典)
1. 什么是字典 字典是python中唯一的映射类型, 由{ } 括起来的键值对组成,在dict中key是唯一的.字典是以key:value的形式来保存数据, 字典存储数据的时候是用的hash值来存储 ...
- Kali Linux来袭~老司机带你进击
Kali是BackTrackLinux完全遵循Debian开发标准彻底的完全重建.全新的目录框架,复查并打包所有工具,我们还为VCS建立了Git树. 本次推荐内容主要介绍Kali-Linux的安装,包 ...
- 在Myeclipse中查看android源码就是这么easy
在开发android 时不能查看源码必是很不爽的一件事,看过网上一些文章后(都是2.0以前的版本,跟我的2.2最新版本的配置是不一样的)不过还是给了我启示,通过配置终于可以在myeclipse中查看源 ...
- iOS --UIScrollView的学习(二)
1.接着上一次的说:http://www.cnblogs.com/fengzhihao/p/5287734.html,这次讲一下UISCrollView的缩放功能. 2.当用户在UIScrollVie ...
- linux下启动tomcat服务的命令是什么
Linux下tomcat服务的启动.关闭与错误跟踪,使用PuTTy远程连接到服务器以后,通常通过以下几种方式启动关闭tomcat服务:切换到tomcat主目录下的bin目录(cd usr/local/ ...
- 【性能测试】:LR中解决接口请求中包含中文字符,服务器不识别的问题
在LR中,直接写的接口请求,如果请求字段包含中文字段,服务器会不识别,这个时候就要用到lr_convert_string_encoding这个函数: 具体用法: lr_convert_string_e ...
- OAuth2.0认证和授权以及单点登录
https://www.cnblogs.com/shizhiyi/p/7754721.html OAuth2.0认证和授权机制讲解 2017-10-30 15:33 by shizhiyi, 2273 ...
- Acronis
关于这个神奇的东西也没少折腾了我,这里是它的家:http://www.acronis.com/zh-cn/ 网上也看了一些,没有头绪,总之给我的感觉就是不明觉厉.这里小结自己的学到的一些东西,算是一整 ...
- html网页如何传递接收地址参数?
实现html页面的参数传递 方法一: 下面是javascrīpt的一种实现方法, 这个函数是通过window.location.href中的分割符获得各个参数. 有了这个函数,就可以在页面之间传递参数 ...
- 释放linux端口
感谢作者的共享,在此表示感谢 有时候关闭软件后,后台进程死掉,导致端口被占用.下面以TOMCAT端口8060被占用为例,列出详细解决过程. 解决方法: 1.查找被占用的端口 netstat -tln ...