-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathTrie.java
More file actions
34 lines (27 loc) · 766 Bytes
/
Trie.java
File metadata and controls
34 lines (27 loc) · 766 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package assignment4;
class Trie {
private static final int R = 26;
private Trie.Node root;
static class Node {
private Trie.Node[] next = new Trie.Node[R];
Node getNext(char ch) {
return next['A' - ch];
}
}
void put(String key) {
if (key == null) throw new IllegalArgumentException("first argument to put() is null");
else root = put(root, key, 0);
}
Node getRoot() {
return root;
}
private Trie.Node put(Trie.Node x, String key, int d) {
if (x == null) x = new Trie.Node();
if (d == key.length()) {
return x;
}
char c = key.charAt(d);
x.next['A' - c] = put(x.getNext(c), key, d + 1);
return x;
}
}