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 }
  • 相关阅读:
    一周之内了解一个行业的方法
    du命令 实现Linux 某个文件夹下的文件按大小排序
    蝴蝶效应、青蛙现象、鳄鱼法则、鲇鱼效应.......
    MYSQ提高L查询效率的策略总结
    12个高矮不同的人,排成两排(catalan数)
    四人过桥、三盏灯 三个开关 的答案
    给定一个存放整数的数组,重新排列数组使得数组左边为奇数,右边为偶数。
    一个int 数组,里面数据无任何限制,要求求出所有这样的数a[i],其左边的数都小于等于它,右边的数都大于等于它。能否只用一个额外数组和少量其它空间实现。
    二分检索(查找)的各种变种
    排序算法稳定性
  • 原文地址:https://www.cnblogs.com/ireneyanglan/p/4873410.html
Copyright © 2011-2022 走看看