leetcode 地址:

https://leetcode.com/problems/implement-trie-prefix-tree/description/

难度:中等

描述:略

解题思路:

Trie树 也就是字典查找树,是一种能够实现在一个字符串集中实现快速查找和匹配的多叉树结构,关于Trie树的深入分析我就不展开了,因为我自己也理解的不深刻^_^,这里只给出Trie树的定义,以及常用的应用场景,然后给出一个简单的java实现,当然代码简洁性和性能上有很大的优化空间。

首先,Trie树的定义(或者说是性质):

1. 根节点是一个空节点,不包含字符

2. 每个节点含有一个字符,以及若干个子节点

3. 每个节点的所有子节点所包含的字符都不相同

3. 树的每个节点到根节点的路径上的所有字符组成一个字符串,表示这个字符串在树中可能存在,或者至少Trie树中存在以此字符串为前缀的字符串

4. 每个非根节点还应该包含一个整型数值,表示根节点到这个节点组成的字符串在Trie树中出现的次数

Trie数的常见应用场景:

1. 字符串检索

2. 词频统计

3. 前缀检索

4.前缀词频统计

5. 对所有的字符串按照字典序排序

java实现:

public class Trie {
public static void main(String[] args) {
Trie trie = new Trie();
trie.insert("apple");
System.out.println(trie.search("apple"));
System.out.println(trie.search("app")); // returns false
System.out.println(trie.startsWith("app")); // returns true
trie.insert("app");
System.out.println(trie.search("app")); // returns true
} TrieNode root; /**
* Initialize your data structure here.
*/
public Trie() {
root = new TrieNode();
} /**
* Inserts a word into the trie.
*/
public void insert(String word) {
insert(word, root, 0);
} /**
* 将从index处开始的字串插入到root的子节点中,即将index对应的字符插入到root的子节点中
* @param word
* @param root
* @param index
*/
private void insert(String word, TrieNode root, int index) {
assert index < word.length() && index > -1;
char cur = word.charAt(index);
TreeMap<Character, TrieNode> children = root.children;
if (null == children) {
children = new TreeMap<>();
root.children = children;
}
if (!children.containsKey(cur)) {
children.put(cur, new TrieNode(cur));
}
if (index == word.length() - 1) {
children.get(cur).occurency++;
return;
}
insert(word, children.get(cur), index + 1);
} /**
* Returns if the word is in the trie.
*/
public boolean search(String word) {
return search(word, root, 0);
} /**
* 在root的子节点中搜索从index开始的字串
* @param word
* @param root
* @param index
* @return
*/
private boolean search(String word, TrieNode root, int index) {
assert index > -1 && index < word.length();
char cur = word.charAt(index);
if (root.children == null ||
!root.children.containsKey(cur)) {
return false;
}
if (index == word.length() - 1) {
return root.children.get(cur).occurency > 0;
}
return search(word, root.children.get(cur), index + 1);
} /**
* Returns if there is any word in the trie that starts with the given prefix.
*/
public boolean startsWith(String prefix) {
return startsWith(prefix, root, 0);
} /**
* 在root的子节点中搜索从index开始字串对应的前缀
* @param prefix
* @param root
* @param index
* @return
*/
private boolean startsWith(String prefix, TrieNode root, int index) {
assert index > -1 && index < prefix.length();
char cur = prefix.charAt(index);
if (root.children == null ||
!root.children.containsKey(cur)) {
return false;
}
if (index == prefix.length() - 1) {
return true;
}
return startsWith(prefix, root.children.get(cur), index + 1);
} static class TrieNode {
char c;
int occurency = 0;
TreeMap<Character, TrieNode> children; public TrieNode() {
} public TrieNode(char c) {
this.c = c;
}
}
}

