-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTST.java
More file actions
210 lines (186 loc) · 6.46 KB
/
Copy pathTST.java
File metadata and controls
210 lines (186 loc) · 6.46 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import edu.princeton.cs.algs4.Queue;
import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;
/******************************************************************************
* Compilation: javac TST.java
* Execution: java TST < words.txt
* Dependencies: StdIn.java
* Data files: http://algs4.cs.princeton.edu/52trie/shellsST.txt
*
* Symbol table with string keys, implemented using a ternary search
* trie (TST).
*
*
* % java TST < shellsST.txt
* keys(""):
* by 4
* sea 6
* sells 1
* she 0
* shells 3
* shore 7
* the 5
*
* longestPrefixOf("shellsort"):
* shells
*
* keysWithPrefix("shor"):
* shore
*
* keysThatMatch(".he.l."):
* shells
*
* % java TST
* theory the now is the time for all good men
*
* Remarks
* --------
* - can't use a key that is the empty string ""
*
******************************************************************************/
/**
* Created by Mofeng on 2017/6/1.
*/
public class TST<Value> {
private int n;
private Node<Value> root;
private static class Node<Value> {
private char c;
private Node<Value> left, mid, right;
private Value val;
}
public TST() {
}
public int size() {
return n;
}
public boolean contains(String key) {
if (key == null)
throw new IllegalArgumentException("argument to conatians is null");
return get(key) != null;
}
public Value get(String key) {
if (key == null)
throw new IllegalArgumentException("Calls to get() with null Argument");
if (key.length() == 0) throw new IllegalArgumentException("Key must have length >1");
Node<Value> x = get(root, key, 0);
if (x == null) return null;
return x.val;
}
private Node<Value> get(Node<Value> x, String key, int d) {
if (x == null) return null;
if (key.length() == 0) throw new IllegalArgumentException("key must have length >1");
char c = key.charAt(d);
if (c < x.c) return get(x.left, key, d);
else if (c > x.c) return get(x.right, key, d);
else if (d < key.length() - 1) return get(x.mid, key, d + 1);
else return x;
}
public void put(String key, Value val) {
if (key == null)
throw new IllegalArgumentException("call put with null argument");
if (!contains(key)) n++;
root = put(root, key, val, 0);
}
private Node<Value> put(Node<Value> x, String key, Value val, int d) {
char c = key.charAt(d);
if (x == null) {
x = new Node<Value>();
x.c = c;
}
if (c < x.c) x.left = put(x.left, key, val, d);
else if (c > x.c ) x.right = put(x.right, key, val, d);
else if (d < key.length()-1) x.mid = put(x.mid, key, val, d);
else x.val = val;
return x;
}
public String longestPrefixOf(String query){
if (query == null)
throw new IllegalArgumentException("call to longestPrefixOf with null arguments");
if (query.length() == 0) return null;
int length = 0;
Node<Value> x = root;
int i = 0;
while (x!= null && i <query.length()) {
char c = query.charAt(i);
if (c < x.c) x = x.left;
else if (c > x.c) x = x.right;
else {
i++;
if (x.val != null) length =i;
x = x.mid;
}
}
return query.substring(0,length);
}
public Iterable<String> keys(){
Queue<String> queue = new Queue<String>();
collect(root, new StringBuilder(), queue);
return queue;
}
public Iterable<String> keysWithPrefix(String prefix){
if (prefix == null) throw new IllegalArgumentException("prefix is null");
Queue<String> queue = new Queue<String>();
Node<Value> x = get(root, prefix,0);
if (x == null) return queue;
if (x.val != null) queue.enqueue(prefix);
collect(x.mid, new StringBuilder(prefix), queue);
return queue;
}
private void collect(Node<Value> x, StringBuilder prefix, Queue<String> queue) {
if (x == null) return;
collect(x.left, prefix, queue);
if (x.val != null) queue.enqueue(prefix.toString() + x.c);
collect(x.mid, prefix.append(x.c), queue);
prefix.deleteCharAt(prefix.length() - 1);
collect(x.right, prefix, queue);
}
public Iterable<String> keysThatMatch(String pattern) {
Queue<String> queue = new Queue<String>();
collect(root, new StringBuilder(), 0, pattern, queue);
return queue;
}
public void collect(Node<Value> x, StringBuilder prefix, int i, String pattern, Queue<String> queue) {
if (x == null) return;
char c = pattern.charAt(i);
if (c == '.' || c < x.c) collect(x.left, prefix, i, pattern, queue);
if (c == '.' || c == x.c) {
if (i == pattern.length() - 1 && x.val != null)
queue.enqueue(prefix.toString() + x.c);
if (i < pattern.length() - 1) {
collect(x.mid, prefix.append(x.c), i+1, pattern, queue);
prefix.deleteCharAt(prefix.length()-1);
}
}
if (c == '.' || c > x.c) collect(x.right, prefix, i, pattern, queue);
}
public static void main(String[] args) {
// build symbol table from standard input
TST<Integer> st = new TST<Integer>();
for (int i = 0; !StdIn.isEmpty(); i++) {
String key = StdIn.readString();
st.put(key, i);
}
// print results
if (st.size() < 100) {
StdOut.println("keys(\"\"):");
for (String key : st.keys()) {
StdOut.println(key + " " + st.get(key));
}
StdOut.println();
}
StdOut.println("longestPrefixOf(\"shellsort\"):");
StdOut.println(st.longestPrefixOf("shellsort"));
StdOut.println();
StdOut.println("longestPrefixOf(\"shell\"):");
StdOut.println(st.longestPrefixOf("shell"));
StdOut.println();
StdOut.println("keysWithPrefix(\"shor\"):");
for (String s : st.keysWithPrefix("shor"))
StdOut.println(s);
StdOut.println();
StdOut.println("keysThatMatch(\".he.l.\"):");
for (String s : st.keysThatMatch(".he.l."))
StdOut.println(s);
}
}