zoukankan      html  css  js  c++  java
  • Implement Trie (Prefix Tree) 解答

    Question

    Implement a trie with insertsearch, and startsWith methods.

    Note:
    You may assume that all inputs are consist of lowercase letters a-z.

    Solution

    A trie node should contains the character, its children and the flag that marks if it is a leaf node.

    Trie is an efficient information retrieval data structure. Using trie, search complexities can be brought to optimal limit (key length).

    Every node of trie consists of multiple branches. Each branch represents a possible character of keys. We need to mark the last node of every key as leaf node.

    We design TrieNode to store 1. value 2. isLeaf 3. a map of children (map is used for quick selection)

    In this way, we can implement Trie start from root. Add new child to original parent is implemented by put action in map.

    Note here, search and startsWith differs.

      null

       |

       a

       |

       b

    for "a", search will return false, while startsWith will return true.

     1 /**
     2  * Your Trie object will be instantiated and called as such:
     3  * Trie trie = new Trie();
     4  * trie.insert("lintcode");
     5  * trie.search("lint"); will return false
     6  * trie.startsWith("lint"); will return true
     7  */
     8 class TrieNode {
     9     char val;
    10     boolean isLast;
    11     Map<Character, TrieNode> children;
    12     // Initialize your data structure here.
    13     public TrieNode(char val) {
    14         children = new HashMap<Character, TrieNode>();
    15         this.val = val;
    16         this.isLast = false;
    17     }
    18 }
    19 
    20 public class Trie {
    21     private TrieNode root;
    22 
    23     public Trie() {
    24         root = new TrieNode(' ');
    25     }
    26 
    27     // Inserts a word into the trie.
    28     public void insert(String word) {
    29         if (word == null || word.length() == 0) {
    30             return;
    31         }
    32         TrieNode p = this.root;
    33         char[] arr = word.toCharArray();
    34         for (char cur : arr) {
    35             if (!p.children.containsKey(cur)) {
    36                 p.children.put(cur, new TrieNode(cur));
    37             }
    38             p = p.children.get(cur);
    39         }
    40         p.isLast = true;
    41     }
    42 
    43     // Returns if the word is in the trie.
    44     public boolean search(String word) {
    45         if (word == null || word.length() == 0) {
    46             return false;
    47         }
    48         char[] arr = word.toCharArray();
    49         TrieNode p = this.root;
    50         for (char cur : arr) {
    51             Map<Character, TrieNode> children = p.children;
    52             if (!children.containsKey(cur)) {
    53                 return false;
    54             }
    55             p = children.get(cur);
    56         }
    57         return p.isLast;
    58     }
    59 
    60     // Returns if there is any word in the trie
    61     // that starts with the given prefix.
    62     public boolean startsWith(String prefix) {
    63         if (prefix == null || prefix.length() == 0) {
    64             return false;
    65         }
    66         char[] arr = prefix.toCharArray();
    67         TrieNode p = this.root;
    68         for (char cur : arr) {
    69             Map<Character, TrieNode> children = p.children;
    70             if (!children.containsKey(cur)) {
    71                 return false;
    72             }
    73             p = children.get(cur);
    74         }
    75         return true;
    76     }
    77 }
  • 相关阅读:
    字符串----不可重叠的最长重复子串
    字符串----最长重复子串
    字符串----HDU-1358
    字符串----hiho字符串(尺取法)
    字符串匹配(二)----KMP算法
    字符串匹配(一)----Rabin-Karp算法
    字符串----最短摘要生成(尺取法)
    【Hibernate 检索策略】
    【Hibernate 多表查询】
    【Hibernate QBC】
  • 原文地址:https://www.cnblogs.com/ireneyanglan/p/4873410.html
Copyright © 2011-2022 走看看