Trie树的分析与实现
字典树
又称单词查找树,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树的分析与实现的更多相关文章
- Trie树(c++实现)
转:http://www.cnblogs.com/kaituorensheng/p/3602155.html http://blog.csdn.net/insistgogo/article/detai ...
- 【BZOJ-4523】路由表 Trie树 + 乱搞
4523: [Cqoi2016]路由表 Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 155 Solved: 98[Submit][Status][ ...
- 【Hihocoder】1014 : Trie树
问题:http://hihocoder.com/problemset/problem/1014 给定一个字符串字典dict,输入字符串str, 要求从dict中找出所有以str为前缀的字符串个数. 构 ...
- Trie树
一.什么是trie树 1.Trie树 (特例结构树) Trie树,又称单词查找树.字典树,是一种树形结构,是一种哈希树的变种,是一种用于快速检索的多叉树结构.典型应用是用于统计和排序大量的字符串( ...
- 数据结构《16》----自动补齐实现《一》----Trie 树
1. 简述 Trie 树是一种高效的字符串查找的数据结构.可用于搜索引擎中词频统计,自动补齐等. 在一个Trie 树中插入.查找某个单词的时间复杂度是 O(len), len是单词的长度. 如果采用平 ...
- 字符串 --- KMP Eentend-Kmp 自动机 trie图 trie树 后缀树 后缀数组
涉及到字符串的问题,无外乎这样一些算法和数据结构:自动机 KMP算法 Extend-KMP 后缀树 后缀数组 trie树 trie图及其应用.当然这些都是比较高级的数据结构和算法,而这里面最常用和最熟 ...
- [POJ] #1002# 487-3279 : 桶排序/字典树(Trie树)/快速排序
一. 题目 487-3279 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 274040 Accepted: 48891 ...
- trie树(前缀树)
问题描述: Trie树,即字典树,又称单词查找树或键树,是一种树形结构,是一种哈希树的变种.典型应用是用于统计和排序大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计.它的优 ...
- [转]数据结构之Trie树
1. 概述 Trie树,又称字典树,单词查找树或者前缀树,是一种用于快速检索的多叉树结构,如英文字母的字典树是一个26叉树,数字的字典树是一个10叉树. Trie一词来自retrieve,发音为/tr ...
随机推荐
- perl 读取json 格式的文件
使用JSON 模块中的 decode_json 函数,将json文件中的数据解码为perl 中的对象,然后进行处理 代码如下: #!/usr/bin/env perl use JSON; use En ...
- 说说PHP中foreach引用的一个坑
From: http://blog.csdn.net/yipiankongbai/article/details/45307767 先来看看下面这段代码: <?php $arr = array( ...
- LAMP架构介绍MySQL、MariaDB介绍 MySQL安装
- SharePoint PowerShell部署开发好的WebPart到服务器上
内容仅供参考,需结合实际需求来处理. =========SharePoint 环境下运行ps1文件,ps1内容如下======= Set-ExecutionPolicy ByPass Add-PSSn ...
- [转]Linux服务器上11种网络连接状态 和 TCP三次握手/四次挥手详解
一.Linux服务器上11种网络连接状态: 图:TCP的状态机 通常情况下:一个正常的TCP连接,都会有三个阶段:1.TCP三次握手;2.数据传送;3.TCP四次挥手. 注:以下说明最好能结合”图:T ...
- 【机翻】Deep Plug-and-Play Super-Resolution for Arbitrary
论文 (PDF) Deep Plug-and-Play Super-Resolution for Arbitrary Blur Kernels https://www.researchgate.net ...
- C++的UML类图
OOAD(object-oriented analysis and design)面向对象分析和设计 UML(Unified Modeling Language)统一建模语言.可以清晰表达任何OOAD ...
- JSP判断闰年
闰年条件: 能被4整除但不能被100整除 能被400整除 <%@ page language="java" import="java.util.*" co ...
- JavaScript实现最简单的拖拽效果
一.一些无关痛痒的唠叨 拖拽还是挺不错的一个页面效果,我个人认为,其生命力在于可以让用户自己做一些操作,所谓自定义.例如: ①浏览器标签顺序的拖拽切换 现在基本上所有的选项卡式的浏览器都有顺序拖拽切换 ...
- Python中dataframe数据框中选择某一列非空的行
利用pandas自带的函数notnull可以很容易判断某一列是否为null类型,但是如果这一列中某一格为空字符串"",此时notnull函数会返回True,而一般我们选择非空行并不 ...