208. Implement Trie (Prefix Tree)

题目:

Implement a trie with insertsearch, and startsWith methods.

链接: http://leetcode.com/problems/implement-trie-prefix-tree/

7/14/2017
60%,照着课件代码改写的。

将val cast成boolean,注意第21行需要设为false。以下代码可以不需要看,第一版虽然通过了,但是具体内容并不了解。

 1 public class Trie {
 2     private static final int R = 26;
 3     private Node root;
 4     
 5     private class Node {
 6         private Object val;
 7         private Node[] next = new Node[R];
 8     }
 9     /** Initialize your data structure here. */
10     public Trie() {
11         root = new Node();
12     }
13     
14     /** Inserts a word into the trie. */
15     public void insert(String word) {
16         put(root, word, true, 0);
17     }
18     private Node put(Node x, String key, Boolean val, int d) {
19         if (x == null) {
20             x = new Node();
21             x.val = false;
22         }
23         if (d == key.length()) {
24             x.val = val;            
25             return x;
26         }
27         char c = key.charAt(d);
28         x.next[c - 'a'] = put(x.next[c - 'a'], key, val, d + 1);
29         return x;
30     }
31     /** Returns if the word is in the trie. */
32     public boolean search(String word) {
33         Node x = get(root, word, 0);
34         if (x == null) return false;
35         return (boolean)x.val;
36     }
37     private Node get(Node x, String key, int d) {
38         if (x == null) return null;
39         if (d == key.length()) return x;
40         char c = key.charAt(d);
41         return get(x.next[c - 'a'], key, d + 1);
42     }
43 
44     /** Returns if there is any word in the trie that starts with the given prefix. */
45     public boolean startsWith(String prefix) {
46         Node x = get(root, prefix, 0);
47         return x != null;
48     }
49 }
50 
51 /**
52  * Your Trie object will be instantiated and called as such:
53  * Trie obj = new Trie();
54  * obj.insert(word);
55  * boolean param_2 = obj.search(word);
56  * boolean param_3 = obj.startsWith(prefix);
57  */

不知道为什么笔记上一定要让val的type是Object,其实改成boolean也是可以的,99+%

查了下老大爷的视频:

One slight complication in the implementation that we encountered before with hashing algorithms and other things. We're going to need arrays of nodes. That's what our lengths are. So we can't have any generics with nodes, even though I would like to. So we have to declare the value to be a type object, and then we'll have to cast it back to whatever type it should be, when we return it.

use Object instead of Value since no generic array creation in Java

这部分估计跟Java Generic部分相关,但是我是Java渣,不太懂。

 1 public class Trie {
 2     private static final int R = 26;
 3     private Node root;
 4     
 5     private class Node {
 6         private boolean val;
 7         private Node[] next = new Node[R];
 8     }
 9     /** Initialize your data structure here. */
10     public Trie() {
11         root = new Node();
12     }
13     
14     /** Inserts a word into the trie. */
15     public void insert(String word) {
16         put(root, word, true, 0);
17     }
18     private Node put(Node x, String key, Boolean val, int d) {
19         if (x == null) {
20             x = new Node();
21         }
22         if (d == key.length()) {
23             x.val = val;            
24             return x;
25         }
26         char c = key.charAt(d);
27         x.next[c - 'a'] = put(x.next[c - 'a'], key, val, d + 1);
28         return x;
29     }
30     /** Returns if the word is in the trie. */
31     public boolean search(String word) {
32         Node x = get(root, word, 0);
33         if (x == null) return false;
34         return x.val;
35     }
36     private Node get(Node x, String key, int d) {
37         if (x == null) return null;
38         if (d == key.length()) return x;
39         char c = key.charAt(d);
40         return get(x.next[c - 'a'], key, d + 1);
41     }
42 
43     /** Returns if there is any word in the trie that starts with the given prefix. */
44     public boolean startsWith(String prefix) {
45         Node x = get(root, prefix, 0);
46         return x != null;
47     }
48 }
49 
50 /**
51  * Your Trie object will be instantiated and called as such:
52  * Trie obj = new Trie();
53  * obj.insert(word);
54  * boolean param_2 = obj.search(word);
55  * boolean param_3 = obj.startsWith(prefix);
56  */

这道题越写到这里越发现很多好玩的地方:

1. Object vs Value?

只要代码里不是用generics,其实直接用类是没有问题的。如果用了generic,那就是第三个问题。

2. private static class Node, why static is needed?

https://stackoverflow.com/a/28468158

3. How this question is different from princeton algorithm code?

这道题不需要generics,老大爷的代码里最开始就是用了generics:  public class TrieST

关于以上的问题可以参考:

http://algs4.cs.princeton.edu/lectures/52Tries.pdf

http://x-wei.github.io/algoII_week4_1.html

http://www.geeksforgeeks.org/static-class-in-java/

https://stackoverflow.com/questions/28467984/why-static-nested-class

http://docs.oracle.com/javase/tutorial/java/generics/restrictions.html#createArrays

http://algs4.cs.princeton.edu/52trie/TrieST.java.html

以及

《数据结构与算法分析-Java语言描述》1.5,3.4

关注一个账户,属于学习比较深入的,因为在面试时候不深入又想装x是很虚的。。。

http://x-wei.github.io/tag/algorithm.html

 

官方解释(非常好,争取二刷写到这个程度)

https://leetcode.com/articles/implement-trie-prefix-tree/

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

更多讨论

https://discuss.leetcode.com/category/216/implement-trie-prefix-tree

posted @ 2017-07-15 08:00  panini  阅读(164)  评论(0编辑  收藏  举报