Trie

Below is an method that builds Trie for alphabet char-based indices.

class TrieNode {
    boolean isWord;
    TierNode[] children = new TrieNode[26];
}

private void buildTrieTree(TrieNode root, String word){
    TrieNode cur = root;
    for(int i=0;i<word.length();i++){
        char ch = word.charAt(i);
        int index = ch - 'a';
        if(cur.children[index]==null){
            cur.children[index] = new TrieNode();
        }
        cur = cur.children[index];
    }
    cur.isWord = true;
}

Copyright © 2024 Ming