• 游戏思考20:前缀树用途、实现及优化


    一、前缀树用途

    查找字符串是否存在,比如查找某个工会名字是否存在,查找某个人名是否存在,但是删除的话比较麻烦,因为可能删掉前缀相同但是别人的名字(类似redis的布隆过滤器,猜测是否命中,但是不一定就有)

    二、前缀树代码(以leetcode原题举例)

    1)题描述

    Trie(发音类似 “try”)或者说 前缀树 是一种树形数据结构,用于高效地存储和检索字符串数据集中的键。这一数据结构有相当多的应用情景,例如自动补完和拼写检查。

    请你实现 Trie 类:

    • Trie() 初始化前缀树对象。
    • void insert(String word) 向前缀树中插入字符串 word 。
    • boolean search(String word) 如果字符串 word 在前缀树中,返回 true(即,在检索之前已经插入);否则,返回 false 。
    • boolean startsWith(String prefix) 如果之前已经插入的字符串 word 的前缀之一为 prefix ,返回 true ;否则,返回 false 。

    2)传入参数

    • 示例
    输入
    ["Trie", "insert", "search", "search", "startsWith", "insert", "search"]
    [[], ["apple"], ["apple"], ["app"], ["app"], ["app"], ["app"]]
    输出
    [null, null, true, false, true, null, true]
    
    解释
    Trie trie = new Trie();
    trie.insert("apple");
    trie.search("apple");   // 返回 True
    trie.search("app");     // 返回 False
    trie.startsWith("app"); // 返回 True
    trie.insert("app");
    trie.search("app");     // 返回 True
    
    来源:力扣(LeetCode)
    链接:https://leetcode.cn/problems/implement-trie-prefix-tree
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
    
    /**
     * Your Trie object will be instantiated and called as such:
     * Trie* obj = new Trie();
     * obj->insert(word);
     * bool param_2 = obj->search(word);
     * bool param_3 = obj->startsWith(prefix);
     */
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    3)代码及解说

    class Trie {
    private:
        vector<Trie*> _child;
        bool _isEnd;
    
        Trie* _getNode(string word)
        {
            Trie* tr = this;
            for(auto&s:word)
            {
                if(!tr->_child[s-'a'])
                    return nullptr;
                tr = tr->_child[s - 'a'];
            }
            return tr;
        }
    
    public:
        Trie()
        :_child(26), _isEnd(false)
        {
            
        }
        
        //
        void insert(string word) 
        {
            Trie* tr = this;
            for(auto& n:word)
            {
                if(tr->_child[n-'a'] == nullptr)
                    tr->_child[n-'a'] = new Trie();
                tr = tr->_child[n-'a'];
            }
            tr->_isEnd = true;
        }
        
        //搜索
        bool search(string word) 
        {
            Trie* node = _getNode(word);
            return node &&  node->_isEnd;
        }
        
        //前序匹配
        bool startsWith(string prefix)
        {
            return _getNode(prefix) != nullptr;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50

    4)可以优化的点

    分配的内存的时候存在数组里面,但是如果这段数据是放在共享内存的话,不该放在std容器里面,因为这个进程一但宕机,从共享内存恢复的话,std::vector的容器会失效,幸运的话会指向乱码的区域,糟糕的话会再宕机一次

    • 解决方法
      1)std::vector换成自制的容器
      2)新建的节点换成唯一ID存储或是存在对象池内,这样无论是restore回来还是进程结束,都方便管理
  • 相关阅读:
    cmd、conhost退居二线,Win 11将设置默认终端
    请求传参.
    千亿体培市场进入快车道
    windowsAPI程序设计菜单栏设计BUG
    图的数据结构
    数电学习(五、触发器)(一)
    5V*0.5A低压降二极管芯片 CH213
    Explore EPF021D ADC微控制器
    猿创征文|超实用的前端开发工具分享
    ceres解析导数(Analytic Derivatives)进阶
  • 原文地址:https://blog.csdn.net/weixin_43679037/article/details/126180479