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的;只是为了减少代码量
    // Initialize your data structure here.
    public Map<Character,TrieNode> map;//存放后缀
    public char val;//当前节点的字符值
    public boolean tail;//一个字符串以该节点结尾
    public TrieNode() {
        map = new HashMap<>();
        tail=false;
    }
    public TrieNode(char c) {
        map = new HashMap<>();
        this.val=c;
        tail=false;
    }
}

public class Trie {
    private TrieNode root;

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

    // Inserts a word into the trie.
    public void insert(String word) {
        insert(root,word);
    }

    private void insert(TrieNode root,String word){
        if(word.length()==0){
            root.tail=true;
            return;
        }
        TrieNode cur=root;
        char c=word.charAt(0);
        if(cur.map.containsKey(c)){
            TrieNode child = cur.map.get(c);
            insert(child,word.substring(1));
        }else{
            TrieNode child = new TrieNode(c);
            cur.map.put(c,child);
            insert(child,word.substring(1));
        }
    }

    // Returns if the word is in the trie.
    public boolean search(String word) {
        return search(root,word);
    }

    private boolean search(TrieNode root,String word){
        if(word.length()==0)
            return root.tail==true;
        char c = word.charAt(0);
        if(!root.map.containsKey(c))
            return false;
        TrieNode child = root.map.get(c);
        return search(child,word.substring(1));
    }

    // Returns if there is any word in the trie
    // that starts with the given prefix.
    public boolean startsWith(String prefix) {
         return startsWith(root,prefix);
    }

    private boolean startsWith(TrieNode root,String prefix) {
        if(prefix.length()==0)
            return true;
        char c = prefix.charAt(0);
        if(!root.map.containsKey(c))
            return false;
        TrieNode child = root.map.get(c);
        return startsWith(child,prefix.substring(1));
    }
}

// Your Trie object will be instantiated and called as such:
// Trie trie = new Trie();
// trie.insert("somestring");
// trie.search("key");
时间: 2024-10-09 19:46:57

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

[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) - LeetCode

Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs are consist of lowercase letters a-z. 思路:应该就是字典树. 1 class TrieNode { 2 public: 3 TrieNode *dic[27]; 4 // Initialize your data structure here. 5 TrieNode

[LeetCode] 208. Implement Trie (Prefix Tree) Java

题目: Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs are consist of lowercase letters a-z. 题意及分析:实现一个字典树或者叫前缀树的插入.删除和startsWith判断.这里定义一个trieNode类作为字典树的节点类.然后有一个chilrden数组保存子节点,一个isWord变量来判断从根节点到该节点是否是一