import java.util.ArrayList;
import java.util.TreeMap; import util.FileOperation; public class Trie {
private class Node{
public boolean isWord;
public TreeMap<Character,Node> next; public Node(boolean isWord) {
this.isWord=isWord;
next=new TreeMap<>();
}
public Node() {
this(false);
}
}
private Node root;
private int size;
public Trie() {
root=new Node();
size=0;
} public int getSize() {
return size;
}
public void add(String word) { Node cur=root;
for(char c:word.toCharArray()) {
if(cur.next.get(c)==null)
cur.next.put(c, new Node());
cur=cur.next.get(c);
}
if(!cur.isWord) {
cur.isWord=true;
size++;
}
} public boolean find(String word) {
Node cur=root;
for(char c:word.toCharArray()) {
if(cur.next.get(c)==null)
return false;
cur=cur.next.get(c);
}
return cur.isWord;
} public boolean isPrefix(String word) {
Node cur=root;
for(char c:word.toCharArray()) {
if(cur.next.get(c)==null)
return false;
cur=cur.next.get(c);
}
return true;
} public boolean searchRegex(String word) {
return match(root,word,0);
}
private boolean match(Node node,String word,int index) {
if(index==word.length()) return node.isWord;
else {
char c=word.charAt(index);
if(c!='.') {
if(node.next.get(c)==null) return false;
return match(node.next.get(c), word, index+1);
}else {
for(char w:node.next.keySet()) {
if(match(node.next.get(w), word, index+1)) {
return true;
}
}
return false;
}
}
}
public static void main(String[] args) { System.out.println("Pride and Prejudice"); ArrayList<String> words = new ArrayList<>();
if(FileOperation.readFile("pride-and-prejudice.txt", words)){ long startTime = System.nanoTime();
long endTime = System.nanoTime();
// ---
startTime = System.nanoTime();
Trie trie = new Trie();
for(String word: words)
trie.add(word);
for(String word: words)
trie.find(word);
endTime = System.nanoTime(); double time = (endTime - startTime) / 1000000000.0; System.out.println("Total different words: " + trie.getSize());
System.out.println("Trie: " + time + " s");
} }
}

Trie树的简单实现的更多相关文章

  1. POJ 3630 Phone List(trie树的简单应用)

    题目链接:http://poj.org/problem?id=3630 题意:给你多个字符串,如果其中任意两个字符串满足一个是另一个的前缀,那么输出NO,否则输出YES 思路:简单的trie树应用,插 ...

  2. hdu 1251 统计难题(trie 树的简单应用)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1251 题意:给你多个字符串,求以某个字符串为前缀的字符串数量. 思路:简单的trie数应用,在trie ...

  3. 数据结构《16》----自动补齐实现《一》----Trie 树

    1. 简述 Trie 树是一种高效的字符串查找的数据结构.可用于搜索引擎中词频统计,自动补齐等. 在一个Trie 树中插入.查找某个单词的时间复杂度是 O(len), len是单词的长度. 如果采用平 ...

  4. trie树(前缀树)

    问题描述:   Trie树,即字典树,又称单词查找树或键树,是一种树形结构,是一种哈希树的变种.典型应用是用于统计和排序大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计.它的优 ...

  5. Trie树(字典树) 最热门的前N个搜索关键词

    方法介绍 1.1.什么是Trie树 Trie树,即字典树,又称单词查找树或键树,是一种树形结构.典型应用是用于统计和排序大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计.它的优 ...

  6. POJ 2001 Shortest Prefixes 【Trie树】

    <题目链接> 题目大意: 找出能唯一标示一个字符串的最短前缀,如果找不出,就输出该字符串. 解题分析: Trie树的简单应用,对于每个单词的插入,都在相应字符对应的节点 num 值+1 , ...

  7. 从Trie树(字典树)谈到后缀树

    转:http://blog.csdn.net/v_july_v/article/details/6897097 引言 常关注本blog的读者朋友想必看过此篇文章:从B树.B+树.B*树谈到R 树,这次 ...

  8. Trie树 - 字典树

    1.1.什么是Trie树 Trie树,即字典树,又称单词查找树或键树,是一种树形结构.典型应用是用于统计和排序大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计.它的优点是最大限 ...

  9. hdu 1251 trie树

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others) Problem De ...

随机推荐

  1. SurfaceView和TextureView的区别

    SurfaceView和TextureView均继承于android.view.View,与其它View不同的是,两者都能在独立的线程中绘制和渲染,在专用的GPU线程中大大提高渲染的性能.Surfac ...

  2. 【自己的下载平台】搭建aria2网站

    前言 本文章将带你搭建一个自己的服务器下载平台:aria2,它的用途是什么? 下载用途 百度网盘 普通文件 迅雷种子 等等 准备工具 服务器连接软件xshell或者putty 一台服务器 安装宝塔面板 ...

  3. P1678 烦恼的高考志愿

    P1678题库链接:https://www.luogu.org/problem/P1678 难度:普及- 算法标签:模拟,贪心,排序,二分查找 1.朴素模拟 O(m*n) 得分30 先将m个学校的录取 ...

  4. spring——AOP原理及源码(二)

    回顾: 在上一篇中,我们提到@EnableAspectJAutoProxy注解给容器中加入了一个关键组件internalAutoProxyCreator的BeanDefinition,实际类型为 An ...

  5. web前端性能优化一

    作为一个前端会允许自己的作品,在非硬性条件下出现卡顿? 肯定是不会,所以给大家梳理一下前端性能的优化. 一:文件操作 html文件压缩: 删除无用的空格和换行符等其他无意义字符 css文件压缩: 删除 ...

  6. 【jQuery学习日记】jQuery实现滚动动画

    需要实现的效果 样式分析: 2个主要部分,头部的标题和导航部分,和主要的功能实现区域: 1.头部 <div id="header"> <h1>动漫视频< ...

  7. 微信小程序用setData修改数组或对象中的一个属性值,超好用,最简单的实现方法,不容错过!大神们 都 在 看 的方法!!!

    在page中 data: { info: [{ name: "yuki", tou: "../img/head.jpg", zGong: 130, gMoney ...

  8. 轻装上阵Flink--在IDEA上开发基于Flink的实时数据流程序

    前言 本文介绍如何在IDEA上快速开发基于Flink框架的DataStream程序.先直接上手! 环境清单 案例是在win7运行.安装VirtualBox,在VirtualBox上安装Centos操作 ...

  9. Block详解一(底层分析)

    本篇博客不再讲述Block的基本定义使用,最近而是看了很多的block博客讲述的太乱太杂,所以抽出时间整理下block的相关底层知识,在讲述之前,提出几个问题,如果都可以回答出来以及知道原理,大神绕过 ...

  10. python从数据库取数据后写入excel 使用pandas.ExcelWriter设置单元格格式

    用python从数据库中取到数据后,写入excel中做成自动报表,ExcelWrite默认的格式一般来说都比较丑,但workbook提供可以设置自定义格式,简单记录个demo,供初次使用者参考. 一. ...