java Trie tree implementation

leetcode Address:

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

Difficulty: Moderate

Description: Slightly

 

Problem-solving ideas:

Trie dictionary lookup tree is a tree, is a string that can be implemented in a centralized fast search and matching multi-tree structure, on the Trie-depth analysis I will not start, because I myself do not understand the profound ^ _ ^, given here only define the Trie, and common scenarios, and give a simple java implementation, of course, a great simplicity of code to optimize space and performance.

First, define Trie tree (or nature):

1. The root node is empty, does not contain character

2. Each node contains a character, and a number of child nodes

3. The characters all child nodes of each node contains are not the same

All characters on the paths from each node to the root node of the tree 3. The composition of a string, the string may be represented in the tree exist, or at least present in the Trie this prefix string is a string

4. Each non-root nodes should comprise an integer value that represents the number of the root node to the nodes in the Trie string appears in

 

Trie number of common scenarios:

1. string retrieval

2. Frequency Statistics

3. retrieval prefix

4. prefix word frequency statistics

5. Sort all the strings in lexicographic order

 

java implementation:

public class Trie {
    public static void main(String[] args) {
        Trie trie = new Trie();
        trie.insert("apple");
        System.out.println(trie.search("apple"));
        System.out.println(trie.search("app"));     // returns false
        System.out.println(trie.startsWith("app")); // returns true
        trie.insert("app");
        System.out.println(trie.search("app"));     // returns true
    }

    TrieNode root;

    / ** 
     . * Data Structure your here Wallpaper the Initialize 
     * / 
    public Trie () { 
        the root = new new TrieNode (); 
    } 

    / ** 
     * Inserts Number INTO A Word The Trie. 
     * / 
    Public  void INSERT (String Word) { 
        INSERT (Word, root, 0 ); 
    } 

    / ** 
     * index from the string starting at the root node is inserted into the child, i.e. index corresponding character into a sub-root node 
     * @param Word 
     * @param root 
     * @param index
      * / 
    Private  void INSERT (String Word, TrieNode root, int index) {
        assert index < word.length() && index > -1;
        char cur = word.charAt(index);
        TreeMap<Character, TrieNode> children = root.children;
        if (null == children) {
            children = new TreeMap<>();
            root.children = children;
        }
        if (!children.containsKey(cur)) {
            children.put(cur, new TrieNode(cur));
        }
        if (index == word.length() - 1) {
            children.get (CUR) .occurency ++ ;
             return ; 
        } 
        INSERT (Word, children.get (CUR), index +. 1 ); 
    } 

    / ** 
     * Returns The IF The Word in IS Trie. 
     * / 
    public  Boolean Search ( Word String) {
         return Search (Word, root, 0 ); 
    } 

    / ** 
     * search for the root node of the sub-index from the beginning of the string 
     * @param Word 
     * @param root 
     * @param index 
     * @return 
     * / 
    Private  boolean search(String word, TrieNode root, int index) {
        assert index > -1 && index < word.length();
        char cur = word.charAt(index);
        if (root.children == null ||
                !root.children.containsKey(cur)) {
            return false;
        }
        if (index == word.length() - 1) {
            return root.children.get(cur).occurency > 0;
        }
        return search(word, root.children.get(cur), index + 1);
    }

    /**
     Returns the any IF there IS * Word The Trie in that Soho starts with GIVEN The prefix. 
     * / 
    Public  Boolean startsWith (String prefix) {
         return startsWith (prefix, root, 0 ); 
    } 

    / ** 
     * search from the root node of the sub corresponding to string index started prefix 
     * @param prefix 
     * @param the root 
     * @param index 
     * @return 
     * / 
    Private  Boolean startsWith (String prefix, the root TrieNode, int index) {
         Assert index> -1 && index < prefix.length ( );
         char cur = prefix.charAt(index);
        if (root.children == null ||
                !root.children.containsKey(cur)) {
            return false;
        }
        if (index == prefix.length() - 1) {
            return true;
        }
        return startsWith(prefix, root.children.get(cur), index + 1);
    }

    static class TrieNode {
        char c;
        int occurency = 0;
        TreeMap<Character, TrieNode> children;

        public TrieNode() {
        }

        public TrieNode(char c) {
            this.c = c;
        }
    }
}

 

Guess you like

Origin www.cnblogs.com/zhuge134/p/11072470.html