TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
Trie树,又称单词查找树或键树,是一种树形结构,是一种哈希树的变种。 典型应用是用于统计和排序大量的字符串(但不仅限于字符串), 所以经常被搜索引擎系统用于文本词频统计。
它的优点是:
利用字符串的公共前缀来节约存储空间,最大限度的减少无谓的字符串比较,查询效率比哈希表高。
比如说我们想储存3个单词,sky、skyline、skymoon。如果只是单纯的按照以前的字符数组存储的思路来存储的话,那么我们需要定义三个字符串数组。但是如果我们用字典树的话,只需要定义一个树就可以了。在这里我们就可以看到字典树的优势了。
它有三个基本性质:
(1)根节点不包含字符;
(2) 除根节点外每一个节点都只包含一个字符:
(3) 从根节点到某一节点,路径上经过的字符连接起来,为该节点对应的字符串,每个节点的所有子节点包含的字符都不相同。


字典树的插入,删除和查找都非常简单,用一个一重循环即可。
1. 从根节点开始一次搜索
2. 取得要查找关键词的第一个字母,并根据该字母选择对应的子树并转到该子树继续进行检索
3. 在相应的子树上,取得要查找关键词的第二个字母,并进一步选择对应的子树进行检索
4. 迭代过程...
5. 在某个节点处,关键词的所有字母已被取出,则读取附在该节点上的信息,即完成查找 例:
Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀).
Input
输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一个提问,每个提问都是一个字符串. 注意:本题只有一组测试数据,处理到文件结束.
Output
对于每个提问,给出以该字符串为前缀的单词的数量.
Sample Input
banana
band
bee
absolute
acm
ba
b
band
abc
Sample Output
2
3
1
0
代码: (字典树模板)- import java.util.LinkedList;
- public class Trie {
- private int SIZE = 26;
- private TrieNode root; //字典树的根
-
- Trie() { //初始化字典树
- root = new TrieNode();
- }
-
- private class TrieNode { //字典树节点
- private int num;//有多少单词通过这个节点,即节点字符出现的次数
- private TrieNode[] son;// 所有的儿子节点
- private boolean isEnd;//是不是最后一个节点
- private char val;// 节点的值
-
-
- TrieNode() {
- num = 1;
- son = new TrieNode[SIZE];
- isEnd = false;
-
- }
- }
- //建立字典树
- public void insert(String str) { //在字典树中插入一个单词
- if (str == null || str.length() == 0) {
- return;
- }
- TrieNode node = root;
- char[] letters=str.toCharArray();
- for (int i = 0, len = str.length(); i < len; i++) {
- int pos = letters[i] - "a";
- if (node.son[pos] == null) {
- node.son[pos] = new TrieNode();
- node.son[pos].val = letters[i];
- } else {
- node.son[pos].num++;
- }
- node = node.son[pos];
- }
- node.isEnd = true;
- }
-
-
- public int countPrefix(String prefix){ //计算单词前缀的数量
- if(prefix==null||prefix.length()==0){
- return -1;
- }
- TrieNode node=root;
- char[] letters=prefix.toCharArray();
- for(int i=0,len=prefix.length();i< len;i++){
- int pos=letters[i]-"a";
- if(node.son[pos]==null){
- return 0;
- }else{
- node=node.son[pos];
- }
- }
- return node.num;
- }
-
- // 在字典树中查找一个完全匹配的单词.
- public boolean has(String str) {
- if (str == null || str.length() == 0) {
- return false;
- }
- TrieNode node = root;
- char[] letters=str.toCharArray();
- for (int i = 0, len = str.length(); i < len; i++) {
- int pos = letters[i] - "a";
- if (node.son[pos] != null) {
- node = node.son[pos];
- } else {
- return false;
- }
- }
- return node.isEnd;
- }
-
-
- //前序遍历字典树.
- public void preTraverse(TrieNode node){
- if(node!=null){
- System.out.print(node.val+"-");
- for(TrieNode child: node.son){
- preTraverse(child);
- }
- }
-
- }
-
- public TrieNode getRoot(){
- return this.root;
- }
-
- public static void main(String[] args) {
- Trie tree = new Trie();
- String[] strs={
- "banana",
- "band",
- "bee",
- "absolute",
- "acm",
- };
- String[] prefix={
- "ba",
- "b",
- "band",
- "abc",
- };
- for(String str : strs){
- tree.insert(str);
- }
- System.out.println(tree.has("abc"));
- tree.preTraverse(tree.getRoot());
- System.out.println();
- //tree.printAllWords();
- for(String pre : prefix){
- int num=tree.countPrefix(pre);
- System.out.println(pre+" "+num);
- }
-
- }
- }
复制代码 运行:

源码下载:http://file.javaxxz.com/2014/12/4/000757281.zip |
|