字典树经常用于单词搜索,现在网络引擎中也应用了trie树;

public class Trie{
private int SIZE = 26;
private TrieNode root;
Trie(){
root = new TrieNode();
}
private class TrieNode{
private int num;//the times that words passing this node
private TrieNode[] son;//son point
private boolean isEnd;//record weather there are points ending in this point
private char val;//char value
public TrieNode(){
num = 1;
son = new TrieNode[SIZE];
isEnd = false;
}
}
//create the trie tree
public void insert(String str){//insert a word to the trie tree
if(str == null||str.length() == 0){
return;
}
TrieNode node = root;
char[] letters = str.toCharArray();
for(int i =0,len = str.length();i<len;i++){
int pos = letters[i]-'a';
if(node.son[pos]==null){
node.son[pos] = new TrieNode();
node.son[pos].val = letters[i];
}else{
node.son[pos].num++;
}
node = node.son[pos];
}
node.isEnd= true;//
}
//calculate the count of words'prefix
public int countPrefix(String prefix){
if(prefix == null||prefix.length()==0){
return -1;
}
TrieNode node = root;
char[] letters = prefix.toCharArray();
int pos;
int num = 0;
for(int i=0,len = prefix.length();i<len;i++){
pos = letters[i]-'a';
node = node.son[pos];
if(node==null){
return 0;
}else{
num = node.num;
}
}
return num;
}
//print the word having the assigned prefix
public String hasprefix(String prefix){
if(prefix ==null||prefix.length()==0){
return null;
}
TrieNode node = root;
char[] letters = prefix.toCharArray();
int pos;
for(int i=0,len = prefix.length();i<len; i++){
pos = letters[i]-'a';
if(node.son[pos]==null){
return null;
}else{
node = node.son[pos];
}
}
preTraverse(node,prefix);
return null;
}
//walk the node's words
public void preTraverse(TrieNode node,String prefix){
if(!node.isEnd){
for(TrieNode child:node.son){
if(child!=null){
preTraverse(child,prefix+child.val);
}
}
return;
}
System.out.println(prefix);
}
//find the completely matching word
public boolean has(String str){
if(str == null||str.length() == 0){
return false;
}
TrieNode node = root;
char[] letters = str.toCharArray();
int pos;
for(int i=0,len = str.length();i<len;i++){
pos = letters[i]-'a';
if(node.son[pos]==null){
return false;
}else{
node = node.son[pos];
}
}
return node.isEnd;
}
//pre-order of the trie tree
public void preTraverse(TrieNode node){
if(node!=null){
System.out.print(node.val+" ");
for(TrieNode child:node.son){
preTraverse(child);
}
}
}
public TrieNode getRoot(){
return this.root;
}
public static void main(String[] args){
Trie tree = new Trie();
String[] strs = {"dsfahjk","fjdkafhdask","fdhjfk","dafjdk","qepoi","fda"};
String[] prefix={"ds","f","qepo","abd"};
for(String str:strs){
tree.insert(str);
}
for(String pre:prefix){
int num = tree.countPrefix(pre);
System.out.println(pre+" "+num);
}
tree.preTraverse(tree.getRoot());
System.out.println("\ntree.has(\"f\"):"+tree.has("f"));
System.out.println("tree.has(\"fda\"):"+tree.has("fda"));
tree.hasprefix("f");
System.out.println("countprefix(\"f\"):"+tree.countPrefix("f"));
}
}

