Java实现字典树-字符串的查找与次数统计

一、简介

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

它有3个基本性质:根节点不包含字符,除根节点外每一个节点都只包含一个字符; 从根节点到某一节点,路径上经过的字符连接起来,为该节点对应的字符串; 每个节点的所有子节点包含的字符都不相同。

其基本操作有:查找、插入和删除,当然删除操作比较少见。

二、代码实现

1、java的实现

package com.yzl.controller;

/**
 * ClassName: Trie
 * Description: 字典树算法的实现
 * date: 2021/5/30 6:33 下午
 *
 * @author yangzhiliang
 */
public class Trie {
   

    private int SIZE=26;
    private TrieNode root;//字典树的根

    /**
     * @description:  字典树构造方法
     * @author: yangzhiliang
     * @date: 2021/5/30
     * @param : null
     * @return: null
     */
    Trie()
    {
   
        root=new TrieNode();
    }

    /**
     * @description:  字典树节点类
     * @author: yangzhiliang
     * @date: 2021/5/30
     */

    private class TrieNode
    {
   
        //有多少单词通过这个节点,即由根至该节点组成的字符串模式出现的次数
        private int num;
        //所有的子节点
        private TrieNode[]  son;
        //是不是最后一个节点
        private boolean isEnd;
        //节点的值
        private char val;
        //是否有子节点
        private boolean haveSon;

        /**
         * @description:  字典树节点类构造方法
         * @author: yangzhiliang
         * @date: 2021/5/30
         * @param : null
         * @return: null
         */
        TrieNode()
        {
   
            num=1;
            son=new TrieNode[SIZE];
            isEnd=false;
            haveSon=false;
        }
    }

    /**
     * @description:  字典树节点插入方法
     * @author: yangzhiliang
     * @date: 2021/5/30
     * @param str: 要插入的字符串
     * @return: null
     */
    public void insert(String str)
    {
   
        //当插入的字符串为空,或字符串的长度为0,直接返回
        if(str==null||str.length()==0)
        {
   
            return;
        }
        //新建一个节点实例
        TrieNode node=root;
        //将字符串转化为字节数组
        char[] letters = str.toCharArray();
        //循环遍历字符串
        for(int i=0,len=str.length(); i<len; i++)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
字典树Java实现可以使用递归来构建。首先需要定义一个TrieNode类,其中包含一个Map用于存储子节点,以及一个布尔值表示当前节点是否是一个单词的结尾。然后,定义一个Trie类来维护根节点。在Trie类中实现insert、search和startsWith方法。 下面是一个简单的字典树Java代码实现示例: ```java class TrieNode { Map<Character, TrieNode> children; boolean isEndOfWord; public TrieNode() { children = new HashMap<>(); isEndOfWord = false; } } class Trie { private TrieNode root; public Trie() { root = new TrieNode(); } public void insert(String word) { TrieNode current = root; for (int i = 0; i < word.length(); i++) { char ch = word.charAt(i); TrieNode node = current.children.get(ch); if (node == null) { node = new TrieNode(); current.children.put(ch, node); } current = node; } current.isEndOfWord = true; } public boolean search(String word) { TrieNode current = root; for (int i = 0; i < word.length(); i++) { char ch = word.charAt(i); TrieNode node = current.children.get(ch); if (node == null) { return false; } current = node; } return current.isEndOfWord; } public boolean startsWith(String prefix) { TrieNode current = root; for (int i = 0; i < prefix.length(); i++) { char ch = prefix.charAt(i); TrieNode node = current.children.get(ch); if (node == null) { return false; } current = node; } return true; } } ``` 这是一个简单的Trie字典树Java代码实现,其中包括了插入、查询和查询前缀三个常用操作。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值