特别声明:

  博文主要是学习过程中的知识整理,以便之后的查阅回顾。部分内容来源于网络(如有摘录未标注请指出)。内容如有差错,也欢迎指正!

系列文章:

1. 标准Trie字典树学习一:原理解析

2.标准Trie字典树学习二:Java实现方式之一

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实现方式之一的更多相关文章

  1. 标准Trie字典树学习一:原理解析

    特别声明: 博文主要是学习过程中的知识整理,以便之后的查阅回顾.部分内容来源于网络(如有摘录未标注请指出).内容如有差错,也欢迎指正! 系列文章: 1. 字典树Trie学习一:原理解析 2.字典树Tr ...

  2. 萌新笔记——C++里创建 Trie字典树(中文词典)(一)(插入、遍历)

    萌新做词典第一篇,做得不好,还请指正,谢谢大佬! 写了一个词典,用到了Trie字典树. 写这个词典的目的,一个是为了压缩一些数据,另一个是为了尝试搜索提示,就像在谷歌搜索的时候,打出某个关键字,会提示 ...

  3. 算法导论:Trie字典树

    1. 概述 Trie树,又称字典树,单词查找树或者前缀树,是一种用于快速检索的多叉树结构,如英文字母的字典树是一个26叉树,数字的字典树是一个10叉树. Trie一词来自retrieve,发音为/tr ...

  4. C++里创建 Trie字典树(中文词典)(一)(插入、遍历)

    萌新做词典第一篇,做得不好,还请指正,谢谢大佬! 写了一个词典,用到了Trie字典树. 写这个词典的目的,一个是为了压缩一些数据,另一个是为了尝试搜索提示,就像在谷歌搜索的时候,打出某个关键字,会提示 ...

  5. Trie字典树 动态内存

    Trie字典树 #include "stdio.h" #include "iostream" #include "malloc.h" #in ...

  6. 817E. Choosing The Commander trie字典树

    LINK 题意:现有3种操作 加入一个值,删除一个值,询问pi^x<k的个数 思路:很像以前lightoj上写过的01异或的字典树,用字典树维护数求异或值即可 /** @Date : 2017- ...

  7. 数据结构 -- Trie字典树

    简介 字典树:又称单词查找树,Trie树,是一种树形结构,是一种哈希树的变种. 优点:利用字符串的公共前缀来减少查询时间,最大限度地减少无谓的字符串比较,查询效率比哈希树高. 性质:   1.  根节 ...

  8. 踹树(Trie 字典树)

    Trie 字典树 ~~ 比 KMP 简单多了,无脑子选手学不会KMP,不会结论题~~ 自己懒得造图了OI WIKI 真棒 字典树大概长这么个亚子 呕吼真棒 就是将读进去的字符串根据当前的字符是什么和所 ...

  9. Trie字典树的学习及理解

    字典树详解见此 我这里学习时主要是看了李煜东的进阶指南里的讲解,以下是书中介绍的内容. Trie,又称字典树,是一种用于实现字符串快速检索的多叉树结构,Tire的每个节点都拥有若干个字符指针,若在插入 ...

随机推荐

  1. 2、Orcal数据库创建第一个(管理员)连接

    (注意这里第一个创建的是管理员连接也是我们的总连接,之后我们所有的其他新用户都要创建在它里面,所以它的一些属性我们在填写以及设置时需要注意!!!) 1.确认Orcal服务开启: 2.创建连接: 打开我 ...

  2. “全栈2019”Java异常第二十二章:try-with-resources语句详解

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java异 ...

  3. “全栈2019”Java异常第七章:try-catch-finally组合方式

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java异 ...

  4. SpringMVC--视图

    本章简介 视图(View)和视图解析器(ViewResolver)的工作流程: 当请求处理方法处理完请求之后,会返回String.ModelAndView或View对象,如return “succes ...

  5. Mac上使用oh-my-zsh+iterm2

    一.安装oh-my-zsh插件 1.1 下载 git clone git://github.com/robbyrussell/oh-my-zsh.git ~/.oh-my-zsh 1.2创建新配置 如 ...

  6. 关于CentOS-6的默认带的mysql启动和安装问题

    http://blog.csdn.net/arrowzz/article/details/24439731 以下纯复制粘贴: 一开始想自己一步一步从编译开始搭建一个lanmp环境: 从鸟哥的linux ...

  7. 极其简单的用JS在浏览器中创建下载文件的方法

    有这样一个需求,在js中动态创建一个页面,然后下载该页面为word文档,研究了一上午,最后发现实现起来如此简单. 在js中创建如下方法:(直接复制即可) function downloadFile(f ...

  8. 题目1006:ZOJ问题(字符串处理)

    问题来源 http://ac.jobdu.com/problem.php?pid=1006 问题描述 输入一个只包含'z','o','j'三种字符的字符串,判断是否符合要求. 问题分析 分析AC的三个 ...

  9. 【文档】二、Binlog结构和内容概述

    binlog是一系列文件,这些文件包含了Mysql服务实例中数据的变化. binlog包含一系列二进制日志文件,还包含一个索引文件. 每个日志文件包含了一个4字节的魔法数,后面跟着描述数据变化的事件内 ...

  10. java的SSH的baseDao,巧用泛型

    BaseDao接口: import java.util.List; public interface BaseDao<T,PK> { public void add(T t); publi ...