zoukankan      html  css  js  c++  java
  • LeetCode208:Implement Trie (Prefix Tree)

    Implement a trie with insert, search, and startsWith methods.

    Note:
    You may assume that all inputs are consist of lowercase letters a-z.

    Hide Tags Data Structure Trie

    实现一棵Trie树以及实现查询的功能,依据上一篇文章中的分析和伪代码能够非常迅速地实现:
    runtime:68ms

    class TrieNode {
    public:
        // Initialize your data structure here.
        TrieNode() {
                words=0;
                prefixs=0;
                for(int i=0;i<26;i++)
                   edges[i]=NULL; 
            }
            int words;
            int prefixs;
            TrieNode* edges[26];
    };
    
    class Trie {
    public:
        Trie() {
            root = new TrieNode();
        }
    
        // Inserts a word into the trie.
        void insert(string word) {
                insertHelper(root,word,0);
        }
    
        // Returns if the word is in the trie.
        bool search(string word) {
                return searchHelper(root,word,0)!=0;
        }
    
        // Returns if there is any word in the trie
        // that starts with the given prefix.
        bool startsWith(string prefix) {
                return startsWithHelper(root,prefix,0)!=0;
        }
    
        void insertHelper(TrieNode * node,string &word,int pos) {
            if(pos==word.size())
            {
                node->words++;
                node->prefixs++;
            }
            else
            {
                node->prefixs++;
                int char_code=word[pos]-'a';
                if(node->edges[char_code]==NULL)
                    node->edges[char_code]=new TrieNode();
                insertHelper(node->edges[char_code],word,pos+1);
            }
        }
    
        int searchHelper(TrieNode * node,string &word,int pos)
        {
            int char_code=word[pos]-'a';
            if(pos==word.size())
                return node->words;
            else if(node->edges[char_code]==NULL)
                return 0;
            else 
                return searchHelper(node->edges[char_code],word,pos+1);
        }
    
        int startsWithHelper(TrieNode * node,string &word,int pos)
        {
            int char_code=word[pos]-'a';
            if(pos==word.size())
                return node->prefixs;
            else if(node->edges[char_code]==NULL)
                return 0;
            else
                return startsWithHelper(node->edges[char_code],word,pos+1);
        }
    
    private:
        TrieNode* root;
    };
    
    // Your Trie object will be instantiated and called as such:
    // Trie trie;
    // trie.insert("somestring");
    // trie.search("key");
  • 相关阅读:
    技术的那些事儿_2_产品与工艺
    for与foreach再探讨
    技术的那些事儿_3_西方技术管理的精髓
    搭建免费的.Net开发环境
    CDN
    servu 系统服务看门狗,自动脱机补丁,自动启动
    .NET Remoting程序开发入门篇
    网管必知远程终端3389端口合理修改秘藉
    反射方法调用时的一个错误:参数计数不匹配( parameter count mismatch )
    VPS性能测试第一步:CPU测试
  • 原文地址:https://www.cnblogs.com/blfbuaa/p/7121078.html
Copyright © 2011-2022 走看看