Java Trie字典树,前缀树
Trie查询每个条目的时间复杂度,和字典中一共有多少条无关。
时间复杂度为O(W)
w为查询单词的长度

import java.util.TreeMap;
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;
}
// 返回Trie中存储的单词数量
public int getSize() {
return size;
}
// 向Trie中添加一个新的单词word
public void add(String word) {
Node cur = root;
for (int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
if (cur.next.get(c) == null)
cur.next.put(c, new Node());
cur = cur.next.get(c);
}
if (!cur.isWord) {
cur.isWord = true;
size++;
}
}
//查询单词word是否在Trie中
public boolean contains(String word){
Node cur=root;
for (int i = 0; i < word.length(); i++) {
char c=word.charAt(i);
if(cur.next.get(c)==null)
return false;
cur=cur.next.get(c);
}
return cur.isWord;
}
//查询是否在Trie中有单词以prefix为前缀
public boolean isPrefix(String prefix) {
Node cur=root;
for (int i = 0; i < prefix.length(); i++) {
char c=prefix.charAt(i);
if(cur.next.get(c)==null)
return false;
cur=cur.next.get(c);
}
return true;
}
}
测试:
public class Main {
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();
BSTSet<String> set=new BSTSet<String>();
for(String word:words)
set.add(word);
for(String word:words)
set.contains(word);
long endTime = System.nanoTime();
double time = (endTime - startTime) / 1000000000.0;
System.out.println("Total different words: " + set.getSize());
System.out.println("BSTSet: " + time + " s");
startTime = System.nanoTime();
Trie trie = new Trie();
for(String word: words)
trie.add(word);
for(String word: words)
trie.contains(word);
endTime = System.nanoTime();
time = (endTime - startTime) / 1000000000.0;
System.out.println("Total different words: " + trie.getSize());
System.out.println("Trie: " + time + " s");
}
}
}
search可以搜索文字或正则表达式字符串,字符串只包含字母.或者a-z.
import java.util.TreeMap;
public class WordDictionary {
public class Node{
private boolean isWord;
public TreeMap<Character, Node> next;
public Node(boolean isWord){
this.isWord=isWord;
next=new TreeMap<>();
}
public Node() {
this(false);
}
}
private Node root;
public WordDictionary(){
root =new Node();
}
public void addWord(String word) {
Node cur = root;
for (int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
if (cur.next.get(c) == null)
cur.next.put(c, new Node());
cur = cur.next.get(c);
}
cur.isWord = true;
}
public boolean search(String word){
return match(root, word, 0);
}
private boolean match(Node node,String word,int index) {
if(index==word.length())
return node.isWord;
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 nextChar :node.next.keySet()) {
if(match(node.next.get(nextChar), word, index+1))
return true;
}
return false;
}
}
}
Trie和映射
import java.util.TreeMap;
public class MapSum {
private class Node{
public boolean isWord;
public int value;
public TreeMap<Character, Node> next;
public Node(int value){
this.value=value;
next=new TreeMap<>();
}
public Node(){this(0);}
}
private Node root;
public MapSum() {
root=new Node();
}
public void insert(String word,int val){
Node cur=root;
for (int i = 0; i < word.length(); i++) {
char c=word.charAt(i);
if(cur.next.get(c)==null)
cur.next.put(c,new Node());
cur=cur.next.get(c);
}
cur.value=val;
}
public int sum(String prefix){
Node cur=root;
for (int i = 0; i < prefix.length(); i++) {
char c=prefix.charAt(i);
if(cur.next.get(c)==null)
return 0;
cur=cur.next.get(c);
}
return sum(cur);
}
private int sum(Node node){
if(node.next.size()==0)
return node.value;
int res=node.value;
for (char c:node.next.keySet())
res+=sum(node.next.get(c));
return res;
}
}
Java Trie字典树,前缀树的更多相关文章
- 9-11-Trie树/字典树/前缀树-查找-第9章-《数据结构》课本源码-严蔚敏吴伟民版
课本源码部分 第9章 查找 - Trie树/字典树/前缀树(键树) ——<数据结构>-严蔚敏.吴伟民版 源码使用说明 链接☛☛☛ <数据结构-C语言版>(严蔚 ...
- [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...
- [LeetCode] 208. Implement Trie (Prefix Tree) 实现字典树(前缀树)
Implement a trie with insert, search, and startsWith methods. Example: Trie trie = new Trie(); trie. ...
- 内存空间有限情况下的词频统计 Trie树 前缀树
数据结构与算法专题--第十二题 Trie树 https://mp.weixin.qq.com/s/nndr2AcECuUatXrxd3MgCg
- Trie - leetcode [字典树/前缀树]
208. Implement Trie (Prefix Tree) 字母的字典树每个节点要定义一个大小为26的子节点指针数组,然后用一个标志符用来记录到当前位置为止是否为一个词,初始化的时候讲26个子 ...
- LeetCode OJ:Implement Trie (Prefix Tree)(实现一个字典树(前缀树))
Implement a trie with insert, search, and startsWith methods. 实现字典树,前面好像有道题做过类似的东西,代码如下: class TrieN ...
- HDU 1251 字典树(前缀树)
题目大意 :Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀).(单词互不相同) ...
- Trie(前缀树/字典树)及其应用
Trie,又经常叫前缀树,字典树等等.它有很多变种,如后缀树,Radix Tree/Trie,PATRICIA tree,以及bitwise版本的crit-bit tree.当然很多名字的意义其实有交 ...
- TRIE 字典树 前缀紧急集合!
TRIE: 在计算机科学中,Trie,又称前缀树或字典树,是一种有序树状的数据结构,用于保存关联数组,其中的键通常是字符串.——百度百科 自我理解: trie树,是一种处理字符串前缀的数据结构,通常会 ...
随机推荐
- 第六届SD省赛 Circle of Friends
Circle of Friends Time Limit: 2000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descr ...
- spring启动容器加载成功后执行调用方法
需求: 由于在微服务架构中各服务之间都是通过接口调用来进行交互的,像很多的基础服务,类似字典信息其实并不需每次需要的时候再去请求接口.所以我的想法是每次启动项目的时候,容器初始化完成,就去调用一下基础 ...
- [ZJOI2019]线段树
题目大意 一开始有一棵线段树,然后有一个操作序列,问执行这个操作序列的所有子集时线段树上有标记的节点个数和. 题解 其实我们把它除以\(2^m\)后发现就是有标记节点的期望个数. 然后套路的根据期望的 ...
- 互联网+ 何人能挡?带着你的Code飞奔吧!
Python方向: 早期方向 Web全栈 擅长专栏 爬虫系列 数据分析 人工智能 物联网系(lot万物互联)[逆天很看好未来的前景] 自动化运维(安全与测试) 其他系列 游戏开发(最近很火) 导航栏: ...
- 不用代码就能实现get与post
这些天在测试各种API,每次都敲代码实现,就显得有点浪费时间了 为了节约时间,提高效率,我想着收集一些可以只用 -命令行- 或者是 -浏览器- 就能够实现的技巧 在这里,我简单测试三种工具来实现 ge ...
- GIT-Linux(CentOS7)系统部署git服务器
GIT-Linux(CentOS7)系统部署git服务器 root账号登录 一. 安装并配置必要的依赖关系在CentOS系统上安装所需的依赖:ssh,防火墙,postfix(用于邮件通知) ,wget ...
- 虚拟机有QQ消息时宿主机自动弹窗提示
因为是检测窗口实现的,所以要求设置会话窗口自动弹出,而且看完消息就把QQ消息窗口关掉... 虚拟机端 #! /usr/bin/env python # -*- coding: utf-8 -*- fr ...
- mysql命令行的导入导出sql,txt,excel(都在linux或windows命令行操作)(转自筑梦悠然)
原文链接https://blog.csdn.net/wuhuagu_wuhuaguo/article/details/73805962 Mysql导入导出sql,txt,excel 首先我们通过命令行 ...
- Python中所有的关键字
在python中若想查询python中有哪些关键字可以先导入keyword模块 import keyword #导入关键字模块print(keyword.kwlist) #查询所有关键字 查询结果: ...
- 初识正则表达式matcher.group
matcher.group中group是匹配()的,group(0)指的是整个串,group(1) 指的是第一个括号里的内容,group(2)指的第二个括号里的内容,以此类推. 例如: str = & ...