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. win10锁屏壁纸文件夹位置

    Win10默认系统下载的壁纸怎么下载?在哪里找出来呢?首先我们要把系统的锁屏壁纸要设置为Windows聚焦才会自动从微软的服务器上去下载壁纸.这些都是随机下载的.每个人的都Win10 都有可能不一样. ...

  2. Python面试题(1)

    1.如何反向迭代一个序列 #如果是一个list,最快的方法使用reversetempList = [1,2,3,4]tempList.reverse()for x in tempList:    pr ...

  3. python字典中显示中文

    #coding=utf-8import jsondict={'title':"这是中文"}print json.dumps(dict,ensure_ascii=False,enco ...

  4. 进程及Python实现

    进程杂谈 #进程就是正在执行的一个过程,是对正在运行程序的一个抽象 #进程由程序.数据集和进程控制块(最重要的,进程切换 状态如何保存,恢复和记录)组成 """ 进程调度 ...

  5. React的使用小规范----长期更新

    用this.state控制组件显示,用this.props控制页面业务数据,用this.other保存其他需要的属性,如计时器setInterval的id

  6. [RN] React Native 自定义导航栏随滚动渐变

    React Native 自定义导航栏随滚动渐变 实现效果预览: 代码实现: 1.定义导航栏 NavPage.js import React, {Component} from 'react'; im ...

  7. Apache的安装部署 2(加密认证 ,网页重写 ,搭建论坛)

    一.http和https的基本理论知识1. 关于https: HTTPS(全称:Hypertext Transfer Protocol Secure,超文本传输安全协议),是以安全为目标的HTTP通道 ...

  8. HTTP、HTTP2.0、SPDY、HTTPS 你应该知道的一些事

    参考: https://www.cnblogs.com/wujiaolong/p/5172e1f7e9924644172b64cb2c41fc58.html

  9. Intellij IDEA运行前不检查其他类的错误

    解决方法 第一步 第二步 在工具栏选择 , Run Configurations  设置在运行前不检查错误 

  10. python实现栈结构

    # -*- coding:utf-8 -*- # __author__ :kusy # __content__:文件说明 # __date__:2018/9/30 17:28 class MyStac ...