-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGramNode.java
More file actions
48 lines (39 loc) · 1.5 KB
/
Copy pathGramNode.java
File metadata and controls
48 lines (39 loc) · 1.5 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
//package anagrams.algs;
/**
*
* @author Ben Zifkin
*
*
* $$$$$ In this file lines 12,13, 34-46 should be commented out to run on dict2
*/
public class GramNode {
boolean inBigs = false;
int capacity = 35;
Word[] anagrams = new Word[capacity]; //list of anagrams in this node
int numGrams = 0;
char alpha;
GramNode[] links = new GramNode[('z' - alpha) + 1]; //map to other links.
// the 'z'-alpha is there to ensure that we don't make a needlessly large array
// e.g. if we are in 'f' we know that we will not be seeing a-e, so we have the map
public GramNode() {} // reflect that
public GramNode(char alpha) {
this.alpha = alpha;
}
public GramNode getLink(char alpha) {
if (links[alpha - 97] == null) { //-97 b/c 'a' is 97 in ASCII so subtracting gets us the proper index
links[alpha - 97] = new GramNode(alpha);
}
return links[alpha - 97];
}
public int checkAndExpand() { //just in case there are too many words in a certain node we can increase the size of the array
if (numGrams +5 >= capacity) {
Word[] old = anagrams;
capacity = capacity *2;
anagrams = new Word[capacity];
for (int i = 0; i <= old.length-1; i++) {
anagrams[i] = old[i];
}
}
return anagrams.length;
}
}