字典树trie
字典树经常用于单词搜索,现在网络引擎中也应用了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的更多相关文章
- [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的思想就是利用节点来记录单词,这样重复的单词可以很快速统计,单词也可以快速的索引.缺点是内存消耗大 http://blog.csdn.net/chenleixing/article/de ...
- 字典树(Trie)详解
详解字典树(Trie) 本篇随笔简单讲解一下信息学奥林匹克竞赛中的较为常用的数据结构--字典树.字典树也叫Trie树.前缀树.顾名思义,它是一种针对字符串进行维护的数据结构.并且,它的用途超级广泛.建 ...
- 字典树(Trie Tree)
在图示中,键标注在节点中,值标注在节点之下.每一个完整的英文单词对应一个特定的整数.Trie 可以看作是一个确定有限状态自动机,尽管边上的符号一般是隐含在分支的顺序中的.键不需要被显式地保存在节点中. ...
- 字典树(Trie树)实现与应用
一.概述 1.基本概念 字典树,又称为单词查找树,Tire数,是一种树形结构,它是一种哈希树的变种. 2.基本性质 根节点不包含字符,除根节点外的每一个子节点都包含一个字符 从根节点到某一节点.路径上 ...
- 字典树(Trie树)的实现及应用
>>字典树的概念 Trie树,又称字典树,单词查找树或者前缀树,是一种用于快速检索的多叉树结构,如英文字母的字典树是一个26叉树,数字的字典树是一个10叉树.与二叉查找树不同,Trie树的 ...
- 字典树trie的学习与练习题
博客详解: http://www.cnblogs.com/huangxincheng/archive/2012/11/25/2788268.html http://eriol.iteye.com/bl ...
- [转载]字典树(trie树)、后缀树
(1)字典树(Trie树) Trie是个简单但实用的数据结构,通常用于实现字典查询.我们做即时响应用户输入的AJAX搜索框时,就是Trie开始.本质上,Trie是一颗存储多个字符串的树.相邻节点间的边 ...
- Codevs 4189 字典(字典树Trie)
4189 字典 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 大师 Master 传送门 题目描述 Description 最经,skyzhong得到了一本好厉害的字典,这个字典里 ...
随机推荐
- zabbix自动清理30天前的数据
转:http://www.361way.com/delete-zabbix-histroy-data/3826.html zabbix属于一个细度化的监控工具,其入库数据随着细度的增加相应的入库数据量 ...
- 洛谷P1233 [木棍加工]
主要思路: 这道题一眼看过去就可以贪心.. 首先可以按L排序.. 显然排序之后我们就可以抛开L不管了.. 然后就可以愉快的贪心了.. 细节: 这道题可以看成用 最少的合法序列(详见原题) 装下所有木棍 ...
- Request.getparameternames 获取form表单里面所有的请求参数 。 返回一个Enumeration类型的枚举.
通过Enumeration的hasMoreElements()方法遍历.再由nextElement()方法获得枚举的值.此时的值是form表单中所有控件的name属性的值. 最后通过request.g ...
- JDBC (二)
1 使用JDBC进行批处理 当需要向数据库发送一批SQL语句的时候,应该避免向数据库一条条的发送执行,而应该采用JDBC的批处理机制,以提高执行效率. 实现批处理的方式一: Statement.add ...
- js控制滚动条滑动
window.scrollTo(0,document.body.scrollHeight);或者通过设置Location的hash属性参见:http://www.cnblogs.com/oospace ...
- php中curl模拟post提交多维数组(转载)
原文地址:http://www.cnblogs.com/mingaixin/archive/2012/11/09/2763265.html 今天需要用curl模拟post提交参数,请求同事提供的一个接 ...
- awk grep sed cut学习
awk学习网站 grep sed cut
- Linux时间转标准时间
[root@nhserver2 ~]# date -d '1970-1-1 0:0:0 GMT + 1394592071 seconds'Wed Mar 12 10:41:11 CST 2014
- Docker之镜像
镜像(Images) 镜像是Docker的三大核心之一,类似于虚拟机,作用和虚拟机是一样的,唯独是组成部分会有些区别.简单的说如果我们想启动一个容器就必须要有镜像.docker运行容器前需要本地存在对 ...
- HDU 4333 Revolving Digits
扩展KMP的应用 我们发现本题的关键在于如何高效的判断两个同构字符串的大小关系,想到如果能够预处理出每一个同构字符串与原字符串的最长公共前缀,那么直接比较它们不一样的部分就好,扩展KMP正好是用来处理 ...