1804. 实现 Trie (前缀树) II

前缀树(trie ,发音为 "try")是一个树状的数据结构,用于高效地存储和检索一系列字符串的前缀。前缀树有许多应用,如自动补全和拼写检查。

实现前缀树 Trie 类:

Trie() 初始化前缀树对象。
void insert(String word) 将字符串 word 插入前缀树中。
int countWordsEqualTo(String word) 返回前缀树中字符串 word 的实例个数。
int countWordsStartingWith(String prefix) 返回前缀树中以 prefix 为前缀的字符串个数。
void erase(String word) 从前缀树中移除字符串 word 。
 

示例 1:

 

Trie trie = new Trie();
trie.insert("apple"); // 插入 "apple"。
trie.insert("apple"); // 插入另一个 "apple"。
trie.countWordsEqualTo("apple"); // 有两个 "apple" 实例,所以返回 2。
trie.countWordsStartingWith("app"); // "app" 是 "apple" 的前缀,所以返回 2。
trie.erase("apple"); // 移除一个 "apple"。
trie.countWordsEqualTo("apple"); // 现在只有一个 "apple" 实例,所以返回 1。
trie.countWordsStartingWith("app"); // 返回 1
trie.erase("apple"); // 移除 "apple"。现在前缀树是空的。
trie.countWordsStartingWith("app"); // 返回 0

 

public class TrieTree {
    public static class TrieNode{
       public int pass;
       public int end;
      // public HashMap<Character,TrieNode> map=new HashMap<>();
       //a->0,b->1,z->25
       public TrieNode[] nexts=new TrieNode[26];
    }
    public static class Trie{
        private TrieNode root;
        public Trie(){
            root=new TrieNode();
        }

        /**
         遍历所有字符,如果该字符没有路了,则建出node,pass++
         到最后一个字符时end++
         时间复杂度是:所有字符的数量
         */
        public void insert(String word){
            if(word==null){
                return;
            }
            char[] chs=word.toCharArray();
            TrieNode node=root;
            node.pass++;
            int index;
            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++;
        }

        /**
        查word加入了几次
         O(K)
         这个hashmap也可以
         */
        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;
        }

        /**

          有多少个字符串以word为前缀的,这个hashMap做不到
          时间复杂度:O(K)
          沿途找,返回最后一个字符node的pass
         */
        public int preNumber(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.pass;
        }

        /**
         先保证加入过才能删除
         沿途pass--
         最后end--
         注意:如果pass减为0时该怎么办?
         把这条路径标为null,jvm会自动回收这个node
         */
        public void delete(String word){
            if(search(word)==0){
                return;
            }
            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){
                    //如果pass=0则
                    node.nexts[index]=null;
                    return;
                }
                node=node.nexts[index];
            }
            node.end--;

        }
    }
}

  

posted @ 2021-09-01 13:35  sherry001  阅读(184)  评论(0编辑  收藏  举报