码迷,mamicode.com
首页 > 其他好文 > 详细

Implement Trie (Prefix Tree) 解答

时间:2015-10-13 08:04:34      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:

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 class TrieNode {
 2     public char value;
 3     public boolean isLeaf;
 4     public HashMap<Character, TrieNode> children;
 5     
 6     // Initialize your data structure here.
 7     public TrieNode(char c) {
 8         value = c;
 9         children = new HashMap<Character, TrieNode>();
10     }
11 }
12 
13 public class Trie {
14     TrieNode root;
15 
16     public Trie() {
17         root = new TrieNode(‘!‘);
18     }
19 
20     // Inserts a word into the trie.
21     public void insert(String word) {
22         char[] wordArray = word.toCharArray();
23         TrieNode currentNode = root;
24         
25         for (int i = 0; i < wordArray.length; i++) {
26             char c = wordArray[i];
27             HashMap<Character, TrieNode> children = currentNode.children;
28             TrieNode node;
29             if (children.containsKey(c)) {
30                 node = children.get(c);
31             } else {
32                 node = new TrieNode(c);
33                 children.put(c, node);
34             }
35             currentNode = node;
36             
37             if (i == wordArray.length - 1)
38                 currentNode.isLeaf = true;
39         }
40         
41     }
42 
43     // Returns if the word is in the trie.
44     public boolean search(String word) {
45         TrieNode currentNode = root;
46         for (int i = 0; i < word.length(); i++) {
47             char c = word.charAt(i);
48             HashMap<Character, TrieNode> children = currentNode.children;
49             if (children.containsKey(c)) {
50                 TrieNode node = children.get(c);
51                 currentNode = node;
52                 // Need to check whether it‘s leaf node
53                 if (i == word.length() - 1 && !node.isLeaf)
54                     return false;
55             } else {
56                 return false;
57             }
58         }
59         return true;
60     }
61 
62     // Returns if there is any word in the trie
63     // that starts with the given prefix.
64     public boolean startsWith(String prefix) {
65         TrieNode currentNode = root;
66         for (int i = 0; i < prefix.length(); i++) {
67             char c = prefix.charAt(i);
68             HashMap<Character, TrieNode> children = currentNode.children;
69             if (children.containsKey(c)) {
70                 TrieNode node = children.get(c);
71                 currentNode = node;
72             } else {
73                 return false;
74             }
75         }
76         return true;
77     }
78 }
79 
80 // Your Trie object will be instantiated and called as such:
81 // Trie trie = new Trie();
82 // trie.insert("somestring");
83 // trie.search("key");

 

Implement Trie (Prefix Tree) 解答

标签:

原文地址:http://www.cnblogs.com/ireneyanglan/p/4873410.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!