算法-數據結構-實現 Trie (前綴樹)

算法-數據結構-實現 Trie (前綴樹)

1 題目概述

1.1 題目出處

https://leetcode-cn.com/problems/implement-trie-prefix-tree/

1.2 題目描述

實現一個 Trie (前綴樹),包含 insert, search, 和 startsWith 這三個操作。

示例:

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
說明:

你可以假設所有的輸入都是由小寫字母 a-z 構成的。
保證所有輸入均爲非空字符串。

2 遞歸版

2.1 思路

2.2 代碼

class Trie {
    private Trie[] children;
    private boolean is_end;
    /** Initialize your data structure here. */
    public Trie() {
        children = new Trie[26];
    }
    
    /** Inserts a word into the trie. */
    public void insert(String word) {
        if(word == null || word.trim().length() == 0){
            return;
        }
        char first = word.charAt(0);
        int index = first - 'a';
        Trie child = children[index];
        if(child == null){
            child = new Trie();
            children[index] = child;
        }

        word = word.substring(1);
        if(word.length() > 0){
            child.insert(word);
        }else{
            child.is_end = true;
        }
    }
    
    /** Returns if the word is in the trie. */
    public boolean search(String word) {
        if(word == null || word.trim().length() == 0){
            return true;
        }
        char first = word.charAt(0);
        int index = first - 'a';
        Trie child = children[index];
        if(child == null){
            return false;
        }

        word = word.substring(1);
        if(word.length() > 0){
            return child.search(word);
        }else{
            return child.is_end == true;
        }
    }
    
    /** Returns if there is any word in the trie that starts with the given prefix. */
    public boolean startsWith(String prefix) {
        if(prefix == null || prefix.trim().length() == 0){
            return true;
        }
        char first = prefix.charAt(0);
        int index = first - 'a';
        Trie child = children[index];
        if(child == null){
            return false;
        }

        prefix = prefix.substring(1);
        if(prefix.length() > 0){
            return child.startsWith(prefix);
        }else{
            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);
 */

2.3 時間複雜度

  • insert
    O(L)
    因爲在一個節點查找一個字符,直接用的數組下標查找,時間複雜度爲O(1)。L表示目標單詞長度。
  • search
    O(L)
  • startsWith
    O(L)

2.4 空間複雜度

  • insert
    O(L)
    最壞情況。新插入的單詞和之前的完全沒有公共前綴,需要創建L個Trie對象
  • search
    O(L)
    遞歸L次
  • startsWith
    O(L)
    遞歸L次

2 循環版

2.1 思路

遍歷單詞的每個字符,每次根據char-'a'從數組取child,取不到就說明不存在

2.2 代碼

class Trie {
    private Trie[] children;
    private boolean is_end;
    /** Initialize your data structure here. */
    public Trie() {
        children = new Trie[26];
    }
    
    /** Inserts a word into the trie. */
    public void insert(String word) {
        Trie current = this;
        for(char c : word.toCharArray()){
            int index = c - 'a';
            Trie child = current.children[index];
            if(child == null){
                child = new Trie();
                current.children[index] = child;
            }
            current = child;
        }
        current.is_end = true;
    }
    
    /** Returns if the word is in the trie. */
    public boolean search(String word) {
        Trie current = this;
        for(char c : word.toCharArray()){
            current = current.children[c - 'a'];
            if(current == null){
                return false;
            }
        }
        return current.is_end == true;
    }
    
    /** Returns if there is any word in the trie that starts with the given prefix. */
    public boolean startsWith(String prefix) {
        Trie current = this;
        for(char c : prefix.toCharArray()){
            current = current.children[c - 'a'];
            if(current == null){
                return false;
            }
        }
        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);
 */

3.3 時間複雜度

在這裏插入圖片描述

  • insert
    O(L)
    因爲在一個節點查找一個字符,直接用的數組下標查找,時間複雜度爲O(1)。L表示目標單詞長度。
  • search
    O(L)
  • startsWith
    O(L)

3.4 空間複雜度

  • insert
    O(L)
    最壞情況。新插入的單詞和之前的完全沒有公共前綴,需要創建L個Trie對象
  • search
    O(1)
  • startsWith
    O(1)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章