字典树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得到了一本好厉害的字典,这个字典里 ...
随机推荐
- Android ui 透明度设置
格式如#00FFFFFF,前两位代表不透明度的十六进制.00表示完全透明,FF就是全不透明.依次递增. <?xml version="1.0" encoding=" ...
- linux下^M问题
^M的原因 Dos.Windows 格式的文件,用 0D 0A (CR+LF)作为换行符 而Unix 的则是以0A(LF) 作为换行符 所以dos 底下的文本文件到了unix的话,换行符就会多出来一个 ...
- Git多帐号配置,管理多个SSH
查看自己所有的SSH-Key $ cd ~/.ssh $ ls id_rsa id_rsa.pub known_hosts 如果你已经创建过git账号那你可能和我一样会看到只有一个SSH-Key, 这 ...
- cache.config文件配置模板
# # cache.config # # The purpose of this file is to alter caching parameters of # specific objects o ...
- 使用jdbc存储图片和大文本
package cn.itcast.i_batch; import java.sql.Connection; import java.sql.PreparedStatement; import jav ...
- JAVA并发编程学习笔记------FutureTask
FutureTask是Future和Callable的结合体.传统的代码是这样写的Future f = executor.submit(new Callable()); 然后通过Future来取得计算 ...
- http目录显示时间与服务器相差8小时
一直用nginx做http服务,代码里访问过文件地址,并未认真关注过访问http目录下的时间戳.今天浏览文件的时候发现一个问题.web上显示的文件时间戳与服务器时间相比差8个小时.具体表现看下图: w ...
- dubbo 线程池
在dubbo调用过程中被调用方有两个线程池:io线程池,业务线程池. 这也是dubbo调优的点. 配置信息: <dubbo:protocol name="dubbo" dis ...
- console引起的eclipse 僵死/假死 问题排查及解决[转]
原文链接:http://www.iteye.com/topic/1133941 症状: 使用Eclipse win 64位版本,indigo及kepler都重现了,使用tomcat 6.0.39,jd ...
- JS中的内置对象简介与简单的属性方法
JS中的数组: 1.数组的概念: 数组是在内存中连续存储的多个有序元素的结构,元素的顺序称为下标,通过下标查找对应元素 2.数组的声明: ①通过字面量声明var arr1 = [,,,,] JS中同一 ...