字典树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得到了一本好厉害的字典,这个字典里 ...
随机推荐
- 转发:python 装饰器--这篇文章讲的通俗易懂
转 http://www.cnblogs.com/wupeiqi/articles/4980620.html 1.必备 #### 第一波 #### def foo(): print 'foo' ...
- DOS、Mac 和 Unix 文件格式[转]
DOS.Mac 和 Unix 文件格式 相信很多朋友都碰到过这三种文件格式的互换问题,今日又碰到这个问题,忽然想寻根问底,于是整理了本文档. 文件格式区别 我们先看看这三个家伙有啥区别.很久以前, ...
- js禁止开发者工具
$(document).keydown(function() { return key(arguments[0]) }); function key(e) { //f12 var keynum; if ...
- Tomcat常用操作
Tomcat简介 TOMCAT是APACHE JAKARTA软件组织的一个子项目,TOMCAT是一个JSP/SERVLET容器,它是在SUN公司的JSWDK(JAVA SERVER WEB DEVEL ...
- MySQL查询结果复制到新表(更新、插入)
MySQL中可以将查询结果复制到另外的一张表中,复制的话通常有两种情况,一种是更新已有的数据,另一种是插入一条新记录.下面通过例子来说明.首先构建两个测试表. 表t1: 表t2: 1.如果t2表中存在 ...
- oracle如何操作约束
自我总结,欢迎怕转! 约束的作用不多说了,直接看怎么用吧. 非空约束: 在创建表时设置非空约束: CREATE TABLE table_name( column_name datatype NOT N ...
- 【Java】泛型学习笔记
参考书籍 <Java核心技术:卷1> 泛型, 先睹为快 先通过一个简单的例子说明下Java中泛型的用法: 泛型的基本形式类似于模板, 通过一个类型参数T, 你可以"私人定制&qu ...
- 12、SEO工程师指南 - 软件项目角色指南系列文章
第11章 SEO工程师 SEO工程师是一个比较新兴的职位,在实际的项目管理过程中,SEO工程师的地位相对靠后,只有在项目试运行以及运营期间才能体现出SEO工程师的作用.在项目完成之后, ...
- Oracle 12cR1 RAC 在VMware Workstation上安装(下)—静默安装
Oracle 12cR1 RAC 在VMware Workstation上安装(下)—静默安装 1.1 静默安装 1.1.1 静默安装grid 安装之前使用脚本进行校验,确保所有的failed选项 ...
- 洛谷 [P3384] 树链剖分 模版
支持各种数据结构上树,注意取膜. #include <iostream> #include <cstring> #include <algorithm> #incl ...