java单词查找树_Trie 单词查找树 java实现(来自算法第4版)
强烈建议看书上的实现步骤,这里只是一个个人记录。
单词查找树的性能:
查找命中所需的时间与被查找的键的长度成正比。比如单词有7个字符,查找或插入操作最多只需要检查8个节点。
查找未命中只需检查若干个字符。
所需空间:在RN~RNw之间,其中R为字母表大小,N为键的个数,w为平均单词长度。
import java.util.LinkedList;
import java.util.Queue;
/**
* @author yuan
* @date 2019/2/21
* @description 单词查找树,参考算法第4版
*/
public class TrieST {
/**
* 基数
*/
private static int R = 256;
/**
* 根节点
*/
private Node root;
private static class Node{
private Object val;
private Node[] next = new Node[R];
}
public Value get(String key) {
Node x = get(root, key, 0);
if (x == null) {
return null;
}
return (Value) x.val;
}
/**
* 返回以x作为根节点的子单词查找树中与key相关联的值
* @param x
* @param key
* @param d 当前的遍历深度
* @return
*/
private Node get(Node x, String key, int d) {
if (x == null) {
return null;
}
if (d == key.length()) {
return x;
}
char c = key.charAt(d);
return get(x.next[c], key, d + 1);
}
public void put(String key,Value val) {
root = put(root, key, val, 0);
}
/**
* 如果key存在以x为根节点的子单词查找树中则更新与它相关联的值
* @param x
* @param key
* @param val
* @param d
* @return
*/
private Node put(Node x, String key, Value val, int d) {
if (x == null) {
x = new Node();
}
if (d == key.length()) {
x.val = val;
return x;
}
// 找到第d个字符所对应的子单词查找树
char c = key.charAt(d);
x.next[c] = put(x.next[c], key, val, d + 1);
return x;
}
/**
* 查找所有的键
* @return
*/
public Iterable keys(){
return keysWithPrefix("");
}
/**
* 匹配以pre为前缀的键
* @param pre
* @return
*/
private Iterable keysWithPrefix(String pre) {
Queue q = new LinkedList<>();
collect(get(root, pre, 0), pre, q);
return q;
}
private void collect(Node x, String pre, Queue q) {
if (x == null) {
return;
}
if (x.val != null) {
q.offer(pre);
}
for (char c = 0; c < R; c++) {
collect(x.next[c], pre + c, q);
}
}
/**
* 匹配含通配符的键('.'能匹配所有字符)
* @param pat
* @return
*/
public Iterable keysThatMatch(String pat) {
Queue q = new LinkedList<>();
collect(root, "", pat, q);
return q;
}
private void collect(Node x, String pre, String pat, Queue q) {
int d = pre.length();
if (x == null) {
return;
}
if (d == pat.length() && x.val != null) {
q.offer(pre);
}
if (d == pat.length()) {
return;
}
char next = pat.charAt(d);
for (char c = 0; c < R; c++) {
if (next == '.' || next == c) {
collect(x.next[c], pre + c, pat, q);
}
}
}
/**
* 查找s中的最长的键
*
* @param s
* @return
*/
public String longestPrefixOf(String s) {
int length = search(root, s, 0, 0);
return s.substring(0, length);
}
private int search(Node x, String s, int d, int length) {
if (x == null) {
return length;
}
// 当前节点非空,更新length
if (x.val != null) {
length = d;
}
// 已经遍历到s的末尾,返回
if (d == s.length()) {
return length;
}
char c = s.charAt(d);
return search(x.next[c], s, d + 1, length);
}
/**
* 删除操作
* @param key
*/
public void delete(String key) {
root = delete(root, key, 0);
}
private Node delete(Node x, String key, int d) {
if (x == null) {
return null;
}
if (d == key.length()) {
x.val = null;
} else {
char c = key.charAt(d);
x.next[c] = delete(x.next[c], key, d + 1);
}
if (x.val != null) {
return x;
}
for (char c = 0; c < R; c++) {
if (x.next[c] != null) {
return x;
}
}
return null;
}
public static void main(String[] args) {
TrieST trieST = new TrieST<>();
trieST.put("abc", 3);
trieST.put("acc", 1);
trieST.put("bb", 1);
System.out.println(trieST.get("abc")); // 3
System.out.println();
Iterable iterable = trieST.keysThatMatch("a.c");
iterable.forEach(i -> System.out.println(i)); // abc acc
System.out.println();
trieST.delete("abc");
System.out.println(trieST.get("abc")); // null
}
}
java单词查找树_Trie 单词查找树 java实现(来自算法第4版)相关推荐
- java工具:字典树(单词查找树/Trie树)的实现以及使用
项目地址 https://github.com/kylin-hunter/k-dic 文章目录 项目地址 前言 一.字典树 二.使用步骤 1 引入库 1.1 编译并发布到本地 1.2 gradle ( ...
- 前缀树(字典树,单词查找树,Trie树)
参考网址:https://blog.csdn.net/u013949069/article/details/78056102?utm_source=copy 概述 前缀树又名字典树,单词查找树,Tri ...
- 字典树(单词查找树、Trie)
文章目录 单词查找树 1 基本概念 2 字典树的应用 3 字典树基本操作的实现 4 参考资料 单词查找树 1 基本概念 百度百科对字典树的介绍为:是一种树形结构,是一种哈希树的变种. < ...
- 字典树(单词查找树)详解
文章目录 前言 什么是字典树 性质 代码详解 属性 & 构造器 insert 插入 searchPrefix 搜索前缀 完整代码: 前言 当你在搜索条输入字符时,搜索引擎会根据你所输入的字符进 ...
- 常见数据结构和算法实现(排序/查找/数组/链表/栈/队列/树/递归/海量数据处理/图/位图/Java版数据结构)
常见数据结构和算法实现(排序/查找/数组/链表/栈/队列/树/递归/海量数据处理/图/位图/Java版数据结构) 数据结构和算法作为程序员的基本功,一定得稳扎稳打的学习,我们常见的框架底层就是各类数据 ...
- java 分析英文段落,查找、统计单词
目录 题目描述 测试段落 运行效果 用到的方法 题目描述 给定一串字符串(英文段落),用户输入某个单词,求该单词出现的总次数,和出现在第几个位置上. import java.util.*; //主程序 ...
- 数据结构—2-3树、2-3-4树、B树、B+树等多路查找树的原理详解
详细介绍了多路查找树中的2-3树.2-3-4树.B树.B+树的概念的区别,以及它们的应用场景. 文章目录 1 多路查找树的概述 1.1 索引概述 1.2 多路查找树的引出 2 2-3树 2.1 2-3 ...
- 平衡查找树C语言程序,树4. Root of AVL Tree-平衡查找树AVL树的实现
对于一棵普通的二叉查找树而言,在进行多次的插入或删除后,容易让树失去平衡,导致树的深度不是O(logN),而接近O(N),这样将大大减少对树的查找效率.一种解决办法就是要有一个称为平衡的附加的结构条件 ...
- 输入一颗二元查找树,将该树转换为它的镜像
题目:输入一颗二元查找树,将该树转换为它的镜像,即在转换后的二元查找树中,左子树的结点都大于右子树的结点. 例如输入: 8 / \ 6 10 /\ /\ 5 7 9 11 输出: 8 / \ 10 6 ...
最新文章
- Android面试题集合
- ffmpeg avformat_open_input返回失败的解决办法
- 七个帮助你处理Web页面层布局的jQuery插件
- 台达服务器型号,台达网络服务器机柜 42u 600宽 1200 2000mm SR1160标准机柜
- 贝叶斯之垃圾邮件分类
- linux嵌入式工作室,【课程培训】嵌入式linux驱动开发系列教程 | linux2web工作室...
- python之将python代码编译成.so
- hive时间函数入门
- BT和eMule协议的比较和分析
- 树莓派是什么?能干什么?
- 51单片机Proteus仿真+Keil工程-实验5-单片机串行口中断实验
- 高德地图JS--批量规划步行路线 优化
- html代码word,Web前端
- cannot read property 'options' of underfined
- 计算机端口com1,访问端口“COM1”被拒绝(Access to the port “COM1” is denied)
- tablueau地图标记圆形_多点钉图标记-简单易用的地图位置标记标注工具
- 4星+|《赋能:打造应对不确定性的敏捷团队》:海豹突击队学习伊拉克“基地”组织的组织形式并且最终战胜对方的故事...
- 主流计算机戴尔笔记本电脑,主流价位好机器 戴尔灵越一体机23 5348
- 视频教程-简历5个模块讲解-招聘/面试
- 灰度共生矩阵(附python代码)
热门文章
- MySQL-locate()函数
- SpringBoot设置首页(默认页)跳转
- Cookie与Session相关学习笔记
- openocd目录_OpenOCD的调试
- 技校毕业是什么学历_初中毕业上技校学什么专业好
- thinkphp三级分销小程序源码_山东谷道微信小程序商城源码带后台 公众号平台三级分销系统...
- unistd.h linux,Linux 标准库下的unistd.h
- Java常用设计模式————原型模式(二)之深拷贝与浅拷贝
- html加动画不改变高度,怎么为不定高度(height:auto)的元素添加CSS3 transition-property:height 动画...
- 创建mysql视图语法正确的是_MySQL创建视图的语法格式