首页 技术 正文
技术 2022年11月19日
0 收藏 704 点赞 4,682 浏览 2614 个字

字典树

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

它有三个基本性质:

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

Trie树的分析与实现

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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,564
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,413
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,186
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905