前缀树

简介: 前缀树是一个非常好用的数据结构,特别是针对求相同前缀字符串有多少个的时候。如图前缀树.jpg下面是前缀树的一些代码操作。public class TrieTree { //内部类 private class Tri...

前缀树是一个非常好用的数据结构,特别是针对求相同前缀字符串有多少个的时候。如图


img_4edbfe49cea3735b1e566ee16201bc84.jpe
前缀树.jpg

下面是前缀树的一些代码操作。

public class TrieTree {

    //内部类
    private class TrieNode{
        public int path;
        public int end;
        public TrieNode nexts[];

        public TrieNode(){
            this.path = 0;
            this.end = 0;
            this.nexts = new TrieNode[26];//这里假设是26个字母,如果有其他的情况,参照ascii表
        }
    }

    private TrieNode root;

    public TrieTree(){
        this.root = new TrieNode();
    }

    //插入操作
    public void insert(String word){
        if(word == null || word.length() == 0)
            return;
        char[] chs = word.toCharArray();
        TrieNode node = this.root;
        int index = 0;
        for(int i = 0; i < chs.length; i++){
            index = chs[i] - 'a';
            if(node.nexts[index] == null){
                node.nexts[index] = new TrieNode();
            }
            node = node.nexts[index];
            node.path++;
        }
        node.end++;
    }

    //查找操作
    public int search(String word){
        if(word == null || word.length() == 0)
            return 0;
        char[] chs = word.toCharArray();
        TrieNode node = this.root;
        int index = 0;
        for(int i = 0; i < chs.length; i++){
            index = chs[i] - 'a';
            if(node.nexts[index] == null){
                return 0;
            }
            node = node.nexts[index];
        }
        return node.end;
    }

    //删除操作
    public void delete(String word){
        if(search(word) != 0){
            char[] chs = word.toCharArray();
            TrieNode node = this.root;
            int index = 0;
            for(int i = 0; i < chs.length; i++){
                index = chs[i] - 'a';
                if(--node.nexts[index].path == 0){
                    node.nexts[index] = null;
                    return;
                }
                node = node.nexts[index];
            }
            node.end--;
        }
    }

    //判断前缀相同的字符串个数
    public int prefix(String prefix){
        if(prefix == null)
            return 0;
        char[] chs = prefix.toCharArray();
        TrieNode node = root;
        int index = 0;
        for(int i = 0; i < chs.length; i++){
            index = chs[i] - 'a';
            if(node.nexts[index] == null)
                return 0;
            node = node.nexts[index];
        }
        return node.path;
    }

    public static void main(String[] args) {
        TrieTree root = new TrieTree();
        root.insert("abc");
        root.insert("bdc");
        root.insert("abe");
        root.insert("abfg");
        System.out.println(root.prefix("ab"));
        root.delete("abe");
        System.out.println(root.prefix("ab"));
    }
}

目录
相关文章
|
3天前
|
人工智能 运维 安全
|
5天前
|
SpringCloudAlibaba 负载均衡 Dubbo
微服务架构下Feign和Dubbo的性能大比拼,到底鹿死谁手?
本文对比分析了SpringCloudAlibaba框架下Feign与Dubbo的服务调用性能及差异。Feign基于HTTP协议,使用简单,适合轻量级微服务架构;Dubbo采用RPC通信,性能更优,支持丰富的服务治理功能。通过实际测试,Dubbo在调用性能、负载均衡和服务发现方面表现更出色。两者各有适用场景,可根据项目需求灵活选择。
393 124
微服务架构下Feign和Dubbo的性能大比拼,到底鹿死谁手?
|
8天前
|
人工智能 JavaScript 测试技术
Qwen3-Coder入门教程|10分钟搞定安装配置
Qwen3-Coder 挑战赛简介:无论你是编程小白还是办公达人,都能通过本教程快速上手 Qwen-Code CLI,利用 AI 轻松实现代码编写、文档处理等任务。内容涵盖 API 配置、CLI 安装及多种实用案例,助你提升效率,体验智能编码的乐趣。
741 109
|
2天前
|
算法 Python
【轴承故障诊断】一种用于轴承故障诊断的稀疏贝叶斯学习(SBL),两种群稀疏学习算法来提取故障脉冲,第一种仅利用故障脉冲的群稀疏性,第二种则利用故障脉冲的额外周期性行为(Matlab代码实现)
【轴承故障诊断】一种用于轴承故障诊断的稀疏贝叶斯学习(SBL),两种群稀疏学习算法来提取故障脉冲,第一种仅利用故障脉冲的群稀疏性,第二种则利用故障脉冲的额外周期性行为(Matlab代码实现)
229 152
|
4天前
|
Java 数据库 数据安全/隐私保护
Spring 微服务和多租户:处理多个客户端
本文介绍了如何在 Spring Boot 微服务架构中实现多租户。多租户允许单个应用实例为多个客户提供独立服务,尤其适用于 SaaS 应用。文章探讨了多租户的类型、优势与挑战,并详细说明了如何通过 Spring Boot 的灵活配置实现租户隔离、动态租户管理及数据源路由,同时确保数据安全与系统可扩展性。结合微服务的优势,开发者可以构建高效、可维护的多租户系统。
207 127
|
2天前
|
编解码 算法 自动驾驶
【雷达通信】用于集成传感和通信的OFDM雷达传感算法(Matlab代码实现)
【雷达通信】用于集成传感和通信的OFDM雷达传感算法(Matlab代码实现)
180 125
|
2天前
|
JavaScript 关系型数据库 MySQL
基于python的网上外卖订餐系统
本系统基于Python与Flask框架,结合MySQL数据库及Vue前端技术,实现了一个功能完善的网上订餐平台。系统涵盖餐品、订单、用户及评价管理模块,并深入研究订餐系统的商业模式、用户行为与服务质量。技术上采用HTML、PyCharm开发工具,支持移动端访问,助力餐饮业数字化转型。