字典树

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

它有三个基本性质:

(1)根节点不存储字符
(2)除根节点外每一个节点都只存储一个字符
(3)从根节点到某一节点,路径上经过的字符连接起来,为该节点对应的字符串,每个节点的所有子节点包含的字符都不相同。

Java实现代码(注释详细):

 package com.wxisme.trietree;

 /**
*Trie树的实现
*@author wxisme
*@time 2015-10-13 下午9:48:30
*/
public class TrieTree { private final int SIZE = 26;//字符出现的种类数,以所有的小写字母为例 private int nodeNumber;//子节点的个数 private int depth;//树的深度 private TrieNode root;//树根 public TrieTree() {
this.nodeNumber = 0;
this.depth = 0;
this.root = new TrieNode();
} /**
* 节点结构
* @author wxisme
*
*/
private class TrieNode {
private char val;//节点值 private TrieNode son[];//子节点数组 private boolean isEnd;//是否有以此节点为结束字符的单词 private int pearNumber;//节点出现的次数 public TrieNode() {
this.isEnd = false;
this.pearNumber = 0;
this.son = new TrieNode[SIZE];
}
} /**
* 向Trie中插入一个word
* @param word
*/
public void insert(String word) {
char[] wordChars = word.toCharArray(); TrieNode node = this.root; for(char ch : wordChars) {
int pos = ch - 'a';
//如果相应位置为空则创建
if(node.son[pos] == null) {
node.son[pos] = new TrieNode();
node.son[pos].val = ch;
node.pearNumber = 1;//第一次出现
this.nodeNumber ++;
}
else {//已经有该字符
node.pearNumber ++;
}
node = node.son[pos];
}
node.isEnd = true;
this.depth = Math.max(this.depth, word.length());
} /**
* 查找是否存在单词word
* @param word
* @return 结果
*/
public boolean search(String word) {
char[] wordChars = word.toCharArray(); TrieNode node = this.root; for(char ch : wordChars) {
int pos = ch - 'a';
if(node.son[pos] != null) {
node = node.son[pos];//继续向下查找
}
else {
return false;
}
} return node.isEnd;
} /**
* 查找是否存在以word为前缀的单词,和search()类似,只是不用判断边界。
* @param word
* @return 结果
*/
public boolean searchPrefix(String word) {
char[] wordChars = word.toCharArray(); TrieNode node = this.root; for(char ch : wordChars) {
int pos = ch - 'a';
if(node.son[pos] != null) {
node = node.son[pos];//继续向下查找
}
else {
return false;
}
} return true;
} /**
* 统计单词出现的次数
* @param word
* @return 结果
*/
public int wordCount(String word) {
char[] wordChars = word.toCharArray(); TrieNode node = this.root; for(char ch : wordChars) {
int pos = ch - 'a';
if(node.son[pos] == null) {
return 0;
}
else {
node = node.son[pos];
}
} return node.isEnd?node.pearNumber:0;
} /**
* 统计以word为前缀的单词个数
* @param word
* @return 结果
*/
public int wordPrefixCount(String word) {
char[] wordChars = word.toCharArray(); TrieNode node = this.root; for(char ch : wordChars) {
int pos = ch - 'a';
if(node.son[pos] == null) {
return 0;
}
else {
node = node.son[pos];
}
} return node.pearNumber;
} /**
* 深度优先遍历Trie树
* @param root
*/
public void traversal(TrieNode root) {
if(root == null) {
return;
}
for(TrieNode node : root.son) {
System.out.println(node.val);
traversal(node);
}
} public int getNodeNumber() {
return nodeNumber;
} public int getDepth() {
return depth;
} public TrieNode getRoot() {
return root;
} }

Leetcode应用:http://www.cnblogs.com/wxisme/p/4875309.html    http://www.cnblogs.com/wxisme/p/4876980.html

