-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutocomplete.java
More file actions
105 lines (90 loc) · 3.82 KB
/
Autocomplete.java
File metadata and controls
105 lines (90 loc) · 3.82 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
import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;
import java.util.Arrays;
public class Autocomplete {
private final Term[] search; // search terms
// Initializes the data structure from the given array of terms
public Autocomplete(Term[] terms) {
if (terms == null)
throw new IllegalArgumentException("terms array cannot be null");
search = new Term[terms.length]; // create defensive copy of sorted terms
for (int i = 0; i < terms.length; i++) {
if (terms[i] != null)
search[i] = terms[i];
else throw new IllegalArgumentException("null element in terms");
}
Arrays.sort(search); // sort terms in lexicographic order
}
// Returns all terms that start with the given prefix, in descending order
// of weight
public Term[] allMatches(String prefix) {
if (prefix == null)
throw new IllegalArgumentException("Arguments cannot be null");
// use binary search to find first and last indices of terms that start
// with given prefix
int first = BinarySearchDeluxe.firstIndexOf(
search,
new Term(prefix, 0),
Term.byPrefixOrder(prefix.length())
);
if (first == -1) // return array of length 0 if no matching terms found
return new Term[0];
int last = BinarySearchDeluxe.lastIndexOf(
search,
new Term(prefix, 0),
Term.byPrefixOrder(prefix.length())
);
// create array to store matches; return sorted by desc weight order
Term[] matches = new Term[last - first + 1];
for (int i = 0; i < matches.length; i++) {
matches[i] = search[first + i];
}
Arrays.sort(matches, Term.byReverseWeightOrder());
return matches;
}
// Returns the number of terms that start with the given prefix
public int numberOfMatches(String prefix) {
if (prefix == null)
throw new IllegalArgumentException("Arguments cannot be null");
// use binary search to find first and last indices of terms that start
// with given prefix
int first = BinarySearchDeluxe.firstIndexOf(
search,
new Term(prefix, 0),
Term.byPrefixOrder(prefix.length())
);
if (first == -1) // no matching terms found
return 0;
int last = BinarySearchDeluxe.lastIndexOf(
search,
new Term(prefix, 0),
Term.byPrefixOrder(prefix.length())
);
return last - first + 1;
}
// Unit testing (sample client from assignment specification)
public static void main(String[] args) {
// read in the terms from a file
String filename = args[0];
In in = new In(filename);
int n = in.readInt();
Term[] terms = new Term[n];
for (int i = 0; i < n; i++) {
long weight = in.readLong(); // read the next weight
in.readChar(); // scan past the tab
String query = in.readLine(); // read the next query
terms[i] = new Term(query, weight); // construct the term
}
// read in queries from standard input and print the top k matching terms
int k = Integer.parseInt(args[1]);
Autocomplete autocomplete = new Autocomplete(terms);
while (StdIn.hasNextLine()) {
String prefix = StdIn.readLine();
Term[] results = autocomplete.allMatches(prefix);
StdOut.printf("%d matches\n", autocomplete.numberOfMatches(prefix));
for (int i = 0; i < Math.min(k, results.length); i++)
StdOut.println(results[i]);
}
}
}