前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >208. 实现 Trie (前缀树)

208. 实现 Trie (前缀树)

作者头像
名字是乱打的
发布2021-12-24 08:43:12
3030
发布2021-12-24 08:43:12
举报
文章被收录于专栏:软件工程

思路:

这是一种叫做 单词查找树 的结构。它由字符串键中所有的字符构造而成,允许使用被查找键中的字符进行查找。其中包括插入、查找、删除,寻找前缀等操作. [站外图片上传中...(image-832521-1636112029289)]

代码:

代码语言:javascript
复制
class Trie {
        //定义前缀树结点结构
        public class TrieNode {
            //表示当前节点所能链接到其他节点的个数(在删除操作中会用到)
            public int path;
            //表示以当前节点为结尾的单词的个数
            public int end;
            //表示当前节点能链接到的所有节点。
            public HashMap<Character, TrieNode> next;

            public TrieNode() {
                path = 0;
                end = 0;
                next = new HashMap<>();
            }
        }

        //跟结点
        private TrieNode root;

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

        public void insert(String word) {
            if(word == null || word.length()==0) {
                return ;
            }
            TrieNode node=root;
            for (int i = 0; i < word.length(); i++) {
                char ch=word.charAt(i);
                if (!node.next.containsKey(ch)){
                    node.next.put(ch,new TrieNode());
                }
                node = node.next.get(ch);
                node.path++;
            }
            node.end++;
        }

        public boolean search(String word) {
            if(word==null||word.length()==0) {
                return false;
            }

            TrieNode node=root;
            for (int i = 0; i < word.length(); i++) {
                char currChar=word.charAt(i);
                if (!node.next.containsKey(currChar)){
                    return false;
                }else{
                    node=node.next.get(currChar);
                }
            }
            //如果这个没有以此为截至结点的就返回false
            if (node.end==0){
                return false;
            }
            return true;
        }

        public boolean startsWith(String prefix) {
            if(prefix==null||prefix.length()==0) {
                return false;
            }

            TrieNode node=root;
            for (int i = 0; i < prefix.length(); i++) {
                char currChar=prefix.charAt(i);
                if (!node.next.containsKey(currChar)){
                    return false;
                }else{
                    node=node.next.get(currChar);
                }
            }
            return true;
        }
    }

补充

如果我们有delete操作 delete操作同上述操作大致相同,这里需要使用到path变量,回忆一下,path:表示当前节点所能链接到其他节点的个数。还以五个单词为例,例如删除'the',当匹配到‘t’时,由于进行path--操作后,此时判断path为0,过可直接将当前节点置空,后面的数据则不用再去遍历即可。java中的垃圾回收机制会处理其他的被断开的节点,如果在C++中,则需要全部匹配,之后调用析构函数去操作

代码语言:javascript
复制
public void delete(String word){
    if(word == null || word.equals("") || !search(word)) return ;
    TrieNode node = root;
    for (int i = 0; i < word.length(); i++) {
        char ch = word.charAt(i);
        if(--node.next.get(ch).path == 0){
            node.next.remove(ch);
            return;
        }
        node = node.next.get(ch);
    }
    node.end--;
}

参考作者:Geralt_U 链接:https://leetcode-cn.com/problems/implement-trie-prefix-tree/solution/208-shi-xian-trie-qian-zhui-shu-bao-gua-insert-sea/

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021/11/5 下,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 思路:
  • 代码:
  • 补充
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档