字典树trie学习
字典树trie的思想就是利用节点来记录单词,这样重复的单词可以很快速统计,单词也可以快速的索引。缺点是内存消耗大
http://blog.csdn.net/chenleixing/article/details/44708533 这个是学习资料来源。
附上个人代码实践操作
package ShuJujieGou;
import javax.swing.tree.TreeNode;
public class Tries {
private int deepLength;
private int nodeNumber;
private final int SIZE = 26;
private treeNode root;
public Tries() {
deepLength = 0;
nodeNumber = 0;
root = new treeNode();
}
private class treeNode {
public int passNumeber;// 从这里进过的节点单词数量
private int endNumber;// 相同单词有几个
private boolean isEnd;// 是否结束
private treeNode[] son;
private char word;
public treeNode() {
this.passNumeber = 0;
this.endNumber = 0;
this.isEnd = false;
son = new treeNode[SIZE];
}
}
private boolean isWord(String str) {
if (str == null || !str.matches("[0-9a-zA-Z]*")) {
return false;
}
return true;
}
public void insertWord(String str) {
if (!isWord(str)) {
System.out.println("请输入正确字符");
return;
}
treeNode node = this.root;
String s = str.toLowerCase();
char[] word = s.toCharArray();
for (char c : word) {
int cn = c - 'a';
if (node.son[cn] == null) {
node.son[cn] = new treeNode();
node=node.son[cn];
node.word = c;
this.nodeNumber++;
node.passNumeber=1;
} else {
node = node.son[cn];
node.passNumeber++;
}
}
node.isEnd = true;
node.endNumber++;
if (this.deepLength < word.length) {
this.deepLength = word.length;
}
}
public void sayMyself() {
System.out.println("length:" + this.deepLength);
System.out.println("nodenumber:" + this.nodeNumber);
}
public void getWord(treeNode node,String str){
for (int x=0;x<SIZE;x++){
if(!(node.son[x]==null)){
String s= str+node.son[x].word;
getWord(node.son[x],s);
if(node.son[x].isEnd){
System.out.println("单词:"+s);
}
}
}
}
public void getAllWord(){//这里添加上面文章没有的把所有单词全部索引出来
String s = "";
getWord(root,s);
}
}
//测试代码
package ShuJujieGou;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Tries tree = new Tries();
tree.insertWord("car");
tree.insertWord("bus");
tree.insertWord("chicken");
tree.insertWord("cook");
tree.insertWord("rock");
tree.insertWord("hard");
tree.insertWord("good");
tree.insertWord("bad");
tree.sayMyself();
tree.getAllWord();
}
}
/*
输出
length:7
nodenumber:29
单词:bad
单词:bus
单词:car
单词:chicken
单词:cook
单词:good
单词:hard
单词:rock
*/
字典树trie学习的更多相关文章
- 字典树(Trie)学习笔记
目录 什么是字典树 如何存储字典树 如何查找字符串有没有出现 第一个图的那种线段树 应用 例题 1.统计难题 2.P2580 于是他错误的点名开始了 什么是字典树 上图来自luogu题解 这是一种字典 ...
- [POJ] #1002# 487-3279 : 桶排序/字典树(Trie树)/快速排序
一. 题目 487-3279 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 274040 Accepted: 48891 ...
- 『字典树 trie』
字典树 (trie) 字典树,又名\(trie\)树,是一种用于实现字符串快速检索的树形数据结构.核心思想为利用若干字符串的公共前缀来节约储存空间以及实现快速检索. \(trie\)树可以在\(O(( ...
- 字典树(Trie)详解
详解字典树(Trie) 本篇随笔简单讲解一下信息学奥林匹克竞赛中的较为常用的数据结构--字典树.字典树也叫Trie树.前缀树.顾名思义,它是一种针对字符串进行维护的数据结构.并且,它的用途超级广泛.建 ...
- Trie字典树的学习及理解
字典树详解见此 我这里学习时主要是看了李煜东的进阶指南里的讲解,以下是书中介绍的内容. Trie,又称字典树,是一种用于实现字符串快速检索的多叉树结构,Tire的每个节点都拥有若干个字符指针,若在插入 ...
- 字典树 trie树 学习
一字典树 字典树,又称单词查找树,Trie树,是一种树形结构,哈希表的一个变种 二.性质 根节点不包含字符,除根节点以外的每一个节点都只包含一个字符: 从根节点到某一节点,路径上经过的字符串连接起 ...
- 字典树trie的学习与练习题
博客详解: http://www.cnblogs.com/huangxincheng/archive/2012/11/25/2788268.html http://eriol.iteye.com/bl ...
- 字典树(Trie)的学习笔记
按照一本通往下学,学到吐血了... 例题1 字典树模板题吗. 先讲讲字典树: 给出代码(太简单了...)! #include<cstdio> #include<cstring> ...
- LeetCode 14. Longest Common Prefix字典树 trie树 学习之 公共前缀字符串
所有字符串的公共前缀最长字符串 特点:(1)公共所有字符串前缀 (好像跟没说一样...) (2)在字典树中特点:任意从根节点触发遇见第一个分支为止的字符集合即为目标串 参考问题:https://lee ...
随机推荐
- adb 转自github https://github.com/mzlogin/awesome-adb
基本用法 命令语法 adb 命令的基本语法如下: adb [-d|-e|-s <serialNumber>] <command> 如果只有一个设备/模拟器连接时,可以省略掉 [ ...
- Python基础之模块以及5大模块的使用
内容梗概: 1. 模块的简单认识 2. collections模块 3. time时间模块 4. random模块 5. os模块 6. sys模块 1.模块的简单认识定义:模块就是我们把装有特定功能 ...
- 46. 47. Permutations and Permutations II 都适用(Java,字典序 + 非字典序排列)
解析: 一:非字典序(回溯法) 1)将第一个元素依次与所有元素进行交换: 2)交换后,可看作两部分:第一个元素及其后面的元素: 3)后面的元素又可以看作一个待排列的数组,递归,当剩余的部分只剩一个元素 ...
- HTTP请求/响应报文结构
HTTP协议版本有两种:HTTP1.0和HTTP1.1 它们俩的区别在于:HTTP1.0对于每个连接都只能传送一个请求和响应,请求后就会关闭,HTTP1.0没有Host字段:而HTTP1.1在同一个连 ...
- Spring Cloud系列之Feign的常见问题总结
一.FeignClient接口,不能使用@GettingMapping 之类的组合注解 代码示例: @FeignClient("microservice-provider-user" ...
- Python函数式编程,map/reduce,filter和sorted
什么是函数式编程? 与面向对象编程(Object-oriented programming)和过程式编程(Procedural programming)并列的编程范式. 最主要的特征是,函数是第一等公 ...
- Linux -- 之HDFS实现自动切换HA(全新HDFS)
Linux -- 之HDFS实现自动切换HA(全新HDFS) JDK规划 1.7及以上 https://blog.csdn.net/meiLin_Ya/article/details/8065094 ...
- Lexicography
An anagram of a string is any string that can be formed using the same letters as the original. (We ...
- 【转】借助System.Linq.Dynamic, IQueryable根据排序字符串排序
在使用Entity Framework时,若有多个排序,需要OrderBy (OrderByDescending)再ThenBy (ThenByDescending) 假设需要根据Name升序排序,再 ...
- Linux Shell获取系统资源使用百分比(CentOS)
CPU使用率: top -b -n | | 内存使用率: free -m | grep '^-' | awk '{print $3/($3+$4)*100"%"}' IO使用率(F ...