ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

前缀树

2020-02-24 20:05:47  阅读:208  来源: 互联网

标签:node index 前缀 trie nexts 字符串 public


定义

(一)前缀树作用

  • 基本作用

    给定一系列的字符串,判断有没有以某些字符开头的字符串

  • 扩充用法1

    已有一系列字符串中是否包含某个字符串

    方案:在每一个节点上加上一个数据项,该数据项表示由多少字符串是以当前字符结尾的。

  • 扩充用法2

    给定一系列字符串,查询有多少字符串是以当前字符作为前缀的

    方案:在每个节点上再加上一个数据项,该数据项表示加入所有字符串时候划过当前节点的次数;

(二)生成前缀树

将字符串不断的加入前缀树中,默认刚开始树的结构只有头结点。然后在加入新字符串时候,判断当前有没有通向该结点的路径,如果没有就新建否则就复用。

示例问题

  • 一个字符串类型的数组 arr1,另一个字符串类型的数组 arr2,arr2 中有哪些字符是在 arr1 中出现的?请打印。

  • arr2 中有哪些字符是作为 arr1 中某个字符串前缀出现的?请打印。

  • arr2 中有哪些字符是作为 arr1 中某个字符串前缀出现的?请打印 arr2 中出现次数最大的前缀。

实现

package nowcoder.easy.class_07;

public class Code_01_TrieTree {

    public static class TrieNode {
        // 建立的时候经过结点的次数
        public int path;
        // 有多少字符串是以该结点结尾的
        public int end;
        // 一共有多少路,这里是 26 个字母,所有规定了 26 条路,通过标记该路的子节点是否为空来判断树是否存在
        public TrieNode[] nexts;

        public TrieNode() {
            path = 0;
            end = 0;
            nexts = new TrieNode[26];
        }
    }

    public static class Trie {
        private TrieNode root;
// 创建头结点
        public Trie() {
            root = new TrieNode();
        }

        public void insert(String word) {
            if (word == null) {
                return;
            }
            //把word变成char数组
            char[] chs = word.toCharArray();
            TrieNode node = 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 void delete(String word) {
            if (search(word) != 0) {
                char[] chs = word.toCharArray();
                TrieNode node = root;
                int index = 0;
                for (int i = 0; i < chs.length; i++) {
                    index = chs[i] - 'a';
                    // 删除肯定 path -1 ,如果为 0,则下面结点就找不到了,置为空
                    //这里应对的情况是,该节点下面只剩一条单链没有分支了
                    if (--node.nexts[index].path == 0) {
                        node.nexts[index] = null;
                        return;
                    }
                    node = node.nexts[index];
                }
                node.end--;
            }
        }
// 是否包括该字符串(扩展一)
        public int search(String word) {
            if (word == null) {
                return 0;
            }
            char[] chs = word.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];
            }
            //返回end值说明有多少个这个字符串加入过这个前缀树
            return node.end;
        }

        //包含该前缀有多少个
        public int prefixNumber(String pre) {
            if (pre == null) {
                return 0;
            }
            char[] chs = pre.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) {
        Trie trie = new Trie();
        System.out.println(trie.search("zuo"));
        trie.insert("zuo");
        System.out.println(trie.search("zuo"));
        trie.delete("zuo");
        System.out.println(trie.search("zuo"));
        trie.insert("zuo");
        trie.insert("zuo");
        trie.delete("zuo");
        System.out.println(trie.search("zuo"));
        trie.delete("zuo");
        System.out.println(trie.search("zuo"));
        trie.insert("zuoa");
        trie.insert("zuoac");
        trie.insert("zuoab");
        trie.insert("zuoad");
        trie.delete("zuoa");
        System.out.println(trie.search("zuoa"));
        System.out.println(trie.prefixNumber("zuo"));

    }

}

结果为:

0
1
0
1
0
0
3

标签:node,index,前缀,trie,nexts,字符串,public
来源: https://www.cnblogs.com/kristse/p/TrieTree.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有