前缀树

介绍

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

代码

/**
 * 前缀树
 */
public class trie {
    public static class Node {
        public int pass; //通过该节点的单词数
        public int end; //以该位置为结束点
        public HashMap<Integer, Node> nexts;

        public Node() {
            pass = 0;
            end = 0;
            nexts = new HashMap<>();
        }
    }

    public static class Trie2 {
        private Node root;

        public Trie2() {
            root = new Node();
        }

        public void insert(String word) {
            if (null == word) {
                return;
            }
            char[] chars = word.toCharArray();
            int index;
            Node node = root;
            for (int i = 0; i < chars.length; i++) {
                index = chars[i];
                if (!node.nexts.containsKey(index)) {
                    node.nexts.put(index, new Node());
                }
                // 向下前进一步
                node = node.nexts.get(index);
                node.pass++;
            }
            node.end++;
        }

        public void delete(String word) {
            if (null == word) {
                return;
            }
            if (search(word) != 0) {
                char[] chars = word.toCharArray();
                int index;
                Node node = root;
                for (int i = 0; i < chars.length; i++) {
                    index = chars[i];
                    if (--node.nexts.get(index).pass == 0){
                        node.nexts.remove(index);
                    }
                    node = node.nexts.get(index);
                }
                // 去除标识
                node.end--;
            }
        }

        // word这个单词之前加入过几次
        public int search(String word) {
            if (null == word) {
                return 0;
            }
            char[] chars = word.toCharArray();
            int index;
            Node node = root;
            for (int i = 0; i < chars.length; i++) {
                index = chars[i];
                if (!node.nexts.containsKey(index)){
                    return 0;
                }
                node = node.nexts.get(index);
            }
            return node.end;
        }

        // 所有加入的字符串中,有几个是以pre这个字符串作为前缀的
        public int prefixNumber(String pre) {
            if (null == pre) {
                return 0;
            }
            char[] chars = pre.toCharArray();
            int index;
            Node node = root;
            for (int i = 0; i < chars.length; i++) {
                index = chars[i];
                if (!node.nexts.containsKey(index)){
                    return 0;
                }
                node = node.nexts.get(index);
            }
            return node.pass;
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值