zoukankan      html  css  js  c++  java
  • 数据结构与算法之前缀树

    public class TrieTree {
    
        public Node root;
    
        public TrieTree(){
            root = new Node(' ');
        }
    
        /** 插入字符串 */
        public void insert(String str){
    
            if(str == null){
                return;
            }
    
            Node node = this.root;
    
            /** 每次进来都要加一 */
            node.pass ++;
    
            char [] chs = str.toCharArray();
            int path;
            for (int i = 0; i < chs.length; i++) {
                path = chs[i] - 'a';
    
                /** 如果没有到达path的节点 那么就新建 */
                if(node.nexts[path] == null){
                    node.nexts[path] = new Node(chs[i]);
                }
    
                node = node.nexts[path];
    
                node.pass ++;
    
            }
    
            /** 最后一个节点收尾 要加一 */
            node.end ++;
    
        }
    
        /** 查找字符串的出现次数 */
        public int search(String str){
            if(str == null){
                return 0;
            }
    
            int path;
            char[] chs = str.toCharArray();
    
            Node node = this.root;
    
            for (int i = 0; i < chs.length; i++) {
    
                path = chs[i] - 'a';
                if(node.nexts[path] == null){
                    return 0;
                }
    
                node = node.nexts[path];
            }
    
            return node.end;
        }
    
        /** 删除字符串 */
        public void delete(String str){
    
            if(str == null){
                return;
            }
    
            /** 删除之前 需要判断是否加入过 */
            if(search(str) == 0){
                return;
            }
    
            int path;
            Node node = this.root;
    
            node.pass --;
            char [] chs = str.toCharArray();
            for (int i = 0; i < chs.length; i++) {
                path = chs[i] - 'a';
    
                /** 要考虑一件事 有可能这次delete后 会有无效的节点残留 */
                /** 如果 next接单的pass值-1后为0了 那么就舍弃掉next之后的(包括next)所有节点 */
                if(--node.nexts[path].pass == 0){
                    node.nexts[path] = null;
                    return;
                }
    
                node = node.nexts[path];
            }
    
            node.end --;
        }
    
    
        public static class Node{
    
            public Node [] nexts;
            public int pass;
            public int end;
            public char c;
    
            public Node(char c){
                this.nexts = new Node[26];
                this.pass = 0;
                this.end = 0;
                this.c = c;
            }
        }
    }
  • 相关阅读:
    .net开发微信(1)——微信订阅号的配置
    工作中EF遇到的问题
    .net Entity Framework初识1
    Razor视图
    jquery中利用队列依次执行动画
    .net找List1和List2的差集
    angularjs ng-if 中的ng-model 值作用域问题
    Spring Boot + JPA(hibernate 5) 开发时,数据库表名大小写问题
    springboot 启动排除某些bean 的注入
    angularjs 初始化方法执行两次以及url定义错误导致传值错误问题
  • 原文地址:https://www.cnblogs.com/self-crossing/p/12964552.html
Copyright © 2011-2022 走看看