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;
            }
        }
    }
  • 相关阅读:
    js 产生随机数
    基于bootstrap的multiple-select下拉控件使用
    jqgrid 单列排序和组合排序
    jqgrid 配置行号及行号的宽度
    jqgrid 配置分页大小及下拉项
    jqgrid 滚动分页
    jqgrid 宽度自适应
    jqgrid 行选中multiboxonly属性说明
    jqgrid 谈谈给表格设置列头事件、行事件、内容事件
    jqgrid 加入右键菜单按钮管理
  • 原文地址:https://www.cnblogs.com/self-crossing/p/12964552.html
Copyright © 2011-2022 走看看