字典树trie的更多相关文章

  1. [POJ] #1002# 487-3279 : 桶排序/字典树(Trie树)/快速排序

    一. 题目 487-3279 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 274040   Accepted: 48891 ...

  2. 『字典树 trie』

    字典树 (trie) 字典树,又名\(trie\)树,是一种用于实现字符串快速检索的树形数据结构.核心思想为利用若干字符串的公共前缀来节约储存空间以及实现快速检索. \(trie\)树可以在\(O(( ...

  3. 字典树trie学习

    字典树trie的思想就是利用节点来记录单词,这样重复的单词可以很快速统计,单词也可以快速的索引.缺点是内存消耗大 http://blog.csdn.net/chenleixing/article/de ...

  4. 字典树(Trie)详解

    详解字典树(Trie) 本篇随笔简单讲解一下信息学奥林匹克竞赛中的较为常用的数据结构--字典树.字典树也叫Trie树.前缀树.顾名思义,它是一种针对字符串进行维护的数据结构.并且,它的用途超级广泛.建 ...

  5. 字典树(Trie Tree)

    在图示中,键标注在节点中,值标注在节点之下.每一个完整的英文单词对应一个特定的整数.Trie 可以看作是一个确定有限状态自动机,尽管边上的符号一般是隐含在分支的顺序中的.键不需要被显式地保存在节点中. ...

  6. 字典树(Trie树)实现与应用

    一.概述 1.基本概念 字典树,又称为单词查找树,Tire数,是一种树形结构,它是一种哈希树的变种. 2.基本性质 根节点不包含字符,除根节点外的每一个子节点都包含一个字符 从根节点到某一节点.路径上 ...

  7. 字典树(Trie树)的实现及应用

    >>字典树的概念 Trie树,又称字典树,单词查找树或者前缀树,是一种用于快速检索的多叉树结构,如英文字母的字典树是一个26叉树,数字的字典树是一个10叉树.与二叉查找树不同,Trie树的 ...

  8. 字典树trie的学习与练习题

    博客详解: http://www.cnblogs.com/huangxincheng/archive/2012/11/25/2788268.html http://eriol.iteye.com/bl ...

  9. [转载]字典树(trie树)、后缀树

    (1)字典树(Trie树) Trie是个简单但实用的数据结构,通常用于实现字典查询.我们做即时响应用户输入的AJAX搜索框时,就是Trie开始.本质上,Trie是一颗存储多个字符串的树.相邻节点间的边 ...

  10. Codevs 4189 字典(字典树Trie)

    4189 字典 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 大师 Master 传送门 题目描述 Description 最经,skyzhong得到了一本好厉害的字典,这个字典里 ...

随机推荐

  1. DAY10-万物皆对象-2018-2-2

    许久没有写了,虽然每天都有在学,但是学的东西也少了,后面难度慢慢加大,学习速度也是变慢了.这是许多天积累下来的笔记,从第一次接触对象,到慢慢去了解,现在处于还待深入了解的状态.万物皆对象,那是不是说没 ...

  2. 微信开发获取media_id错误码汇总

    微信开发遇到的错误汇总: 1. 错误代码40001 "errcode": 40001,    "errmsg": "invalid credentia ...

  3. tomcat调优(三)

    标签: linux 笔者Q:972581034 交流群:605799367.有任何疑问可与笔者或加群交流 1.安全优化 降权启动 telnet管理端口保护 ajp连接端口保护 禁用管理端 关闭本地默认 ...

  4. form表单中get和post两种提交方式的区别

    一.form表单中get和post两种提交方式的区别? 1.get提交表单中的内容在链接处是可见的.post不可见 2.post相比于get是安全的 3.post不收限制大小,get有限制大小(黑马视 ...

  5. HTML中的表单

    1.HTML中表单元素的基本概念 HTML表单是HTML元素中较为复杂的部分,表单往往和脚本,动态页面,数据处理等功能相结合,因此是制作动态网站很重要的内容. 表单一般用来收集用户的输入信息 2.表单 ...

  6. 代理(Proxy)模式

    代理模式的类图如下所示: 客户端想调用的是RealSubject,由于某种考虑或原因,只能直接访问到ProxySubject,再由ProxySubject去调用RealSubject,这就完成了一次代 ...

  7. Spring整合JMS(二)——三种消息监听器

    原文地址:http://haohaoxuexi.iteye.com/blog/1893676 1.3     消息监听器MessageListener 在Spring整合JMS的应用中我们在定义消息监 ...

  8. 洛谷 [P3973] 线性代数

    最大权闭合子图,神题 这不是线性代数,这是网络流. 我们看见这是一堆矩阵的运算,而且最后变成了一个数,那么我们就想到,把这个矩阵乘法的过程用具体的数字推出来 我们发现,a是一个01矩阵,然后其实就可以 ...

  9. http协议重点

    https://www.cnblogs.com/ranyonsue/p/5984001.html HTTP简介 HTTP协议是Hyper Text Transfer Protocol(超文本传输协议) ...

  10. Retrofit 实践

    Retrofit是一套RESTful架构的Android(Java)客户端实现,基于注解,提供JSON to POJO(Plain Ordinary Java Object,简单Java对象),POJ ...