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;
            }
        }
    }
  • 相关阅读:
    linux 下面 opcache 拓展
    php中函数前加&符号的作用分解
    apache 设置404页面
    nginx 环境搭建使用之入门
    curl获取http请求的状态码
    js打开新的窗体不被浏览器阻止
    YII 框架使用之——创建应用
    Yii 框架创建自己的 web 应用
    PHP中CURL方法curl_setopt()函数的一些参数
    Linux的学习--使用PuTTY
  • 原文地址:https://www.cnblogs.com/self-crossing/p/12964552.html
Copyright © 2011-2022 走看看