算法-前缀树,将单词以数据结构的形式保存。实现查找添加删除的方法

什么是前缀树

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

核心思想

Trie的核心思想是空间换时间。利用字符串的公共前缀来降低查询时间的开销以达到提高效率的目的。

前缀树的基本性质:

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

案例: 加入abd ,abc 和 ab.
在这里插入图片描述### 代码实现
1.先定义数据结构

  class Node{
  
            public int pass;//经过的次数
            public int end;//结束的次数
            public HashMap<Character,Node> map; //保存节点信息

            public Node() {
                pass = 0;
                end = 0;
                map = new HashMap();
            }
        }

2.插入方法

 class Trie{
        public Node root;

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

        public void insert(String world){
            if (null == world || world.equals("")){
                return ;
            }
            char[] chars = world.toCharArray();

            Node node = root;
            node.pass++;
            for (int i = 0; i < chars.length;i++){
                Character index = chars[i];
                if (!node.map.containsKey(index)){
                    node.map.put(index,new Node());
                }
                node = node.map.get(index);
                node.pass++;
            }
            node.end++;
        }
}

3.查找单词出现了几次

  /**
         * 查询有几个单词
         * @param world
         * @return
         */
        public int countWorld(String world){
            if (null == world || world.equals("")){
                return 0;
            }
            Node node = root;
            char[] chars = world.toCharArray();
            for (int i = 0; i < chars.length;i++){
                Character index = chars[i];
                if (!node.map.containsKey(index)){
                    return 0;
                }
                node = node.map.get(index);
            }
            return node.end;
        }

4.查找以某个字符创为开头的单词出现了几次

     /**
         * 查询有几个以world开头的单词
         * @param world
         * @return
         */
        public int countPrefix(String world){
            if (null == world || world.equals("")){
                return 0;
            }
            Node node = root;
            char[] chars = world.toCharArray();
            for (int i = 0; i < chars.length;i++){
                Character index = chars[i];
                if (!node.map.containsKey(index)){
                    return 0;
                }
                node = node.map.get(index);
            }
            return node.pass;
        }

5.删除一个字符串

           /**
         * 删除一个单词
         * @param word
         */
        public void deleteWword(String word){

            if (countWorld(word) > 0){
                char[] chars = word.toCharArray();
                Node node = root;
                node.pass--;
                for (int i = 0; i < chars.length;i++){
                    Character path = chars[i];
                    if (--node.map.get(path).pass == 0){
                        node.map.remove(path);
                        return;
                    }
                    node = node.map.get(path);
                }
                node.end--;
            }
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值