Untitled

 avatar
unknown
plain_text
16 days ago
3.2 kB
3
Indexable
class Trie {
        Trie[] links;
        boolean flag;
        public Trie()
        {
            links = new Trie[26];
            flag = false;
        }

        // blocks are visualized as a key,value hashmap/arr of size 26
        boolean containsKey(char ch) {
            return links[ch - 'a'] != null;
        }

        void put(char ch, Trie newTrie) {
            links[ch - 'a'] = newTrie;
        }

        // Get the Trie with a specific
        // key (letter) from the Trie
        Trie get(char ch) {
            return links[ch - 'a'];
        }

        // Set the current Trie
        // as the end of a word
        void setWordEndFlag() {
            flag = true;
        }

        // Check if the current Trie
        // marks the end of a word
        boolean getWordEndFlag() {
            return flag;
        }
    }
                            



// single class to perform all operations on that entire trie tree.
public class TrieOperations {
    private Trie root;
    public Trie() {
        root = new Trie();
    }

    // tc : o(wordlen)
    public void insert(String word) {
        Trie temp = root;
        for (int i = 0; i < word.length(); i++) {
            if (!temp.containsKey(word.charAt(i))) {
                temp.put(word.charAt(i), new Trie());
            }
            // Move to the next reference Trie
            temp = temp.get(word.charAt(i));
        }
        // Mark the end of the word
        temp.setWordEndFlag();
    }

    // Returns if the word
    // is in the trie
    public boolean search(String word) {
        Trie temp = root;
        for (int i = 0; i < word.length(); i++) {
            if (!temp.containsKey(word.charAt(i))) {
                return false;
            }
            // Move to the next Trie
            temp = temp.get(word.charAt(i));
        }
        return temp.getWordEndFlag();
    }

    // Returns if there is any word in the
    // trie that starts with the given prefix
    public boolean PrefixExist(String prefix) {
        Trie temp = root;
        for (int i = 0; i < prefix.length(); i++) {
            if (!temp.containsKey(prefix.charAt(i))) {
                return false;
            }
            // Move to the next Trie
            temp = temp.get(prefix.charAt(i));
        }
        // The prefix is found in the Trie
        return true;
    }

    public static void main(String[] args) {
        Trie trie = new Trie();
        System.out.println("Inserting words: Striver, Striving, String, Strike");
        trie.insert("striver");
        trie.insert("striving");
        trie.insert("string");
        trie.insert("strike");

        System.out.println("Search if Strawberry exists in trie: " +
                (trie.search("strawberry") ? "True" : "False"));

        System.out.println("Search if Strike exists in trie: " +
                (trie.search("strike") ? "True" : "False"));

        System.out.println("If words in Trie start with Stri: " +
                (trie.startsWith("stri") ? "True" : "False"));
    }
}
                            
                        
Editor is loading...
Leave a Comment