数据结构——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. GridControl详解(七)事件

    private void gridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventA ...

  2. Codeforces 765F Souvenirs

    time limit per test 3 seconds memory limit per test 512 megabytes input standard input output standa ...

  3. Mac 10.9x下安装配置phonegap3.0开发环境 (涉及android sdk配置)

    最近突然想弄一下phonegap,之前一直是听说,没亲自配置开发过.结果配置过程非常艰难啊.特别是android平台的配置,那叫一个麻烦,网上搜了半天都没找到非常好的资料.文章也都是抄来抄去,最烦的就 ...

  4. Ajax+innerHTML+Dgls=好的用户体验+高性能+高效率

    为了引入Dgls,我们从创建Dom节点说起. 用JS创建Dom节点 var div = document.createElement('div'); div.className = 'gdls'; v ...

  5. php中的转义函数

    <?php parse_url 解析URL, 返回各组成部分 urlencode/urldecode url编码/解码 htmlentities 将字符串转化为html实体 htmlentiti ...

  6. (转)USB协议简介

    USB协议简介     USB是一种协议总线,即主机与设备之间的通信需要遵循一系列约定.协议内容较多,这里仅作一些简单介绍,深入学习,可参看USB规范(WWW.usb.org).     为了理解协议 ...

  7. curl: (6) Couldn’t resolve host ‘www.ttlsa.com’【转】

    上周, 部分站点出现Couldn't resolve host.....问题,  导致公司所有走api的程序都无法正常使用(系统redhat 6.3的都出现问题, redhat 5一切OK). 最后解 ...

  8. http 错误代码解释 && nginx 自定义错误【转】

    如果向您的服务器发出了某项请求要求显示您网站上的某个网页(例如,当用户通过浏览器访问您的网页或在 Googlebot 抓取该网页时),那么,您的服务器会返回 HTTP 状态代码以响应该请求. 此状态代 ...

  9. $fhqTreap$

    - $fhqTreap$与$Treap$的差异 $fhqTreap$是$Treap$的非旋版本,可以实现一切$Treap$操作,及区间操作和可持久化 $fhqTreap$非旋关键在于分裂与合并$(Sp ...

  10. vue单选,多选,多选的内容显示在页面可删除

    vue做单选只能选一个 <template> <div class="list"> <!-- 多行多列单选 --> <span>只能 ...