-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrieSET.java
183 lines (164 loc) · 6 KB
/
TrieSET.java
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/******************************************************************************
* Compilation: javac TrieSET.java
* Execution: java TrieSET < words.txt
* Dependencies: StdIn.java
*
* An set for extended ASCII strings, implemented using a 256-way trie.
*
* Sample client reads in a list of words from standard input and
* prints out each word, removing any duplicates.
*
******************************************************************************/
import java.util.Iterator;
import edu.princeton.cs.algs4.Queue;
/**
* The <tt>TrieSET</tt> class represents an ordered set of strings over
* the extended ASCII alphabet.
* It supports the usual <em>add</em>, <em>contains</em>, and <em>delete</em>
* methods. It also provides character-based methods for finding the string
* in the set that is the <em>longest prefix</em> of a given prefix,
* finding all strings in the set that <em>start with</em> a given prefix,
* and finding all strings in the set that <em>match</em> a given pattern.
* <p>
* This implementation uses a 256-way trie.
* The <em>add</em>, <em>contains</em>, <em>delete</em>, and
* <em>longest prefix</em> methods take time proportional to the length
* of the key (in the worst case). Construction takes constant time.
* <p>
* For additional documentation, see
* <a href="http://algs4.cs.princeton.edu/52trie">Section 5.2</a> of
* <i>Algorithms in Java, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
*
* @author Robert Sedgewick
* @author Kevin Wayne
*/
public class TrieSET implements Iterable<String> {
private static final int R = 26; // extended ASCII
private Node root; // root of trie
private int N; // number of keys in trie
// R-way trie node
// private static class Node {
// private Node[] next = new Node[R];
// private boolean isString;
// }
/**
* Initializes an empty set of strings.
*/
public TrieSET() {
}
/**
* Does the set contain the given key?
* @param key the key
* @return <tt>true</tt> if the set contains <tt>key</tt> and
* <tt>false</tt> otherwise
* @throws NullPointerException if <tt>key</tt> is <tt>null</tt>
*/
public boolean contains(String key) {
Node x = get(root, key, 0);
if (x == null) return false;
return x.isString;
}
public static boolean isstring(Node x) {
if (x == null) return false;
return x.isString;
}
private Node get(Node x, String key, int d) {
if (x == null) return null;
if (d == key.length()) return x;
char c = key.charAt(d);
return get(x.next[c - 'A'], key, d+1);
}
/**
* Adds the key to the set if it is not already present.
* @param key the key to add
* @throws NullPointerException if <tt>key</tt> is <tt>null</tt>
*/
public void add(String key) {
root = add(root, key, 0);
}
private Node add(Node x, String key, int d) {
if (x == null) x = new Node();
if (d == key.length()) {
if (!x.isString) N++;
x.isString = true;
}
else {
char c = key.charAt(d);
x.next[c - 'A'] = add(x.next[c - 'A'], key, d+1);
}
return x;
}
/**
* Returns the number of strings in the set.
* @return the number of strings in the set
*/
public int size() {
return N;
}
/**
* Is the set empty?
* @return <tt>true</tt> if the set is empty, and <tt>false</tt> otherwise
*/
public boolean isEmpty() {
return size() == 0;
}
/**
* Returns all of the keys in the set, as an iterator.
* To iterate over all of the keys in a set named <tt>set</tt>, use the
* foreach notation: <tt>for (Key key : set)</tt>.
* @return an iterator to all of the keys in the set
*/
public Iterator<String> iterator() {
return keysWithPrefix("").iterator();
}
/**
* Returns all of the keys in the set that start with <tt>prefix</tt>.
* @param prefix the prefix
* @return all of the keys in the set that start with <tt>prefix</tt>,
* as an iterable
*/
public Node getNode(String cur_str, int loc, Node previousNode) {
return get(previousNode, cur_str, loc);
}
public Node getRoot() {
return root;
}
public Iterable<String> keysWithPrefix(String prefix) {
Queue<String> results = new Queue<String>();
Node x = get(root, prefix, 0);
collect(x, new StringBuilder(prefix), results);
return results;
}
private void collect(Node x, StringBuilder prefix, Queue<String> results) {
if (x == null) return;
if (x.isString) results.enqueue(prefix.toString());
for (char c = 0; c < R; c++) {
prefix.append(c);
collect(x.next[c], prefix, results);
prefix.deleteCharAt(prefix.length() - 1);
}
}
}
/******************************************************************************
* Copyright 2002-2015, Robert Sedgewick and Kevin Wayne.
*
* This file is part of algs4.jar, which accompanies the textbook
*
* Algorithms, 4th edition by Robert Sedgewick and Kevin Wayne,
* Addison-Wesley Professional, 2011, ISBN 0-321-57351-X.
* http://algs4.cs.princeton.edu
*
*
* algs4.jar is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* algs4.jar is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with algs4.jar. If not, see http://www.gnu.org/licenses.
******************************************************************************/