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");

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-03 05:40:59

LeetCode208:Implement Trie (Prefix Tree)的相关文章

leetcode Implement Trie (Prefix Tree)

题目连接 https://leetcode.com/problems/implement-trie-prefix-tree/ Implement Trie (Prefix Tree) Description Implement a trie with insert, search, and startsWith methods. 字典树.. class TrieNode { public: // Initialize your data structure here. bool vis; Tri

[LeetCode]Implement Trie(Prefix Tree),解题报告

目录 目录 概述 Trie树基本实现 定义Trie树节点 添加操作 查询word是否在Trie树中 AC完整代码 概述 Trie树,又称为字典树.单词查找树或者前缀树,是一种用于快速检索的多叉数结构.例如,英文字母的字典树是26叉数,数字的字典树是10叉树. Trie树的基本性质有三点,归纳为: 根节点不包含字符,根节点外每一个节点都只包含一个字符. 从根节点到某一节点,路径上经过的字符连接起来,为该节点对应的字符串. 每个节点的所有子节点包含的字符串不相同. Trie树基本实现 我们通过Lee

[LeetCode][JavaScript]Implement Trie (Prefix Tree)

Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith methods. https://leetcode.com/problems/implement-trie-prefix-tree/ 实现字典树,每个节点至多有26个子孙,代表26个字母. 每个节点都有三个属性,key, isWord以及字典(哈希表,提高访问速度,也可以用数组),因为JS可以扩展实例化的对象,直接用下标访问对象,不需

LeetCode 208. 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. 这道题让我们实现一个重要但又有些复杂的数据结构-字典树, 又称前缀树或单词查找树,详细介绍可以参见网友董的博客,例如,一个保存了8个键的trie结构,"A", "to", "tea&quo

Implement Trie (Prefix Tree) 解答

Question Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs are consist of lowercase letters a-z. Solution A trie node should contains the character, its children and the flag that marks if it is a leaf

[Swift]LeetCode208. 实现 Trie (前缀树) | Implement Trie (Prefix Tree)

Implement a trie with insert, search, and startsWith methods. Example: Trie trie = new Trie(); trie.insert("apple"); trie.search("apple"); // returns true trie.search("app"); // returns false trie.startsWith("app");

[leedcode 208] Implement Trie (Prefix Tree)

Trie树又被称为字典树.前缀树,是一种用于快速检索的多叉树.Tried树可以利用字符串的公共前缀来节省存储空间. 但如果系统存在大量没有公共前缀的字符串,相应的Trie树将非常消耗内存.(下图为Wiki上的Trie树示意图, https://en.wikipedia.org/wiki/Trie) 子节点用hashMap表示 isWord标记从根节点到该节点是否表示一个单词,还是另一单词的前缀. Implement a trie with insert, search, and startsWi

208. Implement Trie (Prefix Tree)

题目: Implement a trie with insert, search, and startsWith methods. 链接: http://leetcode.com/problems/implement-trie-prefix-tree/ 7/14/201760%,照着课件代码改写的. 将val cast成boolean,注意第21行需要设为false.以下代码可以不需要看,第一版虽然通过了,但是具体内容并不了解. 1 public class Trie { 2 private s

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. 思路: 之前也没有接触过Trie,百科上查了一下,大概就是词源的问题,N个word有公共前缀,只是后缀不同,可以用树表示. 可以通过递归解决. 代码如下: class TrieNode { // 应该都是private的;只是