算法 leetcode208 实现前缀树

方法1 利用map

public class Trie {
    class TrieNode {
        Map<Character, TrieNode> next = new HashMap<>();
        boolean isEnd = false;
        public TrieNode() {
        }

    }

    private final TrieNode root = new TrieNode();
    /** Inserts a word into the trie. */
    public void insert(String word) {
        if (word == null || word.isEmpty()) {
            return;
        }
        TrieNode node = root;
        char[] chs = word.toCharArray();
        for (int i = 0; i < word.length(); i++) {
            if (node != null && !node.next.containsKey(chs[i])) {
                TrieNode trieNode = new TrieNode();
                node.next.put(chs[i], trieNode);
                node = trieNode;
            } else {
                node = node.next.get(chs[i]);
            }
        }
        node.isEnd = true;
    }

    /** Returns if the word is in the trie. */
    public boolean search(String word) {
        if (word == null || word.isEmpty()) {
            return false;
        }
        TrieNode node = root;
        char[] chs = word.toCharArray();
        for (int i = 0; i < word.length(); i++) {
            if (node != null && !node.next.containsKey(chs[i])) {
                return false;
            } else {
                node = node.next.get(chs[i]);
            }
        }
        return node.isEnd;
    }

    /** Returns if there is any word in the trie that starts with the given prefix. */
    public boolean startsWith(String prefix) {
        if (prefix == null || prefix.isEmpty()) {
            return false;
        }
        TrieNode node = root;
        char[] chs = prefix.toCharArray();
        for (int i = 0; i < prefix.length(); i++) {
            if (node != null && !node.next.containsKey(chs[i])) {
                return false;
            } else {
                node = node.next.get(chs[i]);
            }
        }
        return true;
    }

    public static void main(String[] args) {
        Trie testTire = new Trie();
        testTire.insert("word");
        testTire.insert("phone");
        testTire.insert("computer");
        boolean search = testTire.search("word");
        boolean startsWith = testTire.startsWith("ph");
    }
}

方法2 利用array

class Trie {
    private Trie[] children;
    private boolean isEnd;

    public Trie(){
        children = new Trie[26];
        isEnd = false;
    }

    public void insert(String word) {
        Trie node = this;
        for (int i = 0; i < word.length(); i++) {
            char ch = word.charAt(i);
            int index = ch - 'a';
            if (node.children[index] == null) {
                node.children[index] = new Trie();
            }
            node = node.children[index];
        }
        node.isEnd = true;
    }

    boolean search(String word){
        Trie node = searchPrefix(word);
        return node != null && node.isEnd;
    }

    boolean startsWith(String prefix){
        Trie node = searchPrefix(prefix);
        return node != null;
    }

    Trie searchPrefix(String prefix) {
        Trie node = this;
        for (int i = 0; i < prefix.length(); i++) {
            char ch= prefix.charAt(i);
            int index = ch - 'a';
            if(node.children[index] == null){
                return null;
            }
            node = node.children[index];
        }
        return node;
    }
}