Trie树的java实现的更多相关文章

  1. Trie 树 及Java实现

    来源于英文“retrieval”.   Trie树就是字符树,其核心思想就是空间换时间. 举个简单的例子.   给你100000个长度不超过10的单词.对于每一个单词,我们要判断他出没出现过,如果出现 ...

  2. 双数组Trie树(DoubleArrayTrie)Java实现

    http://www.hankcs.com/program/java/%E5%8F%8C%E6%95%B0%E7%BB%84trie%E6%A0%91doublearraytriejava%E5%AE ...

  3. leetcode网站中找到的关于trie树的JAVA版本介绍

    class TrieNode { // R links to node children private TrieNode[] links; private final int R = 26; pri ...

  4. Trie树的应用:查询IP地址的ISP

    1. 问题描述 给定一个IP地址,如何查询其所属的ISP,如:中国移动(ChinaMobile),中国电信(ChinaTelecom),中国铁通(ChinaTietong)?现有ISP的IP地址区段可 ...

  5. 从Trie树到双数组Trie树

    Trie树 原理 又称单词查找树,Trie树,是一种树形结构,是一种哈希树的变种.它的优点是:利用字符串的公共前缀来减少查询时间,最大限度地减少无谓的字符串比较,能在常数时间O(len)内实现插入和查 ...

  6. Trie树(转:http://blog.csdn.net/arhaiyun/article/details/11913501)

    Trie 树, 又称字典树,单词查找树.它来源于retrieval(检索)中取中间四个字符构成(读音同try).用于存储大量的字符串以便支持快速模式匹配.主要应用在信息检索领域. Trie 有三种结构 ...

  7. 字典树(Trie)的java实现

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

  8. java实现的Trie树数据结构

    近期在学习的时候,常常看到使用Trie树数据结构来解决这个问题.比方" 有一个1G大小的一个文件.里面每一行是一个词.词的大小不超过16字节,内存大小限制是1M. 返回频数最高的100个词. ...

  9. Trie树(字典树)的介绍及Java实现

    简介 Trie树,又称为前缀树或字典树,是一种有序树,用于保存关联数组,其中的键通常是字符串.与二叉查找树不同,键不是直接保存在节点中,而是由节点在树中的位置决定.一个节点的所有子孙都有相同的前缀,也 ...

随机推荐

  1. c#-泛型、协变、逆变

    泛型简单介绍: 可以使用泛型声明的元素:类.接口.方法.委托 泛型之前:泛型之前使用object封装不同类型的参数,缺点:性能差.运行时判断类型(不安全)...泛型是在编译期间转为实际类型副本,所以性 ...

  2. CF1245E:Hyakugoku and Ladders

    CF1245E:Hyakugoku and Ladders 题意描述: 给你一个\(10*10\)的矩阵,矩阵描述如下 最开始的时候你在左下角,你的目标是到达左上角. 你可以走路径或者爬梯子. 路径的 ...

  3. 从三数之和看如何优化算法,递推-->递推加二分查找-->递推加滑尺

    人类发明了轮子,提高了力的使用效率. 人类发明了自动化机械,将自己从重复的工作中解脱出来. 提高效率的方法好像总是离不开两点:拒绝无效劳动,拒绝重复劳动.人类如此,计算机亦如是. 前面我们说过了四数之 ...

  4. 09-排序2 Insert or Merge (25 分)

    According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...

  5. wpf radiobuttong 去前面的圆点, 自定义radiobutton样式

    自定义radiobutton样式代码: <windows.Resources> <LinearGradientBrush x:Key="CheckRadioFillNorm ...

  6. List中的ArrayList和LinkedList源码分析

    ​ List是在面试中经常会问的一点,在我们面试中知道的仅仅是List是单列集合Collection下的一个实现类, List的实现接口又有几个,一个是ArrayList,还有一个是LinkedLis ...

  7. matlab 彩色图像转化成灰度图像,灰度图像降低灰度级

    灰度级数k,k=2^b,称该图像为b比特图像. 降低灰度级数是靠2的幂次方 网上代码:https://blog.csdn.net/silence2015/article/details/6892736 ...

  8. 第4课 decltype类型推导

    第4课 decltype类型推导 一.decltype类型推导 (一)语法: 1.语法:decltype(expr),其中的expr为变量(实体)或表达式 2.说明: ①decltype用于获取变量的 ...

  9. 《Linux就该这么学》自学笔记_ch22_使用openstack部署云计算服务环境

    <Linux就该这么学>自学笔记_ch22_使用openstackb部署云计算服务环境 文章主要内容: 了解云计算 Openstack项目 服务模块组件详解 安装Openstack软件 使 ...

  10. cad.net 依照旧样条曲线数据生成一条新样条曲线的代码段. spline生成

    Spline spl = entity as Spline; //拿到旧的spline图元... //样条曲线生成条件 var controlPoints = new Point3dCollectio ...