Trie (prefix tree) &(Lc208) Implement Trie (Medium)

  1. Trie介绍:https://www.geeksforgeeks.org/trie-insert-and-search/
  2. 一种树结构,通常用于存储字符,对字符进行预处理 (比如生成dictionary),然后再进行其他判断
  3. 一盘root为空,每个node就是string中的一个character
  4. 每个node有一个field代表其是否是字符串中的最后一个字符:isEndOfWord
  5. 每个node可能有多个子节点:children[]

一个典型TrieNode结构

static class TreeNode {
    TreeNode children[];
    boolean isWord;
    
    public TreeNode() {
        children = new TreeNode[26];
    }
}

  1. Trie与HashTable的对比,可以发现Trie可以解决Prefix这个hashtable无法解决的问题
image.png

Trie的2个基本操作: insert, search

LeetCode 208. 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"); // returns true
trie.insert("app");   
trie.search("app");     // returns true

Note:

  • You may assume that all inputs are consist of lowercase letters a-z.
  • All inputs are guaranteed to be non-empty strings.
class Trie {
    
    static class TrieNode {
        TrieNode children [];
        boolean isEndOfWord;
        
        public TrieNode () {
            children = new TrieNode [26];
        }
    }
    
    private TrieNode root;

    /** Initialize your data structure here. */
    public Trie() {
        root = new TrieNode ();
    }
    
    /** Inserts a word into the trie. */
    public void insert(String word) {
        TrieNode node = root;
        
        for (char ch : word.toCharArray ()) {
            if (node.children [ch - 'a'] == null) {
                node.children [ch - 'a'] = new TrieNode ();
            }
            
            node = node.children [ch - 'a'];
        }
        
        node.isEndOfWord = true;
    }
    
    /** Returns if the word is in the trie. */
    public boolean search(String word) {
        TrieNode node = root;
        
        for (char ch : word.toCharArray ()) {
            if (node.children [ch - 'a'] == null) {
                return false;
            }
            
            node = node.children [ch - 'a'];
        }
        
        return node.isEndOfWord;
    }
    
    /** Returns if there is any word in the trie that starts with the given prefix. */
    public boolean startsWith(String prefix) {
        TrieNode node = root;
        
        for (char ch : prefix.toCharArray ()) {
            if (node.children [ch - 'a'] == null) {
                return false;
            }
            
            node = node.children [ch - 'a'];
        }
        
        return true;
    }
}

/**
 * Your Trie object will be instantiated and called as such:
 * Trie obj = new Trie();
 * obj.insert(word);
 * boolean param_2 = obj.search(word);
 * boolean param_3 = obj.startsWith(prefix);
 */
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • LeetCode 刷题随手记 - 第一部分 前 256 题(非会员),仅算法题,的吐槽 https://leetc...
    蕾娜漢默阅读 17,911评论 2 36
  • 为什么要读书?从不再需要读书起,我开始问自己这个问题。“不再需要读书”指的意思我想大家都懂。 之所以有这个问题,第...
    我是小曲阅读 260评论 0 0
  • 尊敬的教练、班委,各位亲爱的伙伴,大家早上好! 非常荣幸由我代表宣传部来做此次会议的分享。我先进行一下自我介绍我的...
    A昕婧阅读 151评论 0 0
  • 马师傅裱画可慢了,因为业务特别多。他的小作坊,在交道口北二条胡同里,连个店名也没有(或者有吧,反正我没看到)。每次...
    空森林阅读 432评论 0 0
  • 【0324读书感悟】3304-晓阳 书名:《财富自由之路》 作者:李笑来 金句:“证明自己正确”并不是学习的任务和...
    阿凌_ee52阅读 133评论 0 1