Trie树的分析与实现的更多相关文章

  1. Trie树(c++实现)

    转:http://www.cnblogs.com/kaituorensheng/p/3602155.html http://blog.csdn.net/insistgogo/article/detai ...

  2. 【BZOJ-4523】路由表 Trie树 + 乱搞

    4523: [Cqoi2016]路由表 Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 155  Solved: 98[Submit][Status][ ...

  3. 【Hihocoder】1014 : Trie树

    问题:http://hihocoder.com/problemset/problem/1014 给定一个字符串字典dict,输入字符串str, 要求从dict中找出所有以str为前缀的字符串个数. 构 ...

  4. Trie树

    一.什么是trie树 1.Trie树 (特例结构树)   Trie树,又称单词查找树.字典树,是一种树形结构,是一种哈希树的变种,是一种用于快速检索的多叉树结构.典型应用是用于统计和排序大量的字符串( ...

  5. 数据结构《16》----自动补齐实现《一》----Trie 树

    1. 简述 Trie 树是一种高效的字符串查找的数据结构.可用于搜索引擎中词频统计,自动补齐等. 在一个Trie 树中插入.查找某个单词的时间复杂度是 O(len), len是单词的长度. 如果采用平 ...

  6. 字符串 --- KMP Eentend-Kmp 自动机 trie图 trie树 后缀树 后缀数组

    涉及到字符串的问题,无外乎这样一些算法和数据结构:自动机 KMP算法 Extend-KMP 后缀树 后缀数组 trie树 trie图及其应用.当然这些都是比较高级的数据结构和算法,而这里面最常用和最熟 ...

  7. [POJ] #1002# 487-3279 : 桶排序/字典树(Trie树)/快速排序

    一. 题目 487-3279 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 274040   Accepted: 48891 ...

  8. trie树(前缀树)

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

  9. [转]数据结构之Trie树

    1. 概述 Trie树,又称字典树,单词查找树或者前缀树,是一种用于快速检索的多叉树结构,如英文字母的字典树是一个26叉树,数字的字典树是一个10叉树. Trie一词来自retrieve,发音为/tr ...

随机推荐

  1. CI框架 -- 附属类

    有些时候,你可能想在你的控制器之外新建一些类,但同时又希望 这些类还能访问 CodeIgniter 的资源 任何在你的控制器方法中初始化的类都可以简单的通过 get_instance() 函数来访问 ...

  2. Gateway/Worker模型 数据库使用示例

    From: http://www.bubuko.com/infodetail-777418.html 1.数据库配置Applications/XXX/Config/Db.php <?php na ...

  3. IntelliJ IDEA 的 .idea 目录加入.gitignore无效的解决方法

    [转载] 无效的原因是:对应的目录或者文件已经被git跟踪,此时再加入.gitignore后就无效了, 解决办法: 先执行 [文件夹]  git rm -r --cached .idea [文件]   ...

  4. 启动zookeeper时出现的问题

    zkEnv.cmd @echo off REM Licensed to the Apache Software Foundation (ASF) under one or more REM contr ...

  5. 【RespberryPi】数码管

    http://blog.mangolovecarrot.net/2015/06/03/raspi-study0801/ 应该可以用两块74HC595来驱动显示8位数的数码管.

  6. vue-cli生成的项目配置开发和生产环境不同的接口

    vue-cli生成的项目,vue项目配置了不同开发环境的接口地址,axios.defaults.baseURL如何引用这个地址,这是在我发布项目的时候考虑的,于是想到了 方法一: config下配置文 ...

  7. Npm基本指令(转)

    一些常用的 npm 指令 當你設定好 node.js 的開發環境後, 是時候來把下面這些常用的 npm 指令給摸熟了. 將套件於全域安裝. 全域安裝的套件通常只是為了執行檔而已. $ npm inst ...

  8. Blender 编辑模式

    1.如何进入编辑模式 可直接通过“Tab”快捷键进入编辑模式,或者选择界面底部的下拉列表: 如果想退出编辑模式,可再按下“Tab”键退出. 2.编辑选择 进入编辑状态后,我们可以通过鼠标右键来选择某个 ...

  9. SpringBoot------Eclipce配置Spring Boot

    步骤一: 步骤二: 点击左下角Eclipse图标下的“Popular”菜单,选择Spring安装(已安装的插件在Installed中显示),一直按步骤确定就好了,如果中途下载超时什么的,就看看自己的网 ...

  10. PHP压缩html网页代码原理(清除空格,换行符,制表符,注释标记)

    本博启用了一个叫wp super cache的页面压缩工具, 源代码没有去查看,不过原理很简单. 我们可以自己动手书写一个压缩脚本. 清除换行符,清除制表符,去掉注释标记 .它所起到的作用不可小视. ...