前缀树

前缀树

使用一个树结构记录一组数据,数据可以是字符串数组。

public class TrieTree {
    public static class TrieNode{
        public int pass;//经过该节点的字符的数量
        public int end;//以该节点为终的字符的数量
        public TrieNode[] nexts;

        public TrieNode(){
            pass = 0;
            end = 0;
            //nexts[0] == null 没有走向'a'的路
            //nexts[0] != null 有走向'a'的路
            //...
            //nexts[25] != null 有走向'z'的路
            nexts = new TrieNode[26]; //HashMap<Char, Node> nexts;表示很多的字符,
                                      // Char表示某个字符,Node表示下一个节点
                                      //TreeMap<Char, Node> nexts;有序表
        }

    }

    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;
            node.pass++;//边的end的pass++
            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.pass++;
            }
            node.end++;
        }

        //删除前缀树中的某个单词,删一个
        public void delete(String word){
            if(search(word) != 0){
                char[] chs = word.toCharArray();
                TrieNode node = root;
                node.pass--;
                int index = 0;
                for (int i = 0; i < chs.length; i++) {
                    index = chs[i] - 'a';
                    if(--node.nexts[index].pass == 0){
                        node.nexts[index] = null;
                        return;
                    }
                    node = node.nexts[index];
                }
                node.end--;
            }
        }

        //查看前缀树里有多少word
        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];
            }
            return node.end;
        }
        //所有加入的字符串中,有几个是以pre这个字符串为前缀的
        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.pass;
        }
    }
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 本文只是自己的笔记,并不具备过多的指导意义。 前缀树 何为前缀树 前缀树又名字典树,单词查找树,Trie树,是一种...
    kirito_song阅读 4,554评论 0赞 13
  • 什么是字典树 是一种树形结构,是一种哈希树的变种。典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),...
    叫我胖虎大人阅读 509评论 0赞 4
  • 前缀树是什么 前缀树是一种树结构,其中的键通常是字符串。与二叉查找树不同,键不是直接保存在节点中,而是由节点在树中...
    名字是乱打的阅读 709评论 0赞 1
  • 对字典树的理解: a.Trie字典树又可以称为前缀树,是一种真正为字典设计的数据结构,其中的核心实现就包含了字典M...
    SeekerLinJunYu阅读 350评论 0赞 0
  • 前缀树说明 前缀树Trie是一种用于字符串搜索的树形数据结构。 我们举个例子来说明前缀树是如何表示的。 有三个单词...
    愈强阅读 799评论 0赞 0

友情链接更多精彩内容