zoukankan      html  css  js  c++  java
  • 实现Trie(前缀树)(多叉树)

    题目描述

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

     Trie() 初始化前缀树对象。
     void insert(String word) 向前缀树中插入字符串 word 。
     boolean search(String word) 如果字符串 word 在前缀树中,返回 true(即,在检索之前已经插入);否则,返回 false 。
     boolean startsWith(String prefix) 如果之前已经插入的字符串 word 的前缀之一为 prefix ,返回 true ;否则,返回 false 。
     
    示例:
    输入
    ["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
     
    提示:
     1 <= word.length, prefix.length <= 2000
     word 和 prefix 仅由小写英文字母组成
     insert、search 和 startsWith 调用次数 总计 不超过 3 * 104 次
    链接:https://leetcode-cn.com/problems/implement-trie-prefix-tree
     
    思路分析
    使用多叉树实现前缀树,具体参考:https://leetcode-cn.com/problems/implement-trie-prefix-tree/solution/trie-tree-de-shi-xian-gua-he-chu-xue-zhe-by-huwt/
    实现代码
    //设计一棵多叉树
    class Trie {
    private:
        bool isEnd;
        Trie *next[26];
    public:
        /** Initialize your data structure here. */
        Trie() {
            isEnd = false;
            memset(next, 0, sizeof(next));
        }
        
        /** Inserts a word into the trie. */
        void insert(string word) {
            Trie *ptr = this;
            for(auto ch: word){
                int index = ch - 'a';
                //判断对应的孩子节点是否为空,为空则创建相应的孩子节点
                if(ptr->next[index] == NULL){
                    Trie *node = new Trie();
                    ptr->next[index] = node;
                }
                //然后将指针指向孩子节点
                ptr = ptr->next[index];
            }
            ptr->isEnd = true;
        }
        
        /** Returns if the word is in the trie. */
        bool search(string word) {
            //判断多叉树中是否存在该条路径,不存在则直接返回false,存在则还需要判断最后一个字母是否为节点
            Trie *ptr = this;
            for(auto ch: word){
                int index = ch - 'a';
                if(ptr->next[index] == NULL){
                    return false;
                }
                ptr = ptr->next[index];
            }
            return ptr->isEnd;
        }
        
        /** Returns if there is any word in the trie that starts with the given prefix. */
        bool startsWith(string prefix) {
            //判断多叉树中是否存在该条路径,不存在则直接返回false,不需要判断最后一个字母是否为节点
            Trie *ptr = this;
            for(auto ch: prefix){
                int index = ch - 'a';
                if(ptr->next[index] == NULL){
                    return false;
                }
                ptr = ptr->next[index];
            }
            return true;
        }
    };
  • 相关阅读:
    SharePoint 2019
    SharePoint 2019 图文安装教程
    SharePoint 2016 服务器部署(七)SharePoint 和OOS 集成
    如何将域中的AD数据导入SharePoint
    SharePoint 2016 图文安装教程 后面有激活序列号、密钥分享
    Github上优秀的.NET Core项目
    SQL Server删除/创建复制订阅失败,报15517错误问题的处理
    vue-devtools 开发工具的安装
    laravel 查询数据toArray内层无法转换的问题
    KindEditor 增加html标签
  • 原文地址:https://www.cnblogs.com/latencytime/p/14609628.html
Copyright © 2011-2022 走看看