前缀树

作者: shoulda | 来源:发表于2018-07-17 09:57 被阅读0次

前缀树原理

//节点定义
public static class TrieNode {
        public int path;  //相当于这个节点的子节点有几个
        public int end;//以这个节点结尾的有多少个
        public TrieNode[] map;

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

public static class Trie {
        private TrieNode root;

        public Trie() {
            root = new TrieNode();
        }

        //插入元素
        public void insert(String word) {
            if (word == null) {
                return;
            }
            char[] chs = word.toCharArray();
            TrieNode node = root;
            int index = 0;
            for (int i = 0; i < chs.length; i++) {
                index = chs[i] - 'a';
                if (node.map[index] == null) {
                    node.map[index] = new TrieNode();
                }
                node = node.map[index];
                node.path++;
            }
            node.end++;
        }
       //删除元素
        public void delete(String word) {
            if (search(word)) {
                char[] chs = word.toCharArray();
                TrieNode node = root;
                int index = 0;
                for (int i = 0; i < chs.length; i++) {
                    index = chs[i] - 'a';
                    if (node.map[index].path-- == 1) {
                        node.map[index] = null;
                        return;
                    }
                    node = node.map[index];
                }
                node.end--;
            }
        }
//查询元素
        public boolean search(String word) {
            if (word == null) {
                return false;
            }
            char[] chs = word.toCharArray();
            TrieNode node = root;
            int index = 0;
            for (int i = 0; i < chs.length; i++) {
                index = chs[i] - 'a';
                if (node.map[index] == null) {
                    return false;
                }
                node = node.map[index];
            }
            return node.end != 0;
        }

        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.map[index] == null) {
                    return 0;
                }
                node = node.map[index];
            }
            return node.path;
        }
    }

    public static void main(String[] args) {
        Trie trie = new Trie();
        System.out.println(trie.search("xuda"));
        trie.insert("xuda");
        System.out.println(trie.search("xuda"));
        trie.delete("xuda");
        System.out.println(trie.search("xuda"));
        trie.insert("xuda");
        trie.insert("xuda");
        trie.delete("xuda");
        System.out.println(trie.search("xuda"));
        trie.delete("xuda");
        System.out.println(trie.search("xuda"));
        trie.insert("xudaa");
        trie.insert("xudac");
        trie.insert("xudad");
        trie.insert("xudaw");
        trie.delete("xudacc");
        System.out.println(trie.search("xudas"));
        System.out.println(trie.prefixNumber("xuda"));

    }
****题目:
1.arr2中有哪些字符, 是arr1中出现的? 请打印
2.arr2中有哪些字符, 是作为arr1中某个字符串前缀出现的? 请
打印

相关文章

  • 前缀树(字典树/Trie)Java实现和应用

    摘要: 前缀树,字典树,插入查询逻辑,Java实现,时间复杂度分析 前缀树介绍 Trie树又被称为前缀树、字典树,...

  • 数据结构基础--前缀树&&后缀树

    本文只是自己的笔记,并不具备过多的指导意义。 前缀树 何为前缀树 前缀树又名字典树,单词查找树,Trie树,是一种...

  • 前缀树

    前缀树又名Tries树、字典树、单词查找树等,常用于快速检索,大量字符串的排序和统计等。 三个基本性质 根节点不包...

  • 前缀树

    前缀树原理

  • 前缀树

    概念: 简述:又名单词查找树,tries树,一种多路树形结构,常用来操作字符串(但不限于字符串),和hash效率有...

  • 前缀树

    题目 给定一个字符串数组,其中不含有重复字符串,判断是否有字符串是另一个字符串的前缀 思路 实现前缀树即可,判断是...

  • 前缀树

    1.什么是前缀树 前缀树是N叉树的一种特殊形式。通常来说,一个前缀树是用来存储字符串的。前缀树的每一个节点代表一个...

  • 前缀树

    最近看代码,发现了一个敏感词检测是用前缀树写的,看起来速度蛮快,毕竟是拿空间换时间,LOG倍速。但是缺点也很明显,...

  • 前缀树

  • 前缀树

    问题 已知一个字符串数组words=['accommodate','accompany','accord','ac...

网友评论

      本文标题:前缀树

      本文链接:https://www.haomeiwen.com/subject/akbipftx.html