数据结构——Trie树

概念

Trie树,又称字典树、前缀树,是一种树形结构,是一种哈希树的变种。典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计。它的优点是:利用字符串的公共前缀来减少查询时间,最大限度地减少无谓的字符串比较,查询效率比哈希树高。

Trie树的结构如下图所示:

Trie树中的节点数据结构如下:

  • 当前字符
  • 子节点数组(如果全为小写字母的话,子节点数量固定为26个,根据字符来确定在数组中的位置,如'a'的下标为0,'z'为25)
  • 是否为一个单词的结尾(标红的节点)
  • 出现次数

特点:root节点不存储字符。

代码实现

public class Trie {
private Node root;
class Node{
int count;
char ch;
Node[] child;
boolean isEnd; public Node() {
this.count = 1;
this.child = new Node[26];
this.isEnd = false;
}
} /** Initialize your data structure here. */
public Trie() {
this.root = new Node();
} /** Inserts a word into the trie. */
public void insert(String word) {
if (null == word || "".equals(word)) return;
if (this.search(word)) return; Node cur = this.root;
char[] chars = word.toCharArray();
for(char c : chars) {
//根据字符找到在子节点数组中的下标
int pos = c - 'a';
Node child = cur.child[pos];
//如果子节点没有被初始化过则初始化,并设置其字符
if (child == null) {
cur.child[pos] = new Node();
cur.child[pos].ch = c;
}
cur.count++;
//更新cur节点为子节点,向下递归
cur = cur.child[pos];
} //最后一个字符的节点
cur.isEnd = true;
} /** Returns if the word is in the trie. */
public boolean search(String word) {
if (null == word || "".equals(word)) return true;
Node cur = this.root;
char[] chars = word.toCharArray();
for (char c : chars) {
int pos = c - 'a'; Node node = cur.child[pos];
if (node == null) return false; cur = node;
}
return cur.isEnd;
} /** Returns if there is any word in the trie that starts with the given prefix. */
public boolean startsWith(String prefix) {
if (null == prefix || "".equals(prefix)) return true;
Node cur = this.root;
char[] chars = prefix.toCharArray();
for (char c : chars) {
int pos = c - 'a';
Node node = cur.child[pos];
if (node == null) return false;
cur = node;
} return cur.count > 0;
} public static void main(String[] args) {
Trie trie = new Trie(); trie.insert("a");
trie.insert("adc");
trie.insert("aer"); System.out.println(trie.search("a"));
System.out.println(trie.startsWith("a"));
}
}

结果:

208. Implement Trie (Prefix Tree)

超过94%,感觉还不错~

【数据结构】Trie树的更多相关文章

  1. 数据结构~trie树(字典树)

    1.概述 Trie树,又称字典树,单词查找树或者前缀树,是一种用于快速检索的多叉树结构,如英文字母的字典树是一个26叉树,数字的字典树是一个10叉树. 我理解字典树是看了这位大佬博客.还不了解字典树的 ...

  2. hiho149周 - 数据结构 trie树

    题目链接 坑点:accept和deny的ip可能相同,需加个判断 #include <cstdio> #include <cstdlib> #include <vecto ...

  3. 基于trie树的具有联想功能的文本编辑器

    之前的软件设计与开发实践课程中,自己构思的大作业题目.做的具有核心功能,但是还欠缺边边角角的小功能和持久化数据结构,先放出来,有机会一点点改.github:https://github.com/chu ...

  4. 讲解——Trie树(字典树)

          Trie树(字典树) 一.引入 字典是干啥的?查找字的. 字典树自然也是起查找作用的.查找的是啥?单词. 看以下几个题: 1.给出n个单词和m个询问,每次询问一个单词,回答这个单词是否在单 ...

  5. Trie树(转)

    原文http://www.cnblogs.com/TheRoadToTheGold/p/6290732.html 一.引入 字典是干啥的?查找字的. 字典树自然也是起查找作用的.查找的是啥?单词. 看 ...

  6. 浅谈Trie树(字典树)

          Trie树(字典树) 一.引入 字典是干啥的?查找字的. 字典树自然也是起查找作用的.查找的是啥?单词. 看以下几个题: 1.给出n个单词和m个询问,每次询问一个单词,回答这个单词是否在单 ...

  7. [转] 浅谈Trie树(字典树)

    原文地址:https://www.cnblogs.com/TheRoadToTheGold/p/6290732.html Trie树(字典树) 一.引入 字典是干啥的?查找字的. 字典树自然也是起查找 ...

  8. Trie树分词

    http://www.hankcs.com/program/java/tire-tree-participle.html 最近在看Ansj中文分词的源码,以前没有涉足过这个领域,所以需要做一些笔记. ...

  9. 查找(二)简单清晰的B树、Trie树具体解释

    查找(二) 散列表 散列表是普通数组概念的推广.因为对普通数组能够直接寻址,使得能在O(1)时间内訪问数组中的任何位置.在散列表中,不是直接把keyword作为数组的下标,而是依据keyword计算出 ...

随机推荐

  1. Vue SPA 首屏加载优化实践

    写在前面 本文记录笔者在Vue SPA项目首屏加载优化过程中遇到的一些坑及优化方案! 我们以 vue-cli 工具为例,使用 vue-router 搭建SPA应用,UI框架选用 element-ui ...

  2. Callback2.0

    Callback定义? a callback is a piece of executable code that is passed as an argument to other code, wh ...

  3. C++之C/C++内存对齐

    一.什么是字节对齐,为什么要对齐 现代计算机中内存空间都是按照byte划分的,从理论上讲似乎对任何类型的变量的访问可以从任何地址开始,但实际情况是在访问特定类型变量的时候经常在特 定的内存地址访问,这 ...

  4. 64_o1

    OCE-devel-0.18.1-1.fc26.i686.rpm 15-May-2017 18:37 5634474 OCE-devel-0.18.1-1.fc26.x86_64.rpm 15-May ...

  5. shell将多行文本重定向到文件【转】

    在shell中,使用Here Document方式将文本重定向到文件,格式如下: ( cat << EOF 要写的文本 EOF ) > 目标文件 示例test.sh: #! /bin ...

  6. elk系列3之通过json格式采集Nginx日志【转】

    转自 elk系列3之通过json格式采集Nginx日志 - 温柔易淡 - 博客园http://www.cnblogs.com/liaojiafa/p/6158245.html preface 公司采用 ...

  7. 获取网站所有的url正则表达式

    C# string pattern1 = @"(?is)<[^>]*?src=(['""\s]?)(?<src>[^'""\s ...

  8. Ubuntu16.04安装记

    Ubuntu16.04安装记 基本信息: 华硕笔记本 Windows 10 家庭版 处理器:Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz 2.71GHz 已安装的内 ...

  9. CentOS7安装Hadoop2.7完整步骤

    总体思路,准备主从服务器,配置主服务器可以无密码SSH登录从服务器,解压安装JDK,解压安装Hadoop,配置hdfs.mapreduce等主从关系. 1.环境,3台CentOS7,64位,Hadoo ...

  10. slf4j中的Logger 使用占位符{} 来传入参数记录日志信息

    首先要导入 slf4j包中的2个类 import org.slf4j.Logger;import org.slf4j.LoggerFactory; 再定义如下 private final static ...